How can I check for Null dataset

I wanted to check for an empty dataset which is being returned from a datagrid. How can I do that

Dim myDataSet As DataSet
' this code returns the dataset of datagrid dgmonth.
myDataSet = CType(dgmonth.DataSource, DataSet)


so I wanted to write  
if myDataSet = nothing then
     exit sub
end if

but this if..then loop gives an error. I will appreciate any help. Thanks






       


Answer this question

How can I check for Null dataset

  • Jerrycee11

    After populating your dataset you may use the option below(among several others):

    Try
          If myDataset.Tables(0).Rows.Count = 0 Then
              messagebox.show("Dataset Empty")
          Else
              messagebox.show("Dataset Not Empty")
          End If
    Catch
          'handle error here
    End Try

    This assumes there's a single table in your myDataSet. You can specify the table name if there are more. E.g

    If myDataset.Tables("TableName").Rows.Count = 0 Then

    Else

    End If

  • zatom idham

    On a related note, How can I check for a null value in my dataset. I want to replace the null value with a space (" ").

    Jody

  • Sanjoy Debnath

    Thanks so much

  • sboots

    DBNull.Value
  • Jaqq

    Yes, that is very helpful, thank you.  I will work on that this evening.
  • ImCampos

    this is asp.net  -

    but because you are databinding, I'll let it slide.

    You might want to try <a href="http://www.asp.net">www.asp.net</a> for more response

  • NemosNemos

    I am trying to accomplish the same goal and tried what was suggested above but can't seem to get it to work.  Where would this "If Then" statement go in my code

    Dim strSQL as String = "SELECT * FROM database"
    Dim objAdapter as new OledbDataAdapter(strSQL, objConnection)
    Dim objDataSet as new DataSet()
    objAdapter.Fill(objDataSet, "name")
    Dim objDataView as new DataView(objDataSet.Tables("Balisong"))
    dgbalisong.DataSource = objDataView
    dgbalisong.DataBind()

    I am somewhat new to ASP.NET and I appreciate any help anyone can provide.

  • Cortex

    I am doing something a little bit different now but I am stuck again.  I should first point out that I am not an expert nor do I have any formal education in ASP.NET.  This is a personal website I am building.  My last site was classic ASP and now I am rebuilding it completely using books, Google and the help of experts like yourselves.

    I have an Access database with items from a collection stored in it.  Each item has a "status" field which I assign one of four classifications.  The datagrid displays the items in a specific catagory based on a drop down list selection. Sometimes there won't be anything in a specific catagory like "For Sale" and I don't want to show a datagrid header with nothing in the datagrid (this is what my page is doing now).  I am coding everything by hand; I don't use VS.NET.  Last time around, I had the aspx page and a code behind page.  This time around, I put all the database code in a namespace, compiled it and my code behind page handles only the events.

    Here is my function which is part of a namespace that I have compiled:

    Public Function GetBalisongList(strStatus as string, strSort as string) as Dataset
        If strStatus = "complete" Then
            strSQL = "SELECT BalisongID, make, model, status FROM balisong ORDER BY " & strSort
        Else
            strSQL = "SELECT BalisongID, make, model, status FROM balisong WHERE status = '" & strStatus & "' ORDER BY " & strSort
        End If
        Dim objConnection as New OLEDBConnection(m_DSN)
        Dim objDataAdapter as New OLEDBDataAdapter(strSQL, objConnection)
        Dim BalisongList as new Dataset()
        objDataAdapter.Fill(BalisongList)
        Return BalisongList
    End Function

    Here is the sub that handles the drop down list in my code behind page:

    Public Sub balisongfilter(sender as object, e as eventargs)
        ViewState("status") = ddl_balisong.SelectedItem.Value
        If ViewState("status") = "description" Or ViewState("status") = "" Then
            lblinstructions.visible = true
            dgBalisongList.visible = false
            rptBalisongDetails.visible = false
        Else
            strSort = "make, model"
            Dim BalisongStatusList as new Balisong(strconnect)
            dgBalisongList.DataSource = BalisongStatusList.GetBalisongList(ViewState("status"), strSort)
            dgBalisongList.DataBind()
            lblinstructions.visible = false
            dgbalisonglist.visible = true
            rptBalisongDetails.visible = false
        End If
    End Sub


    If there are not any items with a specific status then I want to display an asp:label saying something like, "I do not currently have any balisongs for sale." instead of an empty datagrid.  Would I check for an empty dataset in my fuction or in the event handling sub   What is the best practice for doing this  Thank you in advance for any help you can provide.

  • Bjorn Coltof

    Try

    If myDataSet Is Nothing then
    Exit Sub
    End If

    Don't ask me why but if I use IS as opposed to =, I get my result...

    Byrd Man

  • shadownnc

    Hi as u  have creatdd new instance of Dataset that means it can not be nothing.......so u have to check for either u r datatable or Row count in Datatable as per ur Requirements......

    Dim strSQL as String = "SELECT * FROM database" 
    Dim objAdapter as new OledbDataAdapter(strSQL, objConnection) 
    Dim objDataSet as new DataSet() 
    objAdapter.Fill(objDataSet, "name") 

    Case 1:
    if  objDataSet.Tables("Balisong") is nothing then
      Exit sub
    Else
    Dim objDataView as new DataView(objDataSet.Tables("Balisong")) 
    dgbalisong.DataSource = objDataView 
    dgbalisong.DataBind() 

    End if


    Case 2

    if  objDataSet.Tables("Balisong").rows.count <=0 then
      Exit sub
    Else
    Dim objDataView as new DataView(objDataSet.Tables("Balisong")) 
    dgbalisong.DataSource = objDataView 
    dgbalisong.DataBind() 

    End if

    hope this will help u.....................

    Regards,
    Ritesh

  • How can I check for Null dataset