我正在尝试创建一个注册页面,但它没有按计划进行。它一直抛出这个错误:
“System.Data.SqlClient.SqlException”类型的异常发生在System.Data.dll中,但未在用户代码中处理
在我的桌子上,
[userid] INT NOT NULL,
[username] VARCHAR (50) NULL,这是我的c#代码
protected void Button1_Click(object sender, EventArgs e)
{
if (txtPassword.Text == txtConfirmPassword.Text)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
string confirmpassword = txtConfirmPassword.Text;
string email = txtEmail.Text;
con.Open();
String str = "INSERT INTO users (userid, username, password, confirmpassword, email) VALUES(@userid, @username, @password, @confirmpassword, @email)";
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
cmd.Parameters.AddWithValue("@confirmpassword", confirmpassword);
cmd.Parameters.AddWithValue("@email", email);
cmd.ExecuteNonQuery();
lblMessage.Text = "OK!!!!!!!!!!!!!!!!!!!";
con.Close();
}
}我不知道如何传递用户I?
更新需要在数据库中将标识设置为userid。谢谢大家
发布于 2015-02-25 21:46:12
您的代码试图编写5个字段,但只传递4个参数。
@userid参数在SqlCommand.Parameters集合中丢失。
现在有两种可能性。如果用户is字段是标识列,则不应该为userid字段传递任何内容,查询将变为
String str = @"INSERT INTO users (username, password, confirmpassword, email)
VALUES(@username, @password, @confirmpassword, @email)";或者,如果它是一个正常字段,则需要将该参数添加到SqlCommand参数集合中。
发布于 2015-02-25 21:48:18
首先,如果isIdentity设置为true,则不要传递userId param。第二,即使在连接字符串中也提到了它,但您可以在查询中通过CatalogName.dbo.TableName调用表。
"INSERT INTO dbName.dbo.users (username, password, confirmpassword, email) VALUES(@username, @password, @confirmpassword, @email)";https://stackoverflow.com/questions/28730139
复制相似问题