-
Jan 26th, 2009, 11:19 AM
#1
Thread Starter
Member
datareader and empty records
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!
-
Jan 26th, 2009, 11:26 AM
#2
Re: datareader and empty records
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
-
Jan 26th, 2009, 12:16 PM
#3
Re: datareader and empty records
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...
Code:
If myDataReader.Read() Then
'You have data
Else
'You don't
End if
-tg
-
Jan 26th, 2009, 08:20 PM
#4
Re: datareader and empty records
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:
vb.net Code:
If myDataReader.HasRows Then While myDataReader.Read() 'Read current row. End While Else MessageBox.Show("No data returned.") End If
If you didn't need to notify the user of the empty result set then you'd do away with the If...Else block.
-
Jun 12th, 2011, 10:05 PM
#5
Lively Member
Re: datareader and empty records
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
-
Jun 12th, 2011, 10:44 PM
#6
Re: datareader and empty records
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:
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
or you could use a loop or LINQ query.
-
Jun 13th, 2011, 07:53 PM
#7
Lively Member
Re: datareader and empty records
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.
-
Jun 13th, 2011, 08:05 PM
#8
Re: datareader and empty records
Originally Posted by viper5646
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
Have you called Read? The DataReader doesn't advance to the first row until you call Read.
-
Jun 13th, 2011, 08:35 PM
#9
Lively Member
Re: datareader and empty records
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|