954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

check if datatable is empty

I can loop while the datatable is full, but get an error when it is empty. How do I know it's empty.

Robert Walker
Newbie Poster
12 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

What is the error you are getting?

You could do a test for the number of records and only loop if it is greater than 0.

And how are you looping through the table?

Pseudo-Code:

Dim dr As OleDbDataReader = cmd.ExecuteReader()

Do While dr.Read
...
...
Loop


Hope this helps

Paladine
Master Poster
Team Colleague
824 posts since Feb 2003
Reputation Points: 211
Solved Threads: 27
 

Dim myConnStr As String = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + myPath
Dim myConn As New OleDb.OleDbConnection(myConnStr)
Dim myAdapt As New OleDb.OleDbDataAdapter(myQuery, myConn)

Try
myConn.Open()
Dim myReader As New DataSet
myAdapt.Fill(myReader)

**
** I need to check if myReader is empty
**

tbGATE.Text = myReader.Rows(0)("dGate")
tbCONC.Text = myReader.Rows(0)("dConc")
tbPAY.Text = myReader.Rows(0)("dPayment")

**
** I need to check if myReader.Rows(0)("dWeather") is empty
**

cbWEATHER.DisplayMember = myReader.Rows(0)("dWeather")

myConn.Close()

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Robert Walker
Newbie Poster
12 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

(For sake of confusion, I would not name my Dataset as myReader - seems to refer to a datareader to someone not knowing what you are coding for.... just some programming edicate I thought I should pass on)

Saying that......

Add the line

If myReader.Tables("dWeather").Rows.Count > 0 Then

Hope this helps

Dim myConnStr As String = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + myPath Dim myConn As New OleDb.OleDbConnection(myConnStr) Dim myAdapt As New OleDb.OleDbDataAdapter(myQuery, myConn)

Try myConn.Open() Dim myReader As New DataSet myAdapt.Fill(myReader)

** ** I need to check if myReader is empty **

tbGATE.Text = myReader.Rows(0)("dGate") tbCONC.Text = myReader.Rows(0)("dConc") tbPAY.Text = myReader.Rows(0)("dPayment")

** ** I need to check if myReader.Rows(0)("dWeather") is empty **

cbWEATHER.DisplayMember = myReader.Rows(0)("dWeather") myConn.Close()

Catch ex As Exception MessageBox.Show(ex.Message) End Try

Paladine
Master Poster
Team Colleague
824 posts since Feb 2003
Reputation Points: 211
Solved Threads: 27
 

Also might do well to ensure the object itself is not Nothing.
If a SQL call fails, the dataset/table object can remain uninitialized, in which case myReader.Rows will throw an error.

ESHbyESH
Newbie Poster
6 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

ds.tables(0).rows.count

janet.

janet_ss
Newbie Poster
4 posts since Apr 2008
Reputation Points: 4
Solved Threads: 0
 

Really Janet.... A Thread From 2005?

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Better late than never :D

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

Try this

If Not dataset1 Is Nothing Then
.....
... Process dataset
.....
End If

keith_mas
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

You probably can guess this from the previous threads but I use:

dim MyTable as Datatable
'fill datatable....
If (MyTable Is Nothing) Or (MyTable.Rows.Count =0 ) Then
'it's empty
Else
'it exists and there are rows 
End if
G_Waddell
Posting Whiz in Training
255 posts since Nov 2009
Reputation Points: 43
Solved Threads: 27
 

last one works....thanks man...

black_sun191
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You