Solved

VB.NET Datagridview Index out of range error

Posted on 2009-05-12
9
713 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word Template Mail merge with vb.net 4 59
help with issues with ReportViewer in VS2015 5 38
Round up to 100% in .NET 10 87
Help with sorting data in Listbox using VB.NET 3 53
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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

896 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

14 Experts available now in Live!

Get 1:1 Help Now