我正在构建将用户信息保存到本地数据库的用户注册页面。但是,我得到一个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
请***不要***在您的数据库表中以**明文**存储密码!这只是要求被黑客攻击和暴露.... –