简体   繁体   中英

Check if VB.net dataset table exists

I have the following code to check if my table exists before proceeding

        If ds.Tables(3).Rows.Count = 0 Then
            MsgBox("Nothing!!!!")
        Else
            DataGridView1.DataSource = ds.Tables(3)

The trouble is I keep getting the error "Cannot find table 3."

How in VB can I check if the table exists, rather than my application erroring I just want it to do nothing if the table doesn't exist.

I have also tried

If ds is nothing

Any help greatly appreciated.

If you don't know if the DataSet is initialized:

If ds IsNot Nothing Then
    ' ... '
End If

If you don't know if it contains four tables(zero based indices):

If ds.Tables.Count >= 4 Then
        ' ... '
End If

So the final super safe version is:

If ds IsNot Nothing AndAlso ds.Tables.Count >= 4 Then
    Dim table As DataTable = ds.Tables(3)
End If

If you now also want to know if that table contains rows:

Dim isEmpty As Boolean = table.Rows.Count = 0

See if the dataset contains the table if you are unsure if it exists:

If mdsMyDataSet1.Tables.Contains("Table3") = True Then
   'Do Something with it
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM