Datagridview error

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

RIASAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
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).
0
RIASAuthor 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

0
RIASAuthor 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
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

RIASAuthor Commented:
Is there any check needed on the code?
0
AndyAinscowFreelance programmer / ConsultantCommented:
With the exact error message you show in the question body?
0
RIASAuthor 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
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>Index was out of range....

That message doesn't make sense with the line you say is generating the error.
0
RIASAuthor Commented:
When i check for datagridview1.rows.count it shows zero
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>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
0
RIASAuthor Commented:
I have no clue Andy,it is what  it is
0
AndyAinscowFreelance programmer / ConsultantCommented:
Hmmmm.
Reboot your PC then check if it is still giving problems
0
RIASAuthor 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 ...
0
AndyAinscowFreelance programmer / ConsultantCommented:
Please try rebooting the PC.  It is surprising just how many strange errors get cured by such a simple measure.
0
RIASAuthor Commented:
OK..its a server cant  restart it , will have to wait till weekend
0
AndyAinscowFreelance programmer / ConsultantCommented:
OK.


ps.  I know you have a good reason but why have you got a windows service interacting with a GUI ?
0
RIASAuthor Commented:
I am debugging
0
RIASAuthor 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
0
AndyAinscowFreelance programmer / ConsultantCommented:
I have a horrible feeling you are creating problems which won't exist in the real version.
0
RIASAuthor Commented:
Why?
0
AndyAinscowFreelance programmer / ConsultantCommented:
Because, if I understand you correctly, you have added code just for debugging and it is this code that is giving the error.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RIASAuthor Commented:
I have not added code ,its already there I am debugging it
0
RIASAuthor Commented:
I've requested that this question be deleted for the following reason:

Neglected question.Please delete it
0
AndyAinscowFreelance programmer / ConsultantCommented:
I haven't neglected this.  You said yourself it would have to wait for the weekend to enable you to try a reboot.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.