我試圖插入一些數據到sql服務器,但我收到錯誤消息,如「違反PRIMARY KEY約束'PK_login1'。無法在對象中插入重複鍵'dbo .login1'語句已被終止。「我試圖從表中刪除主鍵約束,之後我發現它將多於兩個類似的數據插入表中。我的代碼是違反PRIMARY KEY約束'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";
}
}
什麼是你的主鍵 –
@COLDTOLD我使用的電子郵件地址作爲主鍵 – Pritam
那麼你必須檢查是否有相同的電子郵件用戶做插入 –