Cached DropDownList has items, but won't display. Where's my error

J

jroughgarden

I am trying to cache a dropdown list rather than hit the database every time
this or other pages that need it load. I was going to cache the data by
caching the datareader, but that did not work. Then I thought of caching the
entire control, as shown below. The code runs without error, but the dropdown
is empty. With the second line below, I can verify that there are items in
the dropdown, but they don't show on the page. Another fallback will be to
cache the data in an array, and rebuild the dropdown each time, but caching
the entire dropdown would be more elegant.

ddPatients = fddPatients(lngUserID, lblError.Text)
Dim I As Integer = ddPatients.Items.Count

'** This function checks the cache and the second one does the database hit
if needed.

Public Function fddPatients(ByVal vlngUserID As Integer, ByRef rstrErr
As String) As DropDownList
Dim strCacheName = "Patients-" + CStr(vlngUserID)
If Cache(strCacheName) Is Nothing Then
fddPatients = fddPatientsFill(vlngUserID, rstrErr)
Cache.Insert(strCacheName, fddPatients, Nothing,
DateTime.MaxValue, TimeSpan.FromMinutes(20))
Else
fddPatients = CType(Cache(strCacheName), DropDownList)
End If
End Function

Public Function fddPatientsFill(ByVal vlngUserID As Integer, ByRef
rstrErr As String) As DropDownList
Dim strCacheName = "Patients-" + CStr(vlngUserID)
Dim cnnCT As New SqlClient.SqlConnection(Application("gstrCnn"))
Dim cmdX As New SqlClient.SqlCommand("stpPatientsGet", cnnCT)
cmdX.CommandType = CommandType.StoredProcedure
Dim plngUserID As SqlClient.SqlParameter =
cmdX.Parameters.Add("@lngUserID", SqlDbType.Int)
plngUserID.Value = vlngUserID
Dim rdrX As SqlClient.SqlDataReader
Dim ddX As New DropDownList
Try
If cnnCT.State = ConnectionState.Closed Then cnnCT.Open()
'Get Patients
rdrX = cmdX.ExecuteReader()
ddX.Items.Clear()
Do While rdrX.Read
Dim NewItem As New ListItem
NewItem.Text = rdrX("strPatientName")
NewItem.Value = rdrX("lngPatientID")
ddX.Items.Add(NewItem)
Loop
rdrX.Close()
'Add All Patients as option
Dim NewItem2 As New ListItem
NewItem2.Text = "All Patients"
NewItem2.Value = 0
ddX.Items.Add(NewItem2)
ddX.Items(ddX.Items.Count - 1).Selected = True
fddPatientsFill = ddX
Catch ex As Exception
rstrErr = ex.Message
End Try

If (Not cmdX Is Nothing) Then cmdX = Nothing
If (Not cnnCT Is Nothing) Then cnnCT = Nothing
End Function
 
A

Ashish Kaila

Why dont you populate an ArrayList the first time and then save it in
viewstate ? In this way you can just set reference of DataSource to
ArrayList and be done with.
HTH
Ashish
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top