Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is there a cleaner way of checking a dataset for tables/rows before attempting to read them?

If ds.Tables.Count > 0 Then
    If ds.Tables(0).Rows.Count > 0 Then

        'do something with the the rows at this point

    End If
End If

Edit to clarify - I am talking specifically about the two bool checks to see if the dataset acutally contains any data in table(0).

share|improve this question
1  
Please define: Check? Do you want to check for the existence of columns or that a value was returned? – Bobby Dec 15 '11 at 10:12

3 Answers

up vote 1 down vote accepted

Assuming that you are using a version of .NET that supports extension methods you could write an extension which does the basic check.

VB.NET

Public NotInheritable Class DataSetExtensions
    Private Sub New()
    End Sub
    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function IsTableDataPopulated(dataSet As DataSet) As Boolean
        Return dataSet IsNot Nothing AndAlso dataSet.Tables.Count > 0 AndAlso dataSet.Tables(0).Rows.Count > 0
    End Function
End Class

c#

public static class DataSetExtensions
{
    public static bool IsTableDataPopulated(this DataSet dataSet)
    {
        return dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0;
    }
}

Usage

if (ds.IsTableDataPopulated()) 
{
   // do stuff
}

Note: I am not 100% sure if the VB code works as I used a translator for converting c# to VB.NET, call me lazy :)

share|improve this answer
I like it. Thanks! – asawyer Jan 3 '12 at 21:01

I'm doing a couple of assumptions here, but I hope I'm on target.

If you just want the first column of the first row, you should use SqlCommand.ExecuteScalar instead of reading it into a dataset.

For instance:

using (var cn = new SqlConnection("..."))
{
    using (var cmd = new SqlCommand("SELECT statusCode FROM table WHERE whatever", cn)
    {
        cn.Open();
        return (string)cmd.ExecuteScalar();
    }
}

(Notice the using statement if you don't know it. It automatically disposes the connection and command)

You could also look into using SqlCommand.ExecuteReader which returns an SqlDataReader. The latter is cleaner and has better performance than the dataset method too.

using (var reader = cmd.ExecuteReader())
{
    while(reader.Read()) 
    { 
        var value = reader.GetString(0); 
    }
}
share|improve this answer
1  
The reader needs to be disposed, too. – Bobby Dec 15 '11 at 10:12
fixed, thanks. :) – Lars-Erik Dec 15 '11 at 12:45

This looks like a good opportunity to use AndAlso.

From the MSDN documentation (http://msdn.microsoft.com/en-us/library/cb8x3kfz(v=vs.80).aspx):

One use of the AndAlso operator is to test for the existence of an object instance before attempting to access one of its members.

In your case:

If ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then

(Which is basically what Kane said, without the funky extension methoding - now that I re-read his answer)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.