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); }
} } }
|
Kirtan PatelPosted Oct 5, 2009, 9:12 PM
You are trying to insert the Same Value of Primary Key Column into Table
you have defined ProID as Primary Key So You can not Insert Same Value of ProID again
make sure you not Insert Same Value of ProID into Database
:)
dont forget to mark "do you like this answer" if answer helped you please :)