Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to check if DataTable has row

I have a function that search for a keyword and then return DataTable. I want to check if there rows inside because sometimes there's no query found.

    'which one shoud I use ?
    If dtDataTable Is Nothing Then
        'some code
        lbl_count.Text = "Found 0 result"
    End If

    If dtDataTable.Rows.Count > 0 Then
        'some code
        lbl_count.Text = "Found " & dtDataTable.Rows.Count.ToString & " results"
    End If

Thanks.

like image 796
Tola Avatar asked May 12 '09 03:05

Tola


People also ask

How do you check if a row exists in a Datatable C#?

If the count is greater than zero then the row already exists in the Datatable else the datarow is not present in the datatable.

How do you check if a string exists in a Datatable Uipath?

Hi @Ravi, to check whether a string is present in a data table or not, you need to use ReadRange activity to read your excel file and store its output in a data table. Then use ForEachRow activity to read each row of the data table and use If activity to put the condition for matching the string.


2 Answers

How about:

If dtDataTable IsNot Nothing AndAlso dtDataTable.Rows.Count > 0 Then
    'some code
    lbl_count.Text = "Found " & dtDataTable.Rows.Count.ToString & " results"
Else
    'some code
    lbl_count.Text = "Found 0 result"
End If
like image 112
Steve Echols Avatar answered Oct 01 '22 15:10

Steve Echols


If you're using VB 9.0 (VS 2008) you can simply this with the following

lbl_count.Text = String.Format("Found {0} result(s)", if(dbDataTable, dbDataTable.Rows.Count,0))
like image 28
JaredPar Avatar answered Oct 01 '22 15:10

JaredPar