?
Solved

VB.NET Datagridview Index out of range error

Posted on 2009-05-12
9
Medium Priority
?
734 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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