Solved

VB.NET Datagridview Index out of range error

Posted on 2009-05-12
9
721 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
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

792 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