Results 1 to 6 of 6

Thread: [RESOLVED] Test for empty dataset

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Resolved [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

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    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
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

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

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Test for empty dataset

    We posted at the same time, check post 3

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    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
  •  



Click Here to Expand Forum to Full Width