1
Answer

Can't Insert & update to database Error SQLException

mahaboyd

mahaboyd

15y
4.3k
1
Can't Insert & update to database This Error

Violation of PRIMARY KEY constraint 'PK_Product'. Cannot insert duplicate key in object 'dbo.Product'.
The statement has been terminated.


Error Line -->  
cmd.ExecuteNonQuery();

 String Con = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|"
+ "\\TCTSuperMarket.mdf;Integrated Security=True;User Instance=True";//???????????? string ????????????????????????????
SqlCommand cmd = new SqlCommand();


 private void btedit_Click(object sender, EventArgs e)
{
btsave.Enabled = false;
string strSQL;
SqlConnection Conn = new SqlConnection(Con);
//try
//{
strSQL = "UPDATE Product SET " +
" ProID = '" + this.txtProID.Text + "' " +
" ,ProName = '" + this.txtproname.Text + "' " +
" ,Detail = '" + this.txtdetailpro.Text + "' " +
" ,NumInventory = '" + this.txtnuminventory.Text + "' " +
" ,cost = '" + this.txtcost.Text + "' " +
" ,Price = '" + this.txtprice.Text + "' ";
cmd = new SqlCommand();
cmd.Connection = Conn;
cmd.CommandText = strSQL;
cmd.CommandType = CommandType.Text;
Conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("?????????????????????????????! ", "??????", MessageBoxButtons.OK, MessageBoxIcon.Information);

//}
//catch (Exception ex)
//{
// MessageBox.Show("??????????????????????? ! ????????????????????????????????? ", "?????????!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//}




}


 private void btsave_Click(object sender, EventArgs e)
{
int intnumrows;
SqlConnection Conn = new SqlConnection(Con);
Conn.Open();
String strSQL;
strSQL = "SELECT COUNT(*) FROM Product WHERE ProID = '" + this.txtProID.Text + "' ";
cmd = new SqlCommand(strSQL, Conn);
intnumrows = Convert.ToInt32(cmd.ExecuteScalar());
if (intnumrows > 0)
{
MessageBox.Show("?????????????????????????????????????????? ????????????!", "?????????!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtProID.Focus();
}
else
{
if (txtProID.Text == "" || txtproname.Text == "" || txtdetailpro.Text == "" ||
txtnuminventory.Text == "" || txtcost.Text == "" || txtprice.Text == "")
{
MessageBox.Show("?????????????????????????", "???????", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtProID.Focus();
}
else
{
strSQL = "INSERT INTO Product(ProID,ProName,Detail,NumInventory,Cost,Price)" +
" values(@ProID,@ProName,@Detail,@NumInventory,@Cost,@Price)";
// SqlCommand cmd = new SqlCommand("INSERT INTO Product(ProID,ProName,Detail,NumInventory,Cost,Price)" +
// " values(@ProID,@ProName,@Detail,@NumInventory,@Cost,@Price)", Conn);
cmd = new SqlCommand(strSQL, Conn);
cmd.Parameters.AddWithValue("@ProID", txtProID.Text);
cmd.Parameters.AddWithValue("@ProName", txtproname.Text);
cmd.Parameters.AddWithValue("@Detail", txtdetailpro.Text);
cmd.Parameters.AddWithValue("@NumInventory", txtnuminventory.Text);
cmd.Parameters.AddWithValue("@Cost", txtcost.Text);
cmd.Parameters.AddWithValue("@Price", txtprice.Text);
try
{
Conn.Open();
cmd.ExecuteNonQuery();
ShowData();
MessageBox.Show("??????????????????????????", "?????????????????????", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtProID.Text = "";
txtproname.Text = "";
txtdetailpro.Text = "";
txtnuminventory.Text = "";
txtcost.Text = "";
txtprice.Text = "";
Conn.Close();
}
catch (Exception)
{
MessageBox.Show("Error!! ?????????????????????????", "?????????", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

}
}
}

Answers (1)