Question

I am populating a Dataset from mssql, the problem I am having is that when I go to check the Table count and the Row Count to see if it is empty they both return a value Greater then 0 which should mean they are not empty but when I try to access them they are empty.. and they should be empty because to test this I emptied the table I am hitting.

Here is My code

Dim da As OdbcDataAdapter
Dim dsProNum As New DataSet
ssql = "SELECT * FROM ProNumberEXP WHERE BillTo = ? and ShipTo = ?"
    Try
        da = New OdbcDataAdapter(ssql, Me.connString)
        da.SelectCommand.Parameters.Add("@BillTo", OdbcType.VarChar).Value = BillTo.Trim
        da.SelectCommand.Parameters.Add("@ShipTo", OdbcType.VarChar).Value = ShipTo.Trim
        da.Fill(dsProNum)

    Catch ex As Exception
        dsProNum = Nothing
        return False
    End Try
    If dsProNum.Tables(0).Rows.Count = 0 Then
        Found = False
    Else
        Found = True
    End If
    dsProNum = Nothing

My If statement, If dsProNum.Tables(0).Rows.Count = 0 Then always returns false regardless of what is in the table I am pulling from.

can anyone help me out with this?

Was it helpful?

Solution

Without the code that creates the DataSet I can only guess that you created the DataSet with a DataTable before filling it or that you used a designer to create it, in which case it already has a table.

Instead of checking whether the DataSet has any tables, you should check the return value of DbDataAdapter.Fill. Fill returns the number of rows actually added or updated in the dataset.

You should also consider using SqlDataAdapter instead of OdbcDataAdapter as it is much faster and supports more SQL Server features.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top