Datagridview error

RIAS
RIAS used Ask the Experts™
on
Hello,
I get an error :
Row provided does not belong to this DataGridView control.
Parameter name: e.Row

Code:
 If con.State = ConnectionState.Closed Then
                    con.Open()
                End If
                com.Connection = con
                DataGridView1.VirtualMode = False
                'Commented out for messsage queue load on the server
                If DataGridView1.Rows.Count > 0 Then
                    'Commented out for messsage queue load on the server
                    For Each rw As DataGridViewRow In DataGridView1.Rows

                        com.CommandText = ("insert query" )


                        count = com.ExecuteNonQuery()
                        'Commented  for messsage queue load on the server
                        Application.DoEvents()
                        'Commented  for messsage queue load on the server

                        'Objwriter.WriteLine("Total Rows:" + count.ToString)
                        'Objwriter.Flush()

                        com.CommandTimeout = 725
                        com.ExecuteNonQuery()


                        Success = True
                    Next
                End If

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
You might want to post the code where you get the error.  (you don't use e.row anywhere I see in your code snippet).

Author

Commented:
I get error on     For Each rw As DataGridViewRow In DataGridView1.Rows

If con.State = ConnectionState.Closed Then
                    con.Open()
                End If
                com.Connection = con
                DataGridView1.VirtualMode = False
                'Commented out for messsage queue load on the server
                If DataGridView1.Rows.Count > 0 Then
                    'Commented out for messsage queue load on the server
                [b]    For Each rw As DataGridViewRow In DataGridView1.Rows[/b]

                        com.CommandText = ("insert query" )


                        count = com.ExecuteNonQuery()
                        'Commented  for messsage queue load on the server
                        Application.DoEvents()
                        'Commented  for messsage queue load on the server

                        'Objwriter.WriteLine("Total Rows:" + count.ToString)
                        'Objwriter.Flush()

                        com.CommandTimeout = 725
                        com.ExecuteNonQuery()


                        Success = True
                    Next
                End If

Open in new window

Author

Commented:
error2 on the same line :

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Author

Commented:
Is there any check needed on the code?
AndyAinscowFreelance programmer / Consultant

Commented:
With the exact error message you show in the question body?

Author

Commented:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
 on this
For Each rw As DataGridViewRow In DataGridView1.Rows
AndyAinscowFreelance programmer / Consultant

Commented:
>>Index was out of range....

That message doesn't make sense with the line you say is generating the error.

Author

Commented:
When i check for datagridview1.rows.count it shows zero
AndyAinscowFreelance programmer / Consultant

Commented:
>>When i check for datagridview1.rows.count it shows zero

How on earth is it actually reaching the line giving the error then?
   
            If DataGridView1.Rows.Count > 0 Then
                    'Commented out for messsage queue load on the server
                   For Each rw As DataGridViewRow In DataGridView1.Rows

Author

Commented:
I have no clue Andy,it is what  it is
AndyAinscowFreelance programmer / Consultant

Commented:
Hmmmm.
Reboot your PC then check if it is still giving problems

Author

Commented:
Andy,its a windows service.
The code is written on timer_click event.
When the timer _click is fired ..it leaves the loop and the cursor gets on Timer_Click ...
AndyAinscowFreelance programmer / Consultant

Commented:
Please try rebooting the PC.  It is surprising just how many strange errors get cured by such a simple measure.

Author

Commented:
OK..its a server cant  restart it , will have to wait till weekend
AndyAinscowFreelance programmer / Consultant

Commented:
OK.


ps.  I know you have a good reason but why have you got a windows service interacting with a GUI ?

Author

Commented:
I am debugging

Author

Commented:
Also I need to know how I can make the timer_click wait till the looping of datagridview completes rather than just jumping on the timer_click event and leaving the loop
AndyAinscowFreelance programmer / Consultant

Commented:
I have a horrible feeling you are creating problems which won't exist in the real version.

Author

Commented:
Why?
Freelance programmer / Consultant
Commented:
Because, if I understand you correctly, you have added code just for debugging and it is this code that is giving the error.

Author

Commented:
I have not added code ,its already there I am debugging it

Author

Commented:
I've requested that this question be deleted for the following reason:

Neglected question.Please delete it
AndyAinscowFreelance programmer / Consultant

Commented:
I haven't neglected this.  You said yourself it would have to wait for the weekend to enable you to try a reboot.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial