Solved

VB.NET Datagridview Index out of range error

Posted on 2009-05-12
9
710 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

19 Experts available now in Live!

Get 1:1 Help Now