using a Transaction instead of single commits might also solve your Problem and is less Performance killing
just write it like this:
con.Open();
private SqlTransaction trans = con.BeginTransaction();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
cmd.Parameters["@itemno"].Value = row.Cells[0].Value;
cmd.Parameters["@itemname"].Value = row.Cells[1].Value;
cmd.Parameters["@price"].Value = row.Cells[2].Value;
cmd.Parameters["@quantity"].Value = row.Cells[3].Value;
cmd.Parameters["@weight"].Value = row.Cells[4].Value;
cmd.Parameters["@itemtotal"].Value = row.Cells[5].Value;
cmd.Transaction = trans;
cmd.ExecuteNonQuery();
}
}
MessageBox.Show("Procedure Executed");
try
{
this.trans.Commit();
this.trans.Dispose();
this.trans = null;
}
catch ()
{
}
finally
{
if (trans != null)
{
trans.Dispose();
trans = null;
}
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally
{ con.Close(); }
}
If i made no mistakes "i hope so", the cmd is stored into the Transaction and is commited after trans.Commit(), so your commands get done in correct order but in "one step".
No double inserts and if something goes wrong, just Transaction.Rollback();
and no changes will be done, ist like a buffer i'd say.
:)