Empty table in a dataset

  • Thread starter Thread starter pmelanso
  • Start date Start date
P

pmelanso

Hello,
What would be the statement to see if a table in a dataset has no rows
( i.e. is empty)??? What I need this for is if an sql statement doesn't
return anything... I need to know when that happends.

Pam
 
Hello,
What would be the statement to see if a table in a dataset has no rows
( i.e. is empty)??? What I need this for is if an sql statement doesn't
return anything... I need to know when that happends.

Pam

if not DataSet.Tables(0) is nothing then
DataSet.Tables(0).Rows.Count
end if
 
Actually, I would expect that code to throw an index out of bounds
exception.

It should be something like:

If myDS.Tables.Count = 0 Then
' there are no tables
End If
 
Marina said:
Actually, I would expect that code to throw an index out of bounds
exception.

It should be something like:

If myDS.Tables.Count = 0 Then
' there are no tables
End If

You are right... I was thinking but didn't type along the lines of:

if not MyDS.Tables("TableName") is nothing then
....
end if

because if the ds has two tables in it your way isn't valid for his needs
 
'note the test can be done in one line of code like in the commented out line
'below
'If mydataset.tables("TableName").Rows.Count < 1 Then MsgBox("Zero datarows
exist", , "No Data")

'declare objects outside of the block so you can dispose of them in
finally
Dim tbl As DataTable
Dim ds As DataSet
Dim iRows As Integer
Try
'set objects inside of try to trap exceptions
ds = New DataSet("ds")
tbl = New DataTable("t")
tbl.Columns.Add("ID")
ds.Tables.Add(tbl)
'returns 0(zero) if there are no datarows in the table referenced
iRows = ds.Tables("t").Rows.Count

MsgBox(iRows, , "Number of datarows: iRows")
'test the value of iRow
If iRows > 0 Then
'No datarows present
Else
'at least one datarow present
End If

Catch ex As Exception
MsgBox(ex.Message.ToString, , "System Exception")
Finally
tbl.Dispose() : ds.Dispose()
End Try
 
'note the test can be done in one line of code like in the commented out line
'below
'If mydataset.tables("TableName").Rows.Count < 1 Then MsgBox("Zero datarows
exist", , "No Data").

'declare objects outside of the block so you can dispose of them in
finally
Dim tbl As DataTable
Dim ds As DataSet
Dim iRows As Integer
Try
'set objects inside of try to trap exceptions
ds = New DataSet("ds")
tbl = New DataTable("t")
tbl.Columns.Add("ID")
ds.Tables.Add(tbl)
'returns 0(zero) if there are no datarows in the table referenced
iRows = ds.Tables("t").Rows.Count

MsgBox(iRows, , "Number of datarows: iRows")
'test the value of iRow
If iRows > 0 Then
'No datarows present
Else
'at least one datarow present
End If

Catch ex As Exception
MsgBox(ex.Message.ToString, , "System Exception")
Finally
tbl.Dispose() : ds.Dispose()
End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top