我遇到了这个名为Object必须实现IConvertible的错误。 请帮我! 我认为这是因为用户类型的下拉列表。即使我删除/注释掉下拉列表,我仍然会遇到此错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Admin_Register : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(Helper.GetCon());
protected void Page_Load(object sender, EventArgs e)
{
GetUserTypes();
}
void GetUserTypes()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT TypeID, TypeName FROM UserTypes";
SqlDataReader data = cmd.ExecuteReader();
ddlUserTypes.DataSource = data;
ddlUserTypes.DataTextField = "TypeName";
ddlUserTypes.DataValueField = "TypeID";
ddlUserTypes.DataBind();
con.Close();
}
bool IsRecordExisting(string email)
{
bool existing = true;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT EmailAddress FROM Users WHERE EmailAddress=@EmailAddress";
cmd.Parameters.Add("@EmailAddress", SqlDbType.VarChar).Value = email;
SqlDataReader data = cmd.ExecuteReader();
if (data.HasRows)
existing = true;
else
existing = false;
con.Close();
return existing;
}
protected void btnRegister_Click(object sender, EventArgs e)
{
if (!IsRecordExisting(txtEmail.Text))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO Users VALUES (@EmailAddress, @Password, @TypeID, @FirstName, @LastName, @MiddleInitial, @Address, @TelNo, @CelNo, @Status";
cmd.Parameters.Add("@EmailAddress", SqlDbType.VarChar).Value = txtEmail.Text;
cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = Helper.CreateSHAHash(txtPassword.Text);
cmd.Parameters.Add("@TypeID", SqlDbType.Int).Value = ddlUserTypes.SelectedValue;
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFN;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLN;
cmd.Parameters.Add("@MiddleInitial", SqlDbType.VarChar).Value = "";
cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = "";
cmd.Parameters.Add("@TelNo", SqlDbType.VarChar).Value = "";
cmd.Parameters.Add("@CelNo", SqlDbType.VarChar).Value = "";
cmd.Parameters.Add("@Status", SqlDbType.VarChar).Value = "Active";
cmd.ExecuteNonQuery();
con.Close();
Helper.Log("0", "Register", "User Registration");
Helper.ClearTextboxes(this.Controls);
register.Visible = true;
}
else
{
register.Visible = false;
error.Visible = true;
}
}
}
答案 0 :(得分:0)
我在您的代码中发现了两个问题
<强>第一强>
请检查这些行
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFN;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLN;
您将参数值设置为Texbox。您需要将文本框的文本设置为参数值。
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFN.Text;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLN.Text;
<强>第二强>
下拉列表 SelectedValue 属性返回字符串值。你需要在分配时将字符串值转换为int类型。
请更改此行
cmd.Parameters.Add("@TypeID", SqlDbType.Int).Value = ddlUserTypes.SelectedValue;
到
cmd.Parameters.Add("@TypeID", SqlDbType.Int).Value = Convert.ToInt32(ddlUserTypes.SelectedValue);