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
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