我正在構建將用戶信息保存到本地數據庫的用戶註冊頁面。但是,我得到一個SqlException
錯誤。有誰知道我在這裏做錯了嗎?我正在開發ASP.net中的程序並使用本地數據庫服務器。SqlException未由代碼用戶處理
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisterConnectionString"].ConnectionString);
conn.Open();
string checkUser = "select count(*) from Table where userName = '" + txtUN.Text + "'";
SqlCommand comm = new SqlCommand(checkUser, conn);
int temp = Convert.ToInt32(comm.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("user already exist");
}
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisterConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into Table(UserName, name, Address, e-Mail, IC, phone, password) values(@Uname, @name, @add, @mail, @ic, @phone, @pswrd) ";
SqlCommand comm = new SqlCommand(insertQuery, conn);
comm.Parameters.AddWithValue("@Uname", txtUN.Text);
comm.Parameters.AddWithValue("@name", txtName.Text);
comm.Parameters.AddWithValue("@add", txtAdd.Text);
comm.Parameters.AddWithValue("@mail", txtEmail.Text);
comm.Parameters.AddWithValue("@ic", txtIC.Text);
comm.Parameters.AddWithValue("@phone", txtPhone.Text);
comm.Parameters.AddWithValue("@pswrd", txtPsswrd.Text);
comm.ExecuteNonQuery();
Response.Redirect("Default.aspx");
Response.Write("registration was succesful");
conn.Close();
}
catch(Exception ex)
{
Response.Write("error"+ex.ToString());
}
}
哪裏引發異常? Page_Load或Button_Click? – Steve
在VS中,您可以查看異常的詳細信息。這可能會給你提示發生了什麼。如果不是,請將異常詳細信息複製到此帖子。可以幫助其他人提供答案。 – Ladi
請***不要***在您的數據庫表中以**明文**存儲密碼!這只是要求被黑客攻擊和暴露.... –