leonard krasniqi

leonard krasniqi

  • 2.1k
  • 22
  • 374

How to check if a row exist in sql table using datagridview column?

Nov 5 2020 5:19 AM
Hi
I am trying to check if a row exist in sql using a datagridview column
I am using these lines of code:
SqlCommand cmdcontrol= new SqlCommand("checkbarcode", conn);
cmdcontrol.CommandType = CommandType.StoredProcedure;
cmdcontrol.Parameters.AddWithValue("@barcode", Convert.ToString(tgproduct.Rows[i].Cells[0].Value));
int product = (int)cmdcontrol.ExecuteScalar();
if (product > 0)
{
//update qty of product based on barcode (from column index 0)
}
else
{
//do a insertion process with all details from datagridview
}
Note that the datagridview may have product that aren't inserted in sql , and products that are inserted in sql and need to update only QTY.
I tested the code to do insertion or update lonely (not both cases in same time) and code works, but when i mix both cases on of the processes isn't done(update or insert process).
I think the problem is in the loop code, where data from datagridview are checked to see if they exist in sql(and based on result the insertion/update process is done).
Could someone help me why when I put a product in datagridview which exist in sql( and need to update qty), and a procut that doesn't exist in sql table(need to insert the product), both process doesn't happen?
Thanks to everyone

Answers (5)