Retrieving DataTable stored in Session not working as expected

  • Thread starter Thread starter John Kotuby
  • Start date Start date
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...
 
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.
 
Back
Top