|
 |
aspx thread: Missing Record in DataReader
Message #1 by "Ajay Varghese" <reese@b...> on Thu, 18 Jan 2001 01:16:28 +0530
|
|
Hi,
I am trying to bind a DropdownList box to a Data Reader . As an error
would be thrown if I try to set the Dropdown to an empty Datasrource, I
would like to check if there are any records in the DataSource before
trying to bind it to the Dropdown. For this, I use the DataReader.Read
method. But in cases where some record is returned, the list box always
skips the first record when populating the Dropdown box. I guess this
has to do with the DataReader being a Forward Only Stream and the
DataReader.Read method using up the current stream in memory. Does that
make sense?
Anyway, could any one let me know how I can over come this? or if there
is a better way of doing this. Pls see the code below.
Thanks,
Ajay
MyConnection =3D New
SQLConnection("server=3Dservername;uid=3Dusername;pwd=3Dpassword;database
=3DPM")
SQLString =3D "select [Assignment Id], [Assignment Title] from
[Assignment Master] Where [Module Id] =3D " &
LstModule.SelectedItem.Value
Dim MyCommand2 As New SQLCommand(SQLString, MyConnection)
MyConnection.Open()
Dim DR2 as SQLDataReader
MyCommand2.Execute(DR2)
If DR2.Read Then
LstAssignment.DataSource =3D DR2
LstAssignment.DataTextField=3D"Assignment Title"
LstAssignment.DataValueField=3D"Assignment Id"
LstAssignment.DataBind()
End If
MyConnection.Close()
Message #2 by =?iso-8859-1?Q?Fredrik_Norm=E9n?= <fredrik.normen@e...> on Thu, 18 Jan 2001 09:00:11 +0100
|
|
Hi,
If you want to check if there are any records in the DataReader, you could
simply check if there is any record to be read.
The default positioning of the DataReader is prior to the first record,
therefore you must call Read before accessing any data.
So if you write "If DR2.Read()" it will jump to the first record, if there
is a record that could be read it return true otherwise, false.
I hope this will help you.
/Fredrik Normén
-----Original Message-----
From: Ajay Varghese [mailto:reese@b...]
Sent: den 17 januari 2001 20:46
To: ASP+
Subject: [aspx] Missing Record in DataReader
Hi,
I am trying to bind a DropdownList box to a Data Reader . As an error
would be thrown if I try to set the Dropdown to an empty Datasrource, I
would like to check if there are any records in the DataSource before
trying to bind it to the Dropdown. For this, I use the DataReader.Read
method. But in cases where some record is returned, the list box always
skips the first record when populating the Dropdown box. I guess this
has to do with the DataReader being a Forward Only Stream and the
DataReader.Read method using up the current stream in memory. Does that
make sense?
Anyway, could any one let me know how I can over come this? or if there
is a better way of doing this. Pls see the code below.
Thanks,
Ajay
MyConnection =3D New
SQLConnection("server=3Dservername;uid=3Dusername;pwd=3Dpassword;database
=3DPM")
SQLString =3D "select [Assignment Id], [Assignment Title] from
[Assignment Master] Where [Module Id] =3D " &
LstModule.SelectedItem.Value
Dim MyCommand2 As New SQLCommand(SQLString, MyConnection)
MyConnection.Open()
Dim DR2 as SQLDataReader
MyCommand2.Execute(DR2)
If DR2.Read Then
LstAssignment.DataSource =3D DR2
LstAssignment.DataTextField=3D"Assignment Title"
LstAssignment.DataValueField=3D"Assignment Id"
LstAssignment.DataBind()
End If
MyConnection.Close()
Message #3 by Susan Warren <swarren@m...> on Wed, 17 Jan 2001 17:45:36 -0800
|
|
Ajay,
You won't get an error is the datasource is empty, your list will just
render empty. So you don't need to test for records in the datasource
first; just bind. Here's an example that will never return rows, but the
dropdownlist renders.
hth,
Susan
-------------------------------------------------
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<html>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
Dim MyConnection As SQLConnection = New
SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
Dim MyCommand As SQLCommand = New SQLCommand("select * from Authors
WHERE au_id='invalid value'", MyConnection)
MyConnection.Open()
Dim DR As SQLDataReader
MyCommand.Execute(DR)
mylist.DataSource = DR
mylist.DataTextField="au_lname"
mylist.DataValueField="au_id"
mylist.DataBind()
MyConnection.Close()
End Sub
</script>
<body>
<asp:DropDownList id=mylist width="200" runat="server" />
</body>
</html>
-----Original Message-----
From: Ajay Varghese [mailto:reese@b...]
Sent: Wednesday, January 17, 2001 11:46 AM
To: ASP+
Subject: [aspx] Missing Record in DataReader
Hi,
I am trying to bind a DropdownList box to a Data Reader . As an error
would be thrown if I try to set the Dropdown to an empty Datasrource, I
would like to check if there are any records in the DataSource before
trying to bind it to the Dropdown. For this, I use the DataReader.Read
method. But in cases where some record is returned, the list box always
skips the first record when populating the Dropdown box. I guess this
has to do with the DataReader being a Forward Only Stream and the
DataReader.Read method using up the current stream in memory. Does that
make sense?
Anyway, could any one let me know how I can over come this? or if there
is a better way of doing this. Pls see the code below.
Thanks,
Ajay
MyConnection =3D New
SQLConnection("server=3Dservername;uid=3Dusername;pwd=3Dpassword;database
=3DPM")
SQLString =3D "select [Assignment Id], [Assignment Title] from
[Assignment Master] Where [Module Id] =3D " &
LstModule.SelectedItem.Value
Dim MyCommand2 As New SQLCommand(SQLString, MyConnection)
MyConnection.Open()
Dim DR2 as SQLDataReader
MyCommand2.Execute(DR2)
If DR2.Read Then
LstAssignment.DataSource =3D DR2
LstAssignment.DataTextField=3D"Assignment Title"
LstAssignment.DataValueField=3D"Assignment Id"
LstAssignment.DataBind()
End If
MyConnection.Close()
|
|
 |