Retrieving DataTable stored in Session not working as expected

J

John Kotuby

Hi all,

This is the first time I have tried storing a datatable into a session
variable and then later bind it to a DropDownList.
The List contains about 2600 entries of values and text. It represents a
lookup table.
I am trying to not use SessionState on this particular page as that appears
to make the page size too large and affects perfomance.

Upon intitial page load I am running a SQL Server select which uses a
dataAdapter to fill the DataTable.
I then attempt to save the datatable to Session and finally bind the
datatable to the lstOwner (DropDownList).
Here is that code:
-----------------------------------------------------------------------
MyAdapter.SelectCommand = MyCommand
MyAdapter.Fill(OwnerTable)

' Add the Ownertable to session vars for later use
Session.Add("dtOwners", OwnerTable)

'Specify which field is to be Value and wich field is to be Text
Me.lstOwner.DataTextField = "company"
Me.lstOwner.DataValueField = "ownercode"

' Now bind the table to the DropDownList
Me.lstOwner.DataSource = OwnerTable
Me.lstOwner.DataBind()
------------------------------------------------------------------------

Upon postback I attempt to reference the Session variable to re-load the
lookup list without running another SQL query.

------------------------------------------------------------------------
If Page.IsPostBack Then
'Check existance of ddlOwners session var and load the Owners into
lstOwners
If Not Session("dtOwners") Is Nothing Then
lstOwner.DataSource = CType(Session("dtOwners"), DataTable)
lstOwner.DataBind()
Else
'Run the SQL query
GetOwners()
End If
End If

---------------------------------------------------------------------

What I end up with in lstOwners are 2600 entries of
"System.Data.DataRowView".

Can anyone tell me what I am doing wrong? I thought I was following the
example of some code that was posted by Stephen Cheng from Microsoft, but
apparently I am making a mistake.

Thanks for any help...
 
J

John Kotuby

Well I discovered the mistake I was making during my lunch break while
searching on the phrase System.Data.DataRowView.
I guess my old brain needed some glucose.

I was failing to assign the DataTextField and DataValueField properties of
the dropdownlist as I had with the original DataTable.
Me.lstOwner.DataTextField = "company"
Me.lstOwner.DataValueField = "ownercode"

Now that I have added that code thusly:

lstOwner.DataSource = CType(Session("dtOwners"), DataTable)

lstOwner.DataTextField = "company"
lstOwner.DataValueField = "ownercode"
lstOwner.DataBind()

Everything is working as planned.

Thanks for any replies that I have not yet seen posted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top