-1

I have a function that returns a single data row from a data table based on a certain value in a certain column. what if the value doesnt produce a row, how can i determine that by using the empty row?

ie, if i had a query that returned a table that had 0 rows, I could say

         if(table.rows.count == 0)  
          { 
            do stuff
          }

but i cannot do

        if(row.columns.count)
         {
           do stuff
         }

ive tried dbnull and row.haserrors but thats not what i am looking for. i want to test the row because i need to use it if it does have values

I pass a datatable to the function. then loop through to find a specific value on a specific field. if i find it, i assign that row and return it. otherwise it just returns a row

so I got Dim myRow as DataRow

Loop if value found, MyRow = thisRow Next

return myRow

(sorry it is vb)

dwarf
  • 445
  • 2
  • 9
  • 23
  • But how are you *assigning* `row` here? – Mike Perrenoud Oct 09 '13 at 18:57
  • How are you getting the data? If you use a DataReader, determining whether there are any rows returned is pretty easy. – Mike Dinescu Oct 09 '13 at 18:57
  • what is wrong with your first condition? `if (table.Rows.Count == 0)` ? – Khan Oct 09 '13 at 19:00
  • You do realize that you're not comparing `columns.count` to anything, right? Maybe an `== 0` or `> 0` would help. Are you getting any exceptions? – Adam Plocher Oct 09 '13 at 19:01
  • sorry, im just typing this rather than showing my code, i understand your concern about the column count, but that doesnt even exist – dwarf Oct 09 '13 at 19:03
  • Instead of a for loop I would suggest `DataTable.Select` http://msdn.microsoft.com/en-us/library/det4aw50.aspx which "Gets an array of all DataRow objects that match the filter criteria". This returns an array of DataRow[] objects which will have a Length property you can check – Harrison Oct 09 '13 at 19:06

1 Answers1

0

Check if DataRow is null, if not then loop through the ItemArray and see if any of the columns have data, like this:

// Assume it is empty
bool isRowEmpty = true;

if (row != null)
{
    foreach(var columnValue in row.ItemArray)
    {
        if (columnValue != null)
        {
            isRowEmpty = false;
            break;
        }
    }
}

Here is the VB.NET version:

' Assume it is empty
Dim isRowEmpty As Boolean = True

If row IsNot Nothing Then
    For Each columnValue As var In row.ItemArray
        If columnValue IsNot Nothing Then
            isRowEmpty = False
            Exit For
        End If
    Next
End If
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • when i try to see if the row is null, intellisense dont like it. only lets me compare it to nullable,even then it gives an error – dwarf Oct 09 '13 at 19:04
  • Is `row` a `DataRow` or something else? – Karl Anderson Oct 09 '13 at 19:06
  • Using similar logic as @Karl Anderson, you could return a row index of the row that is found. If a match is not found, return -1. – Rick Davin Oct 09 '13 at 19:08
  • i cant return an int or a bool, i need to return a row. but nonetheless, im gonna see if IsDbNull(myRow) works, if not I will try the row.table.count. howeever karl's vb version looks like it should work. it is a datarow. im not the best with vb, this is nothing is odd – dwarf Oct 09 '13 at 19:11
  • @user2070677 - it would help if you posted your actual code instead of the pseudo-code, including the function that returns the `DataRow` object. – Karl Anderson Oct 09 '13 at 19:13