RSS Forums RSS
Please support our VB.NET advertiser: Programming Forums
Views: 14864 | Replies: 4
Reply
Join Date: May 2005
Posts: 12
Reputation: Robert Walker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Robert Walker Robert Walker is offline Offline
Newbie Poster

Help check if datatable is empty

  #1  
May 17th, 2005
I can loop while the datatable is full, but get an error when it is empty. How do I know it's empty.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2003
Location: Canada
Posts: 786
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Rep Power: 9
Solved Threads: 25
Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: check if datatable is empty

  #2  
May 17th, 2005
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
Assistant Manager, Regional Pharmacy Information Systems
TLC Services Website (Under Construction)
Updated : ASP.Net Login Code
Reply With Quote  
Join Date: May 2005
Posts: 12
Reputation: Robert Walker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Robert Walker Robert Walker is offline Offline
Newbie Poster

Help Re: check if datatable is empty

  #3  
May 18th, 2005
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
Reply With Quote  
Join Date: Feb 2003
Location: Canada
Posts: 786
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Rep Power: 9
Solved Threads: 25
Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: check if datatable is empty

  #4  
May 18th, 2005
(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

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
Assistant Manager, Regional Pharmacy Information Systems
TLC Services Website (Under Construction)
Updated : ASP.Net Login Code
Reply With Quote  
Join Date: May 2005
Posts: 6
Reputation: ESHbyESH is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ESHbyESH's Avatar
ESHbyESH ESHbyESH is offline Offline
Newbie Poster

Re: check if datatable is empty

  #5  
May 18th, 2005
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:50 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC