Solved

VB.NET Datagridview Index out of range error

Posted on 2009-05-12
9
732 Views
Last Modified: 2012-05-06
This may be a very simple fix, but I can't seem to figure this out.  I have a query that pull data from a sql database and display it into a datagrid.  When it does this, I also want to fill a textbox (txtTime.text) with the first row ("TIME_ID") from the datagrid.  However, the query pull empty data, I get the index out of range error.

How would I set my syntax so that if datagrid is empty, exit the sub/function?  I tried the following but no luck and still getting index out of range.


If datagridview1.Rows(0).Cells("TIME_ID").Value = "" Then
       EXIT SUB
else
       txtTime.text = datagridview1.Rows(0).Cells("TIME_ID").Value
End If

Open in new window

0
Comment
Question by:holemania
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 3

Expert Comment

by:mpeaden2
ID: 24366471
What data type is time_id?

0
 
LVL 3

Expert Comment

by:mpeaden2
ID: 24366492
Just change your query to 'and not isnull('time_id')'
0
 
LVL 2

Expert Comment

by:krishrr
ID: 24366493
Check for NULL instead of ""
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:holemania
ID: 24366563
TIME_ID is an int data type.  However, I don't think that's the issue.  I am trying to pull TIME_ID from a data grid view into a textbox.  Since the datagrid has nothing in it, I am getting index out of range error.  

Can I code so that if there's nothing in the datagrid, just to exit the function like the example I provided?  I tried that, but still error out at that part.  If I fill the table with data, and the query fill the datagridview, then it is fine and I don't get this error.  So only time I get an error is if it does not pull data into the datagridview.
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24366917
Or else check in this way

If gridView1.Rows.Count > 0 then
txtTime.text = datagridview1.Rows(0).Cells("TIME_ID").Value
Else
Exit Sub
End If
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24366930
You can also use:
        If Me.DataGridView1.RowCount > 0 Then
            ' your code
        End If

Open in new window

0
 
LVL 15

Accepted Solution

by:
JackOfPH earned 500 total points
ID: 24370722
Try the below code...
This will check if the row count in datagridview is zero, If it is zero the code will exit...
If datagridview1.Rows.Count = 0 then
      Exit sub
end if
 
If datagridview1.Rows(0).Cells("TIME_ID").Value = "" Then
       EXIT SUB
else
       txtTime.text = datagridview1.Rows(0).Cells("TIME_ID").Value
End If

Open in new window

0
 

Author Comment

by:holemania
ID: 24391400
Thanks for all the suggestion.  I am currently out of the office traveling and won't be able to test until next week.  I will look again and update, and distribute points accordingly.
0
 

Author Closing Comment

by:holemania
ID: 31580608
Thank you.  The example you provided work great.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

718 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