I have a sql query that sometimes returns nothing...not null, just nothing.
This is causing my datareader to error out.
Is there a function, like isDbNull, but is for empty recordsets?
Thanks!
I have a sql query that sometimes returns nothing...not null, just nothing.
This is causing my datareader to error out.
Is there a function, like isDbNull, but is for empty recordsets?
Thanks!
Is use a line like this:
vb.net Code:
If DataReader IsNot Nothing Then DataReader.Read() End If
Sometimes the Programmer![]()
Sometimes the DBA![]()
Mazz1
I think the key is the DataReader.Read method... if there is a row to read, it will return true... if there are no rows, it returns false...
-tgCode:If myDataReader.Read() Then 'You have data Else 'You don't End if
* I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
*Proof positive that searching the forums does work: View Thread *
* How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
* How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
* Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
"There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
If you want to know whether a DataReader contains any records then you can test its HasRows property. You will also generally use the Read method that tg mentioned in a While loop:If you didn't need to notify the user of the empty result set then you'd do away with the If...Else block.vb.net Code:
If myDataReader.HasRows Then While myDataReader.Read() 'Read current row. End While Else MessageBox.Show("No data returned.") End If
2007-2013
Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB (*NEW* Match Two Game, *NEW* More Random Random Numbers) | C# (*NEW* Match Two Game, *NEW* More Random Random Numbers)
My Blog: Using Parameters in ADO.NET | Keyboard Events | Assemblies & Namespaces
What if the datareader only has an empty row?
I'm tying to fill this Arraylist, but I get an Specified cast is not valid. error
when I get a DataReader that only has an empty row.
So how can I detect if it has data or not?
I have tried m_dr.hasrows and .isdbnull neither worked.Code:Dim x As Integer While m_dr.Read ModelBuilder_Relation.Add(m_dr.GetInt32(0)) x = x + 1 End While
I don't know where that quote of viper5646's came from from but, if the data reader has an "empty" row, obviously HasRows will return True. An "empty" row is still a row. Presumably an "empty" row is one that contains a DBNull in every field, so using IsDBNull is the answer. Whoever posted that quote in the first place says that they tried IsDBNull but they obviously didn't use it properly. They would have to test every field for DBNull, which you could do by explicitly specifying every field:or you could use a loop or LINQ query.vb.net Code:
If dr.IsDBNull(0) AndAlso dr.IsDBNull(1) AndAlso dr.IsDBNull(2) Then 'The first three columns are all NULL. End If
2007-2013
Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB (*NEW* Match Two Game, *NEW* More Random Random Numbers) | C# (*NEW* Match Two Game, *NEW* More Random Random Numbers)
My Blog: Using Parameters in ADO.NET | Keyboard Events | Assemblies & Namespaces
Thanks jmcilhinney for your reply
when I tried the IsDBnull I get a No data exists for the row/column Error
When it executes the If m_dr.IsDBNull(0) line
This error appears even if there is data in the datareader.
This datareader contains only one column .
This is the code I used to test it.
Code:If m_dr.IsDBNull(0) Then MsgBox("null") End If
Last edited by viper5646; Jun 13th, 2011 at 08:03 PM.
2007-2013
Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB (*NEW* Match Two Game, *NEW* More Random Random Numbers) | C# (*NEW* Match Two Game, *NEW* More Random Random Numbers)
My Blog: Using Parameters in ADO.NET | Keyboard Events | Assemblies & Namespaces
Thanks
But there is still a lot that I need to learn about vb.
With your help I have it working.
Last edited by viper5646; Jun 13th, 2011 at 08:43 PM.