You are changing variable
sql
to a new SQL statement. But you do not change SqlCommand.CommandText ?
if (rdbAdd.Checked == true)
{
if (txtBarcode.Text.Length > 7)
{
txtBarcode.Text = txtBarcode.Text.Remove(0, 7);
txtBarcode.SelectionStart = txtBarcode.Text.Length;
}
if (txtBarcode.Text.Length == 7)
{
sql = "select amount from storedData where barcode='" + txtBarcode.Text + "'";
cmd = new SqlCommand(sql, con);
amount = 0;
con.Open();
reader = cmd.ExecuteReader();
try
{
reader.Read();
if (reader.Read() == true)
{
amount = (int)reader["Amount"];
amount++;
sql = "update storedData set amount='" + amount + "' where barcode='" + txtBarcode.Text + "'";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
else
{
MessageBox.Show("new barcode add first");
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
con.Close();
}
}
But it will proberly fail any way. As long you have are running a ExecuteReader(), you cannot use the same SqlConnection for other SQL statements.