Saturday, March 24, 2012

trouble retrieving data from ASPState Database

I'm trying to put together a web form that lists all current session information. The session info is stored in SQL server database (ASPState) and I'm trying to retreive and display using a SQLDataReader. I seem to have no trouble querying the "ASPStateTempSessions" table but when I try to write it to the page, I throw an exception that basically says that there is no data to display. Is there some special method you must use when performing this sort of task?
The code is as follows:
------------------
dim cn as new SqlConnection(ConfigurationSettings.AppSetting ("ASPState").toString())
cn.Open()
dim cmd as new SqlCommand("select * from ASPStateTempSessions where TimeOut = 40", cn)
dim dr as SqlDataReader
dr = cmd.ExecuteReader()
Response.Write(dr("Locked"))

------------------
The exception content is as follows:

System.InvalidOperationException: Invalid attempt to read when no data is present. at System.Data.SqlClient.SqlDataReader.PrepareRecord( Int32 i) at System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) at System.Data.SqlClient.SqlDataReader.get_Item(Strin g name) at ASP.AgreementListing_aspx.Page_Load(Object Sender, EventArgs E) in C:\Inetpub\wwwroot\onehour\AgreementListing.aspx:l ine 82when using a datareader, you have to first call the "read" method.

dr = cmd.ExecuteReader()
dr.read()
Response.Write(dr("Locked"))

OR to get all rows:

do while dr.read()
Response.Write(dr("Locked"))
loop (i think)

"Glenn Venzke" <GlennVenzke@.discussions.microsoft.com> wrote in message
news:BBA23A84-4649-4967-8AA3-6D98648C0555@.microsoft.com...
> I'm trying to put together a web form that lists all current session
information. The session info is stored in SQL server database (ASPState)
and I'm trying to retreive and display using a SQLDataReader. I seem to have
no trouble querying the "ASPStateTempSessions" table but when I try to write
it to the page, I throw an exception that basically says that there is no
data to display. Is there some special method you must use when performing
this sort of task?
> The code is as follows:
> ------------------
> dim cn as new SqlConnection(ConfigurationSettings.AppSetting
("ASPState").toString())
> cn.Open()
> dim cmd as new SqlCommand("select * from ASPStateTempSessions where
TimeOut = 40", cn)
> dim dr as SqlDataReader
> dr = cmd.ExecuteReader()
> Response.Write(dr("Locked"))
>
> ------------------
> The exception content is as follows:
> System.InvalidOperationException: Invalid attempt to read when no data is
present. at System.Data.SqlClient.SqlDataReader.PrepareRecord( Int32 i) at
System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) at
System.Data.SqlClient.SqlDataReader.get_Item(Strin g name) at
ASP.AgreementListing_aspx.Page_Load(Object Sender, EventArgs E) in
C:\Inetpub\wwwroot\onehour\AgreementListing.aspx:l ine 82
Boy, am I a dope! I thought datareader.read worked along the lines of recordset.EOF. I thought it was a simple boolean property that told you if any records were returned or not. But it is actually a method that returns a boolean. I guess you learn something new every day. Thanks much!!

"mark" wrote:

> when using a datareader, you have to first call the "read" method.
> dr = cmd.ExecuteReader()
> dr.read()
> Response.Write(dr("Locked"))
> OR to get all rows:
> do while dr.read()
> Response.Write(dr("Locked"))
> loop (i think)
>
>
> "Glenn Venzke" <GlennVenzke@.discussions.microsoft.com> wrote in message
> news:BBA23A84-4649-4967-8AA3-6D98648C0555@.microsoft.com...
> > I'm trying to put together a web form that lists all current session
> information. The session info is stored in SQL server database (ASPState)
> and I'm trying to retreive and display using a SQLDataReader. I seem to have
> no trouble querying the "ASPStateTempSessions" table but when I try to write
> it to the page, I throw an exception that basically says that there is no
> data to display. Is there some special method you must use when performing
> this sort of task?
> > The code is as follows:
> > ------------------
> > dim cn as new SqlConnection(ConfigurationSettings.AppSetting
> ("ASPState").toString())
> > cn.Open()
> > dim cmd as new SqlCommand("select * from ASPStateTempSessions where
> TimeOut = 40", cn)
> > dim dr as SqlDataReader
> > dr = cmd.ExecuteReader()
> > Response.Write(dr("Locked"))
> > ------------------
> > The exception content is as follows:
> > System.InvalidOperationException: Invalid attempt to read when no data is
> present. at System.Data.SqlClient.SqlDataReader.PrepareRecord( Int32 i) at
> System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) at
> System.Data.SqlClient.SqlDataReader.get_Item(Strin g name) at
> ASP.AgreementListing_aspx.Page_Load(Object Sender, EventArgs E) in
> C:\Inetpub\wwwroot\onehour\AgreementListing.aspx:l ine 82
>

0 comments:

Post a Comment