943,749 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 45949
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
May 17th, 2005
0

check if datatable is empty

Expand Post »
I can loop while the datatable is full, but get an error when it is empty. How do I know it's empty.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Robert Walker is offline Offline
12 posts
since May 2005
May 17th, 2005
0

Re: check if datatable is empty

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:
VB.NET Syntax (Toggle Plain Text)
  1. Dim dr As OleDbDataReader = cmd.ExecuteReader()
  2.  
  3. Do While dr.Read
  4. ...
  5. ...
  6. Loop

Hope this helps
Team Colleague
Reputation Points: 211
Solved Threads: 27
Master Poster
Paladine is offline Offline
793 posts
since Feb 2003
May 18th, 2005
0

Re: check if datatable is empty

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Robert Walker is offline Offline
12 posts
since May 2005
May 18th, 2005
0

Re: check if datatable is empty

(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

Quote originally posted by Robert Walker ...
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
Team Colleague
Reputation Points: 211
Solved Threads: 27
Master Poster
Paladine is offline Offline
793 posts
since Feb 2003
May 18th, 2005
0

Re: check if datatable is empty

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ESHbyESH is offline Offline
6 posts
since May 2005
Jan 19th, 2009
0

Re: check if datatable is empty

ds.tables(0).rows.count

janet.
Last edited by Narue; Jan 19th, 2009 at 11:30 am. Reason: snipped link
Reputation Points: 4
Solved Threads: 0
Newbie Poster
janet_ss is offline Offline
4 posts
since Apr 2008
Jan 19th, 2009
0

Re: check if datatable is empty

Really Janet.... A Thread From 2005?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 19th, 2009
0

Re: check if datatable is empty

Better late than never
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Nov 13th, 2009
-1
Re: check if datatable is empty
Try this

If Not dataset1 Is Nothing Then
.....
... Process dataset
.....
End If
Reputation Points: 10
Solved Threads: 0
Newbie Poster
keith_mas is offline Offline
1 posts
since Nov 2009
Nov 30th, 2009
1
Re: check if datatable is empty
You probably can guess this from the previous threads but I use:


VB.NET Syntax (Toggle Plain Text)
  1. dim MyTable as Datatable
  2. 'fill datatable....
  3. If (MyTable Is Nothing) Or (MyTable.Rows.Count =0 ) Then
  4. 'it's empty
  5. Else
  6. 'it exists and there are rows
  7. End if
Last edited by Nick Evan; Dec 2nd, 2009 at 10:23 am. Reason: Added code-tags
Reputation Points: 37
Solved Threads: 24
Posting Whiz in Training
G_Waddell is offline Offline
207 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Crystal Report Load Error
Next Thread in VB.NET Forum Timeline: problem in database connectivity





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC