|
-
Jan 14th, 2003, 02:22 PM
#1
Thread Starter
Lively Member
how to handle a datareader with no records..
I am using a datareader and its returning no records and I want to know how to handle that.
Example:
If not IsDbNull(dr("field1")) Then
listbox1.Items.FindByValue(CType(dr("field1"), string).Selected = True
End If
Error:
Invalid attempt to Read when no data is present.
-
Jan 14th, 2003, 02:38 PM
#2
if not objReader.Read() then
'no records
else
Do
'stuff
while objReader.read()
end if
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Jan 14th, 2003, 02:44 PM
#3
Thread Starter
Lively Member
-
Jan 16th, 2003, 10:39 AM
#4
Member
I had an issue that the datagrid was still showing the results of the last query when the datareader was coming back empty. Couldn't figure out how to clear it out in that case. I ended up setting the visible property to false if "not datareader.read()" and to true elsewise...
Is there another way of doing this?...
Ooogs
-
Jan 16th, 2003, 12:06 PM
#5
Thread Starter
Lively Member
are you binding the data to the grid?
-
Jan 16th, 2003, 12:32 PM
#6
Member
yep, like this:
Dim qry2 As String = "SELECT * FROM [tbl Main] WHERE (processor =" & Chr(39) & who & Chr(39) & ") AND (code <> 'C - Complete') and [Date Completed] Is Null"
dc1.Open()
Dim cmd As New OleDb.OleDbCommand()
cmd.Connection = dc1
cmd.CommandText = qry2
Dim datareader As OleDb.OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If Not datareader.Read() Then
lblTest.Text = "Empty set"
dgProc.Visible = False
Else
dgProc.Visible = True
dgProc.DataSource = datareader
dgProc.DataBind()
dc1.Close()
End If
-
Jan 16th, 2003, 02:11 PM
#7
Thread Starter
Lively Member
Have you tried to set the datasource property = nothing at the top of the code.
I would think the datasource property is the one that is the source of the problem. Thats where the DataBind() method gets the dataset or datareader from. At least thats my understanding.
just a thought.....
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
|