I encountered a weird problem here. Whenever I run the update query, in the Windows Form I could see the updated value in the DataGridView. But after I stop the program, I check my Database through SQL Server, it doesn't show up the updated value.
try
{
using (SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\SDM.mdf;Integrated Security=True;Connect Timeout=30"))
{
int updateValue = Int32.Parse(numericUpDown.Text);
string productname = productnamecb.Text;
string getvalue = "SELECT Product_UnitStock FROM Product WHERE Product_Name ='" + productname + "'";
SqlCommand cmd = new SqlCommand(getvalue, conn);
conn.Open();
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
rd.Read(); // read first row
var oldvalue = rd.GetInt32(0);
conn.Close();
int total = oldvalue + updateValue;
string updateData = "UPDATE Product SET Product_UnitStock = " + total + " WHERE Product_Name = '" + productname + "'";
SqlCommand cmmd = new SqlCommand(updateData, conn);
conn.Open();
cmmd.ExecuteNonQuery();
conn.Close();
}
else
{
MessageBox.Show("System Error", "System Error");
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
This is the value seen in the Windows Form through DataGridView. As you can see for the 1st row, the Product Stock is "92".
But whenever I close the program, I check through the SQL Server it returns to the default value which is "80".