Cache changes for no reason !!! Please read.

  • Thread starter msnews.microsoft.com
  • Start date
M

msnews.microsoft.com

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

vMike

msnews.microsoft.com said:
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.
My experience with dataview is that cached copy doesn't work well as the
cache is actually changed when you filter the view because the filter
removes the records from the datatable/view. I usually cache the datatable
and use the copy method of the table for further filtering with a dataview.
I have not had a problem with this method. Others may have another approach.
Here is a snip below. In the mgGetProdData function I check the cache for
the datatable, if it is there I return the cached table, otherwise I run my
database function to get the data. Hope this helps.

dim dt as datatable
dt = mgDataObjs.mgGetProdData().copy()
Dim dv as dataview = new dataview(dt)
do filter/sort function here
 
J

Joe Fallon

Any object in the cache is stored as a reference.
So if you get a copy of the item in the cahce and edit it, you are actually
changing the cached item too.
This is because your "copy" is just a reference to the same object.

There are two things you can try:
1. Treat any cached data as Read Only.
2. Copy or clone the cached data and then act on the copy/clone.
 
M

msnews.microsoft.com

Okay, now it is clear - I have made the necessary modifications.

Thank you for your answers.
Alex. Nitulescu
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top