check if datatable is empty

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2005
Posts: 12
Reputation: Robert Walker is an unknown quantity at this point 
Solved Threads: 0
Robert Walker Robert Walker is offline Offline
Newbie Poster

check if datatable is empty

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 26
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: check if datatable is empty

 
0
  #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:
  1. Dim dr As OleDbDataReader = cmd.ExecuteReader()
  2.  
  3. Do While dr.Read
  4. ...
  5. ...
  6. Loop

Hope this helps
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 12
Reputation: Robert Walker is an unknown quantity at this point 
Solved Threads: 0
Robert Walker Robert Walker is offline Offline
Newbie Poster

Re: check if datatable is empty

 
0
  #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 Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 26
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: check if datatable is empty

 
0
  #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, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 6
Reputation: ESHbyESH is an unknown quantity at this point 
Solved Threads: 0
ESHbyESH's Avatar
ESHbyESH ESHbyESH is offline Offline
Newbie Poster

Re: check if datatable is empty

 
0
  #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 Quick reply to this message  
Join Date: Apr 2008
Posts: 4
Reputation: janet_ss has a little shameless behaviour in the past 
Solved Threads: 0
janet_ss janet_ss is offline Offline
Newbie Poster

Re: check if datatable is empty

 
0
  #6
Jan 19th, 2009
ds.tables(0).rows.count

janet.
Last edited by Narue; Jan 19th, 2009 at 11:30 am. Reason: snipped link
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: check if datatable is empty

 
0
  #7
Jan 19th, 2009
Really Janet.... A Thread From 2005?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: check if datatable is empty

 
0
  #8
Jan 19th, 2009
Better late than never
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: keith_mas is an unknown quantity at this point 
Solved Threads: 0
keith_mas keith_mas is offline Offline
Newbie Poster
 
-1
  #9
20 Days Ago
Try this

If Not dataset1 Is Nothing Then
.....
... Process dataset
.....
End If
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: G_Waddell is an unknown quantity at this point 
Solved Threads: 0
G_Waddell G_Waddell is online now Online
Newbie Poster
 
0
  #10
2 Days Ago
You probably can guess this from the previous threads but I use:


  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 niek_e; 19 Hours Ago at 10:23 am. Reason: Added code-tags
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC