Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
cmd.Parameters.Add(new SqlParameter("@itemno", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@itemname", SqlDbType.VarChar));
cmd.Parameters.Add(new SqlParameter("@price", SqlDbType.Decimal));
cmd.Parameters.Add(new SqlParameter("@quantity", SqlDbType.Decimal));
cmd.Parameters.Add(new SqlParameter("@weight", SqlDbType.Decimal));
cmd.Parameters.Add(new SqlParameter("@itemtotal", SqlDbType.Decimal));


C#
con.Open();
                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.ExecuteNonQuery();
                MessageBox.Show("Procedure Executed");
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
            finally
            { con.Close(); }

        }



My last row data is inserting twice in database ....
how can i solve this problem

I have tried t use cmd.ExecuteNonQuery(); outside of loop , but still my last row data in sql database is inserting twice (repeatedly ) in database..........

plz suggest write code to correct error........in this code
Posted
Updated 7-Dec-13 9:12am
v2
Comments
Karthik_Mahalingam 5-Dec-13 8:58am    
try this DataGridViewRow .AllowUserToAddRows = false
joginder-banger 7-Dec-13 15:22pm    
try this. DataGridViewRow1.Rows.Count-1

Your problem is because you are calling
cmd.ExecuteNonQuery();
inside the loop and then once outside.

If you remove that line from just before the MessageBox.Show line then it should solve your issue.
 
Share this answer
 
using a Transaction instead of single commits might also solve your Problem and is less Performance killing

just write it like this:

C#
con.Open();
//initializing transaction
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;
                        //adding command to the Transaction and "executing" it
                        cmd.Transaction = trans;
                        cmd.ExecuteNonQuery();
                    }
 

                }
                MessageBox.Show("Procedure Executed");
                try
                {
                    //commit the Transaction ( all commands)
                    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.

:)
 
Share this answer
 
Comments
Mike Meinz 7-Dec-13 16:55pm    
+5
Nice job!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900