错误:-System.Data.SqlClient.SqlException(0x80131904):列名称'Username'无效。
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into Registration (Name,Username,Password,Email,Phone) values ('" + tname.Text + "','" + tusename.Text + "','" + tpassword.Text + "','" + temail.Text + "','" + tphonenumber.Text + "')";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Name",tname.Text);
com.Parameters.AddWithValue("@Usename",tusename.Text);
com.Parameters.AddWithValue("@Password",tpassword.Text);
com.Parameters.AddWithValue("@Email",temail.Text);
com.Parameters.AddWithValue("@Phone",tphonenumber.Text);
com.ExecuteNonQuery();
Response.Redirect("RegistrationDatasave.aspx");
Response.Write("Registration is successful");
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex);
}
}
答案 0 :(得分:2)
如果查询中没有参数,则参数不起作用。
您的查询应该是这样的
using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString))
{
conn.Open();
string insertQuery = "insert into Registration (Name,Username,Password,Email,Phone) values (@Name,@Usename,@Password,@Email,@Phone)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Name",tname.Text);
com.Parameters.AddWithValue("@Usename",tusename.Text);
com.Parameters.AddWithValue("@Password",tpassword.Text);
com.Parameters.AddWithValue("@Email",temail.Text);
com.Parameters.AddWithValue("@Phone",tphonenumber.Text);
com.ExecuteNonQuery();
Response.Redirect("RegistrationDatasave.aspx");
Response.Write("Registration is successful");
}
此外,您需要检查您的注册表,它实际上有一个名为Username
答案 1 :(得分:0)
我认为您的列名错误。
com.Parameters.AddWithValue("@Usename",tusename.Text);
需要
com.Parameters.AddWithValue("@Username",tusename.Text);
所以添加了r