Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

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

Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
AndyAinscow
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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).
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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

Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Is there any check needed on the code?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

With the exact error message you show in the question body?
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>Index was out of range....

That message doesn't make sense with the line you say is generating the error.
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

When i check for datagridview1.rows.count it shows zero
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>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
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I have no clue Andy,it is what  it is
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Hmmmm.
Reboot your PC then check if it is still giving problems
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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 ...
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Please try rebooting the PC.  It is surprising just how many strange errors get cured by such a simple measure.
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

OK..its a server cant  restart it , will have to wait till weekend
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

OK.


ps.  I know you have a good reason but why have you got a windows service interacting with a GUI ?
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I am debugging
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

I have a horrible feeling you are creating problems which won't exist in the real version.
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Why?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I have not added code ,its already there I am debugging it
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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

Neglected question.Please delete it
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo