It able to update to the database but it pop up error message like this "Violation of PRIMARY KEY constraint 'PK_ProductStock_1'. Cannot insert duplicate key in object 'dbo.ProductStock.' The statement has been terminated."
Your help will be appreciated
Thx you
if (CheckBoxSub8XS.Checked == true && txtSub8XS.Text != "")
{
using (SqlConnection conStrstock8 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
string sqlupStock8 = "UPDATE ProductStock SET ProductQty=@qty1 WHERE ProductID=@pid AND ProductColorType=@protType AND ProductSize=@size";
using (SqlCommand cmdupstock8 = new SqlCommand(sqlupStock8, conStrstock8))
{
cmdupstock8.Parameters.AddWithValue("@size", " XS");
cmdupstock8.Parameters.AddWithValue("@qty1", txtSub8XS.Text);
cmdupstock8.Parameters.AddWithValue("@protType", "Type8");
cmdupstock8.Parameters.AddWithValue("@pid", Id);
conStrstock8.Open();
cmdupstock8.ExecuteNonQuery();
conStrstock8.Close();
}
}
}
if (CheckBoxSub8XS.Checked == true)
{
using (SqlConnection conINstock8 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
string sqlINStock8 = "INSERT INTO ProductStock(ProductID,ProductColorType,ProductSize,ProductQty,ProductSizeEntre,ProductSizeQty) VALUES(@pid,@protType,@size,@qty,@sizeEnt,@sizeQty)";
using (SqlCommand cmdINstock8 = new SqlCommand(sqlINStock8, conINstock8))
{
cmdINstock8.Parameters.AddWithValue("@pid", Id);
cmdINstock8.Parameters.AddWithValue("@protType", "Type8");
cmdINstock8.Parameters.AddWithValue("@size", " XS");
cmdINstock8.Parameters.AddWithValue("@qty", txtSub8XS.Text);
cmdINstock8.Parameters.AddWithValue("@sizeEnt", " XS");
cmdINstock8.Parameters.AddWithValue("@sizeQty", txtSub8XS.Text);
conINstock8.Open();
cmdINstock8.ExecuteNonQuery();
conINstock8.Close();
}
}
}
Member
9 Points
21 Posts
Update and Insert Problem
Dec 08, 2013 10:34 AM|kckcOng|LINK
It able to update to the database but it pop up error message like this "Violation of PRIMARY KEY constraint 'PK_ProductStock_1'. Cannot insert duplicate key in object 'dbo.ProductStock.' The statement has been terminated."
Your help will be appreciated
Thx you