Cache changes for no reason !!! Please read.

N

news.iq.ca

Posted: 01-21-2005 02:11 AM

Hi. I have this code:


----------------------------------------------------------------------------------------------------------------------------------------------------------------
Imports System.Data.SqlClient

Public Class DataTable_3

Inherits System.Web.UI.Page
Protected WithEvents cmdNext As System.Web.UI.WebControls.Button
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
Protected WithEvents cmdSearch As System.Web.UI.WebControls.Button
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents lblResults As System.Web.UI.WebControls.Label

----------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Cache.Remove("DataView")
End If
End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSearch.Click

Dim objDataView As DataView

Try
objDataView = ReadTable()
If txtLastName.Text & txtFirstName.Text = Nothing Then
DataGrid1.DataSource = objDataView
Else
DataGrid1.DataSource = FilterRecords(objDataView,
txtLastName.Text, txtFirstName.Text)
End If
DataGrid1.DataBind()
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
End Try
End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Function ReadTable() As DataView

Dim objConn As New SqlConnection()
Dim objDataAdapter As SqlDataAdapter
Dim objDataTable As New DataTable()
Dim objDataView As DataView

Try
If Cache("DataView") Is Nothing Then
objConn.ConnectionString =
"server=radu;database=workdb;integrated security=sspi;"
objConn.Open()
objDataAdapter = New SqlDataAdapter("select * from Authors order
by au_lname, au_fname", objConn)
objDataAdapter.Fill(objDataTable)
objDataView = objDataTable.DefaultView()
Cache("DataView") = objDataView

objConn.Close()
objConn.Dispose()
objDataAdapter.Dispose()
objDataTable.Dispose()
Else
objDataView = Cache("DataView")
End If
Return objDataView
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
End Try
End Function
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Function FilterRecords(ByVal objDataView As DataView, ByVal
strLastName As String, ByVal strFirstName As String, Optional ByVal strOrder
As String = Nothing) As DataView

'Search in 'Authors' by LastName and/or FirstName
Try
strLastName = txtLastName.Text
strFirstName = txtFirstName.Text
If strLastName <> Nothing And strFirstName <> Nothing Then
objDataView.RowFilter = "au_lname = '" & strLastName & "' and
au_fname = '" & strFirstName & "'"
ElseIf strLastName <> Nothing Then
objDataView.RowFilter = "au_lname = '" & strLastName & "'"
ElseIf strFirstName <> Nothing Then
objDataView.RowFilter = "au_fname = '" & strFirstName & "'"
Else
'Show ALL records.
End If

If strOrder <> Nothing Then
objDataView.Sort = strOrder
End If

Return objDataView
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
End Try
End Function
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand

Dim objDataView As DataView

objDataView = ReadTable()
DataGrid1.DataSource = FilterRecords(objDataView, txtLastName.Text,
txtFirstName.Text, e.SortExpression)
DataGrid1.DataBind()
End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------------
End Class
----------------------------------------------------------------------------------------------------------------------------------------------------------------


The problem is this: I run the project and I press on search without any
search criteria - it returns all 42 records I have in the table. Fine. I now
enter a LastName criteria, in my case "Ringer" which will return 10 records,
fine again. Now I clear the "lastname" textbox and I run "Search" again - I
expect to see again all my 42 records. However, I see only the 10 records
corresponding to "ringer" - the cache got overwritten !!! The question is
WHY ! As you can see in the code, a) I do NOT pass the cache to the
"FilterRecords" function, but objDataView and b) even if I did - I'm passing
ByVal, not ByRef !

So the point is that the cache is not removed - it is overwritten by my
"FilterRecords" function, although I never specifically tell it to do so. I
only have one line which writes to the cache: Cache("DataView") =
objDataView, in the function "ReadTable". That's it. Then I pass objDataView
to a function to be filtered and maybe sorted. But I expect the cache to
stay put, i.e. to contain all the records in my table, not only a filtered
version !

I'm completely lost here. Please help. Thanks a lot !
Alex.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top