Solved

Datagridview error

Posted on 2016-09-06
23
22 Views
Last Modified: 2016-09-13
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

0
Comment
Question by:RIAS
  • 13
  • 10
23 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41786136
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
 

Author Comment

by:RIAS
ID: 41786143
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
 

Author Comment

by:RIAS
ID: 41786183
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
 

Author Comment

by:RIAS
ID: 41786184
Is there any check needed on the code?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41786185
With the exact error message you show in the question body?
0
 

Author Comment

by:RIAS
ID: 41786188
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41786199
>>Index was out of range....

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

Author Comment

by:RIAS
ID: 41786201
When i check for datagridview1.rows.count it shows zero
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41786206
>>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
 

Author Comment

by:RIAS
ID: 41786207
I have no clue Andy,it is what  it is
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41786217
Hmmmm.
Reboot your PC then check if it is still giving problems
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:RIAS
ID: 41786222
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
 
LVL 44

Expert Comment

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

Author Comment

by:RIAS
ID: 41786249
OK..its a server cant  restart it , will have to wait till weekend
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41786261
OK.


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

Author Comment

by:RIAS
ID: 41786276
I am debugging
0
 

Author Comment

by:RIAS
ID: 41786281
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41786292
I have a horrible feeling you are creating problems which won't exist in the real version.
0
 

Author Comment

by:RIAS
ID: 41787065
Why?
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 41787228
Because, if I understand you correctly, you have added code just for debugging and it is this code that is giving the error.
0
 

Author Comment

by:RIAS
ID: 41787356
I have not added code ,its already there I am debugging it
0
 

Author Comment

by:RIAS
ID: 41790880
I've requested that this question be deleted for the following reason:

Neglected question.Please delete it
0
 
LVL 44

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DataGridView does not show data rows correctly in vb.net 2013 3 29
SQL syntax in VB.net 5 27
Connection String 16 42
Help with LINQ and XML 10 25
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now