-
Dec 6th, 2013, 12:16 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Test for empty dataset
I've been unable to figure out how to do this although it ought to be really simple. I'm using VB 2012.
Code:
Dim connection As SqlConnection = clsConnection.GetConnection()
Dim DataAdapter As SqlDataAdapter = New SqlDataAdapter(SQL, connection)
Dim Data As DataSet = New DataSet
DataAdapter.Fill(Data, Grid)
Grid.DataSource = Data
This is the basic reusable code which works fine. However, I need to test for an empty dataset (empty grid) at this point in the code and I can not seem to figure out how to do that. I've tried If (Data.Container is Nothing) then ... but that always is true. I'm not understanding something here ... could someone kindly set me on the right track?
TIA,
Ken
-
Dec 6th, 2013, 01:24 PM
#2
Re: Test for empty dataset
Code:
If (Data Is Nothing) Then
' handle null dataset here.
Else
Grid.DataSource = Data
End If
or if checking a specific table:
Code:
If (Data.Tables(0) Is Nothing) Then
' handle null dataset here.
Else
Grid.DataSource = Data.Tables(0)
End If
-
Dec 6th, 2013, 01:41 PM
#3
Re: Test for empty dataset
You can use "grid.rows.count=0" to check if there is any data. Also, there is no need to use a dataset if your only using one DataTable. A Datset is for using Multiple tables.
-
Dec 6th, 2013, 01:41 PM
#4
Thread Starter
Addicted Member
Re: Test for empty dataset
Thanks for the reply, KG. With either approach, the null dataset apparently is never true - although the grid is empty. I tried both ways and the code always thought the dataset was populated so it never triggered the null event.
Perhaps if I were to test a column in the grid after it's loaded .... ??? I tried doing that but couldn't figure out the syntax.
If (Grid.ActiveRow.Cells("TaskID") Is Nothing) then .... but I get a null reference exception was unhandled for some reason ...
-
Dec 6th, 2013, 01:42 PM
#5
Re: Test for empty dataset
We posted at the same time, check post 3
-
Dec 6th, 2013, 01:47 PM
#6
Thread Starter
Addicted Member
Re: Test for empty dataset
That worked a charm wes4dbt! Thank you! I figured it was some pretty simple syntax that I just couldn't quite get hold of. THANK YOU!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|