Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello Guys.

need your support again.

I am trying to insert data from datagrid into sql and gets the error after the records have been inserted. the following is the code i am using:

SQL
Dim Insert As New SqlCommand("Insert into FAGR(FAGRC,FAGRD,FAGRN,FAGRU) Values(@FAGRC,@FAGRD,@FAGRN,@FAGRU)", connection)

            Insert.Parameters.Add(New SqlParameter("@FAGRC", SqlDbType.NChar, 3))
            Insert.Parameters.Add(New SqlParameter("@FAGRD", SqlDbType.NChar, 1))
            Insert.Parameters.Add(New SqlParameter("@FAGRN", SqlDbType.NVarChar, 50))
            Insert.Parameters.Add(New SqlParameter("@FAGRU", SqlDbType.NChar, 3))

            For I As Integer = 0 To DataGridView1.Rows.Count-1
                Insert.Parameters(0).Value = DataGridView1.Rows(I).Cells(0).Value
                Insert.Parameters(1).Value = DataGridView1.Rows(I).Cells(1).Value
                Insert.Parameters(2).Value = DataGridView1.Rows(I).Cells(2).Value
                Insert.Parameters(3).Value = DataGridView1.Rows(I).Cells(3).Value
                Insert.ExecuteNonQuery()

            Next
Posted

1 solution

Check your row data: if you have a null value in any row of the first column, you will get this error.

(One way to find out is to put a try...catch block inside the For loop, and put a breakpoint on the exception report - you can then use I to tell which row gives the error.)
 
Share this answer
 
Comments
atul sharma 5126 30-Jan-14 11:48am    
The following exception is generated from catch:
Index was out of range. Must be non-negative and less then size of the collection.
Parameter name: index
-------
OriginalGriff 30-Jan-14 12:00pm    
So, what does the row look like? Did you check it in the debugger? Are there at least four columns? (Note: don't look at your form to answer that - look at the debugger, as the code could be looking at different data and you need to know that!)
atul sharma 5126 30-Jan-14 11:56am    
By breakpoint known: even if 1 record is added, the loop is moving twice and each time the exception is generated
atul sharma 5126 30-Jan-14 12:12pm    
FAGRD was not getting value so i changed the parameters to:
Insert.Parameters.Add(New SqlParameter("@FAGRD", "Y"))

but still the same error
atul sharma 5126 30-Jan-14 12:19pm    
I am able to insert the data in database by following changes. but after pressing ok on error

For I As Integer = 0 To DataGridView1.Rows.Count - 1
Try
Insert.Parameters(0).Value = DataGridView1.Rows(I).Cells(0).Value

Insert.Parameters(2).Value = DataGridView1.Rows(I).Cells(1).Value
Insert.Parameters(3).Value = DataGridView1.Rows(I).Cells(2).Value
vartemp1 = Insert.ExecuteNonQuery()

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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