我正在尝试向 sql server 插入一些数据,但收到错误消息,例如“违反主键约束 'PK_login1'。无法在对象 'dbo.login1' 中插入重复键。语句已终止。”。我试图从表中删除主键约束,之后我看到它正在向表中插入两个以上类似的数据。我的代码是
protected void btn_Submit_Click(object sender, EventArgs e)
{
try
{
SqlCommand cmd;
str = "Insert into login1 values ('" + txtbx_Uname.Text + "', '" + txtbx_Pwd.Text + "', '" + txtbx_Email.Text + "', '" + txtbx_Dob.Text + "', " + txtbx_Phone.Text + ")";
con.Open();
cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
lbl_Error.Visible = true;
lbl_Error.Text = "Registration Success";
int n = Convert.ToInt32(cmd.ExecuteScalar());
if(n==1)
Response.Redirect("Login.aspx");
con.Close();
}
catch
{
lbl_Error.Visible = true;
lbl_Error.Text = "SQL Server Error. Pleaase try after sometime";
}
}