none
how to check if a dataset object is null in vb.net RRS feed

  • Question

  • Hi, quick question: (apologies if its in the wrong thread)

    what is the vb.net equivalent of this c# line of code

    if (if dataset1 == null)
    {
    // do what ever
    }
    Friday, May 8, 2009 11:53 AM

Answers

  • Hi, yes I don't think the NULL is limited to datasets only :-)
    But I guess this may work for you:

    C#
                DataSet ds = new DataSet();
    
                ds = null;
    
                if (ds == null)
    
                {
    
                    Console.WriteLine("Null");
    
                }

    VB

            Dim dataSet As DataSet = New DataSet
    
            dataSet = Nothing
    
            If (dataSet Is Nothing) Then
    
                Console.WriteLine("Null/Nothing")
    
            End If

    HTH
    //Michael
    This posting is provided "AS IS" with no warranties.
    Friday, May 8, 2009 12:25 PM

All replies

  • Hi, yes I don't think the NULL is limited to datasets only :-)
    But I guess this may work for you:

    C#
                DataSet ds = new DataSet();
    
                ds = null;
    
                if (ds == null)
    
                {
    
                    Console.WriteLine("Null");
    
                }

    VB

            Dim dataSet As DataSet = New DataSet
    
            dataSet = Nothing
    
            If (dataSet Is Nothing) Then
    
                Console.WriteLine("Null/Nothing")
    
            End If

    HTH
    //Michael
    This posting is provided "AS IS" with no warranties.
    Friday, May 8, 2009 12:25 PM
  • Thanks.
    Friday, May 8, 2009 1:29 PM
  • Hi could you give me some further assistance.

    I want to iterate through the tables in my dataset and write the rows and columns that are present in the dataset to a string:

     

    For Each table In ds1.Tables
    Dim intRow As Integer
    Dim intCol As Integer

     

    For intRow = 0 To table.Rows.Count
    For intCol = 0 To table.Columns.Count
    strVal = table.Rows(intRow)(intCol).ToString()

     

    Next
    Next
    Next table

    However with the above code I only get the values of the last of the last iteration of the loop. Am I approching this the wrong way? Would would you suggest I do? My dataset can contain up to 5 tables

    Thanks

    Friday, May 8, 2009 6:52 PM
  • Hi could you give me some further assistance.

    I want to iterate through the tables in my dataset and write the rows and columns that are present in the dataset to a string:

    For Each table In ds1.Tables
    Dim intRow As Integer
    Dim intCol As Integer

    For intRow = 0 To table.Rows.Count
    For intCol = 0 To table.Columns.Count
    strVal = table.Rows(intRow)(intCol).ToString()

    Next
    Next
    Next table

    However with the above code I only get the values of the last of the last iteration of the loop. Am I approching this the wrong way? Would would you suggest I do? My dataset can contain up to 5 tables

    Thanks

    Saturday, May 9, 2009 7:35 AM
  • This is because inside of the loop each time you assign new value to your strVal variable instead of concatenating values (if this is what you are looking for). You code should look like

    For Each table In ds1.Tables
    Dim intRow As Integer
    Dim intCol As Integer

    For intRow = 0 To table.Rows.Count
    For intCol = 0 To table.Columns.Count
    strVal = strVal & table.Rows(intRow)(intCol).ToString()

    Next
    Next
    Next table

    Another way is to use StringBuilder instead of concatenation or write values directly to file, depending on what you need to achieve


    Val Mazur (MVP) http://www.xporttools.net
    Tuesday, May 12, 2009 10:01 AM
    Moderator