Solved

Datagridview error

Posted on 2016-09-06
23
21 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Is there any check needed on the code?
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
With the exact error message you show in the question body?
0
 

Author Comment

by:RIAS
Comment Utility
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
Comment Utility
>>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
Comment Utility
When i check for datagridview1.rows.count it shows zero
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>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
Comment Utility
I have no clue Andy,it is what  it is
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Hmmmm.
Reboot your PC then check if it is still giving problems
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:RIAS
Comment Utility
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
Comment Utility
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
Comment Utility
OK..its a server cant  restart it , will have to wait till weekend
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
I am debugging
0
 

Author Comment

by:RIAS
Comment Utility
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
Comment Utility
I have a horrible feeling you are creating problems which won't exist in the real version.
0
 

Author Comment

by:RIAS
Comment Utility
Why?
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
Comment Utility
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
Comment Utility
I have not added code ,its already there I am debugging it
0
 

Author Comment

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

Neglected question.Please delete it
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

8 Experts available now in Live!

Get 1:1 Help Now