Friday, March 16, 2012
Persist DataTable Values
I have a DataTable that I have defined Globally. I populate this datatable
dynamically with file/folder information that I read directly from the
server. I use this datatable information to bind mainly to a DataGrid, but
I would also like it to persist for the duration that I have a particular
page open. I seem to be losing this data either thru postbacks or by the
way I have coded it. Does anyone have any suggestions on how I can keep
this datatable in memory? TIA
--
Best regards
Paul Perot
President
Perot Solutions
perotsolutions@dotnet.itags.org.earthlink.net
(770) 565 - 9151
(678) 852 - 7789 (c)You began by saying that the DataTable is "defined Globally." What does that
mean? Global to what? Obviously, it isn't truly global, or you wouldn't be
asking how to persist it. For example, if it was global to the Application
(in Application Cache, for example), you wouldn't need to persist it in any
other way. It would be truly global.
I only asked that to get you thinking along these lines, hopefully so that
this situation would not rear its ugly head again for you. The issue here is
scope. And a good understanding of ASP.Net scope is what is needed to
resolve it and any future issues of scope that may arise.
I suspect that by "defined Globally" you mean that you declared a variable
with Page scope, that is, defined in your Page class as a Field or Property
of your Page class, not inside a Method. However, the scope of such a
variable is global to only a single Page instance, and each Request for a
Page creates a new Page instance. A PostBack will not see the value of a
variable declared in a previous instance of this Page class, as the variable
will be re-initialized with the initialization of the Page class.
As you've defined the requirement that this variable be global to all
instances of this Page class (within the context of a single initial Request
and the related group of PostBacks), you could declare it as static (Shared
in VB.Net) in the Page class, or, if you want it re-initialized with each
non-PostBack (initial Request), you could put it into the Page's ViewState
or in Session. If you put it into ViewState, the size of it may affect the
time it takes for the Page to load in the browser (ViewState being a hidden
form field in the HTML form). The advantage of ViewState is that the
variable will disappear as soon as a different Page is Requested. If you use
Session, the Page will have to re-initialize the Session value when it is
Requested via non-PostBack (initial Request).
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Paul Perot" <perotsolutions@.earthlink.net> wrote in message
news:CyjCb.332$X97.100@.newsread2.news.atl.earthlin k.net...
> Hi All:
> I have a DataTable that I have defined Globally. I populate this
datatable
> dynamically with file/folder information that I read directly from the
> server. I use this datatable information to bind mainly to a DataGrid,
but
> I would also like it to persist for the duration that I have a particular
> page open. I seem to be losing this data either thru postbacks or by the
> way I have coded it. Does anyone have any suggestions on how I can keep
> this datatable in memory? TIA
> --
> Best regards
> Paul Perot
> President
> Perot Solutions
> perotsolutions@.earthlink.net
> (770) 565 - 9151
> (678) 852 - 7789 (c)
persistance of Classes
over to ASP.NET.
One question, I have a class, that i populate on a postback... I want to
keep this data persistant though other multiple postbacks.
What is the desired mechanism to use this.
I'm using the following code but not sure if this is the appropriate
solution to the problem
Private PatientDetails As Patient
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If ViewState("Patient") Is Nothing Then
PatientDetails = New Patient
viewstate.Add("Patient", PatientDetails)
Else
PatientDetails = New Patient
PatientDetails = viewstate("Patient")
End If
Private Function SearchPAS() As Boolean ' called by a button clik
PASmsg = PatientDetails.GetPASDetails(cSite, byCriteria, lbText.Text)
'which fills all the property fields
viewstate("Patient") = PatientDetails
end functionAdtmitedly this doesn't work yet because its not serialized, i'm also
working on how to do that as well
"Namshub" <Richard._NoSpam_ForME_Pullen@.Southend.nhs.uk> wrote in message
news:eqKZwy8KFHA.1176@.TK2MSFTNGP12.phx.gbl...
> I'm A client server developer by trade and i'm moving my skill set slowly
> over to ASP.NET.
> One question, I have a class, that i populate on a postback... I want to
> keep this data persistant though other multiple postbacks.
> What is the desired mechanism to use this.
> I'm using the following code but not sure if this is the appropriate
> solution to the problem
> Private PatientDetails As Patient
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> If ViewState("Patient") Is Nothing Then
> PatientDetails = New Patient
> viewstate.Add("Patient", PatientDetails)
> Else
> PatientDetails = New Patient
> PatientDetails = viewstate("Patient")
> End If
> Private Function SearchPAS() As Boolean ' called by a button clik
> PASmsg = PatientDetails.GetPASDetails(cSite, byCriteria, lbText.Text)
> 'which fills all the property fields
> viewstate("Patient") = PatientDetails
> end function
>
>
As long as your Patient class is serializable (which it apparently is), and
the extra HTML it adds to the document doesn't slow the response time down
too much, it's an excellent way to accomplish your goal. :)
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
What You S
"Namshub" <Richard._NoSpam_ForME_Pullen@.Southend.nhs.uk> wrote in message
news:eqKZwy8KFHA.1176@.TK2MSFTNGP12.phx.gbl...
> I'm A client server developer by trade and i'm moving my skill set slowly
> over to ASP.NET.
> One question, I have a class, that i populate on a postback... I want to
> keep this data persistant though other multiple postbacks.
> What is the desired mechanism to use this.
> I'm using the following code but not sure if this is the appropriate
> solution to the problem
> Private PatientDetails As Patient
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> If ViewState("Patient") Is Nothing Then
> PatientDetails = New Patient
> viewstate.Add("Patient", PatientDetails)
> Else
> PatientDetails = New Patient
> PatientDetails = viewstate("Patient")
> End If
> Private Function SearchPAS() As Boolean ' called by a button clik
> PASmsg = PatientDetails.GetPASDetails(cSite, byCriteria, lbText.Text)
> 'which fills all the property fields
> viewstate("Patient") = PatientDetails
> end function
>
>
Here is an excellent article/blog on making the viewstate serialization the
most efficient for custom classes. In a nutshell, viewstate does a really
good job serializing a specific set of objects, for anything else, your
better off to create a custom type converter.
http://weblogs.asp.net/vga/archive/...It.asp
x
"Namshub" wrote:
> I'm A client server developer by trade and i'm moving my skill set slowly
> over to ASP.NET.
> One question, I have a class, that i populate on a postback... I want to
> keep this data persistant though other multiple postbacks.
> What is the desired mechanism to use this.
> I'm using the following code but not sure if this is the appropriate
> solution to the problem
> Private PatientDetails As Patient
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> If ViewState("Patient") Is Nothing Then
> PatientDetails = New Patient
> viewstate.Add("Patient", PatientDetails)
> Else
> PatientDetails = New Patient
> PatientDetails = viewstate("Patient")
> End If
> Private Function SearchPAS() As Boolean ' called by a button clik
> PASmsg = PatientDetails.GetPASDetails(cSite, byCriteria, lbText.Text)
> 'which fills all the property fields
> viewstate("Patient") = PatientDetails
> end function
>
>
Hi,
You have several methods of persisting data between postbacks:
1. Using the view state - The entire viewstate data is serialized to a
hidden input field in the output html and then deserialized at postback.
This is good if your object does not contain large amounts of data
(serialization to the output html = large html output = slow loading for the
user).
2. Using the session state. This way the data is saved on the server between
postbacks (there are 3 different session state configurations you should
read about in MSDN).
Eran Kampf
http://www.ekampf.com/blog
http://www.ekampf.com
"Namshub" <Richard._NoSpam_ForME_Pullen@.Southend.nhs.uk> wrote in message
news:eqKZwy8KFHA.1176@.TK2MSFTNGP12.phx.gbl...
> I'm A client server developer by trade and i'm moving my skill set slowly
> over to ASP.NET.
> One question, I have a class, that i populate on a postback... I want to
> keep this data persistant though other multiple postbacks.
> What is the desired mechanism to use this.
> I'm using the following code but not sure if this is the appropriate
> solution to the problem
> Private PatientDetails As Patient
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> If ViewState("Patient") Is Nothing Then
> PatientDetails = New Patient
> viewstate.Add("Patient", PatientDetails)
> Else
> PatientDetails = New Patient
> PatientDetails = viewstate("Patient")
> End If
> Private Function SearchPAS() As Boolean ' called by a button clik
> PASmsg = PatientDetails.GetPASDetails(cSite, byCriteria, lbText.Text)
> 'which fills all the property fields
> viewstate("Patient") = PatientDetails
> end function
>
>
persistance of Classes
over to ASP.NET.
One question, I have a class, that i populate on a postback... I want to
keep this data persistant though other multiple postbacks.
What is the desired mechanism to use this.
I'm using the following code but not sure if this is the appropriate
solution to the problem
Private PatientDetails As Patient
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If ViewState("Patient") Is Nothing Then
PatientDetails = New Patient
viewstate.Add("Patient", PatientDetails)
Else
PatientDetails = New Patient
PatientDetails = viewstate("Patient")
End If
Private Function SearchPAS() As Boolean ' called by a button clik
PASmsg = PatientDetails.GetPASDetails(cSite, byCriteria, lbText.Text)
'which fills all the property fields
viewstate("Patient") = PatientDetails
end functionAdtmitedly this doesn't work yet because its not serialized, i'm also
working on how to do that as well
"Namshub" <Richard._NoSpam_ForME_Pullen@.Southend.nhs.uk> wrote in message
news:eqKZwy8KFHA.1176@.TK2MSFTNGP12.phx.gbl...
> I'm A client server developer by trade and i'm moving my skill set slowly
> over to ASP.NET.
> One question, I have a class, that i populate on a postback... I want to
> keep this data persistant though other multiple postbacks.
> What is the desired mechanism to use this.
> I'm using the following code but not sure if this is the appropriate
> solution to the problem
> Private PatientDetails As Patient
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> If ViewState("Patient") Is Nothing Then
> PatientDetails = New Patient
> viewstate.Add("Patient", PatientDetails)
> Else
> PatientDetails = New Patient
> PatientDetails = viewstate("Patient")
> End If
> Private Function SearchPAS() As Boolean ' called by a button clik
> PASmsg = PatientDetails.GetPASDetails(cSite, byCriteria, lbText.Text)
> 'which fills all the property fields
> viewstate("Patient") = PatientDetails
> end function
As long as your Patient class is serializable (which it apparently is), and
the extra HTML it adds to the document doesn't slow the response time down
too much, it's an excellent way to accomplish your goal. :)
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
"Namshub" <Richard._NoSpam_ForME_Pullen@.Southend.nhs.uk> wrote in message
news:eqKZwy8KFHA.1176@.TK2MSFTNGP12.phx.gbl...
> I'm A client server developer by trade and i'm moving my skill set slowly
> over to ASP.NET.
> One question, I have a class, that i populate on a postback... I want to
> keep this data persistant though other multiple postbacks.
> What is the desired mechanism to use this.
> I'm using the following code but not sure if this is the appropriate
> solution to the problem
> Private PatientDetails As Patient
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> If ViewState("Patient") Is Nothing Then
> PatientDetails = New Patient
> viewstate.Add("Patient", PatientDetails)
> Else
> PatientDetails = New Patient
> PatientDetails = viewstate("Patient")
> End If
> Private Function SearchPAS() As Boolean ' called by a button clik
> PASmsg = PatientDetails.GetPASDetails(cSite, byCriteria, lbText.Text)
> 'which fills all the property fields
> viewstate("Patient") = PatientDetails
> end function
Hi,
You have several methods of persisting data between postbacks:
1. Using the view state - The entire viewstate data is serialized to a
hidden input field in the output html and then deserialized at postback.
This is good if your object does not contain large amounts of data
(serialization to the output html = large html output = slow loading for the
user).
2. Using the session state. This way the data is saved on the server between
postbacks (there are 3 different session state configurations you should
read about in MSDN).
--
Eran Kampf
http://www.ekampf.com/blog
http://www.ekampf.com
"Namshub" <Richard._NoSpam_ForME_Pullen@.Southend.nhs.uk> wrote in message
news:eqKZwy8KFHA.1176@.TK2MSFTNGP12.phx.gbl...
> I'm A client server developer by trade and i'm moving my skill set slowly
> over to ASP.NET.
> One question, I have a class, that i populate on a postback... I want to
> keep this data persistant though other multiple postbacks.
> What is the desired mechanism to use this.
> I'm using the following code but not sure if this is the appropriate
> solution to the problem
> Private PatientDetails As Patient
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> If ViewState("Patient") Is Nothing Then
> PatientDetails = New Patient
> viewstate.Add("Patient", PatientDetails)
> Else
> PatientDetails = New Patient
> PatientDetails = viewstate("Patient")
> End If
> Private Function SearchPAS() As Boolean ' called by a button clik
> PASmsg = PatientDetails.GetPASDetails(cSite, byCriteria, lbText.Text)
> 'which fills all the property fields
> viewstate("Patient") = PatientDetails
> end function
Here is an excellent article/blog on making the viewstate serialization the
most efficient for custom classes. In a nutshell, viewstate does a really
good job serializing a specific set of objects, for anything else, your
better off to create a custom type converter.
http://weblogs.asp.net/vga/archive/...terGetAtIt.aspx
"Namshub" wrote:
> I'm A client server developer by trade and i'm moving my skill set slowly
> over to ASP.NET.
> One question, I have a class, that i populate on a postback... I want to
> keep this data persistant though other multiple postbacks.
> What is the desired mechanism to use this.
> I'm using the following code but not sure if this is the appropriate
> solution to the problem
> Private PatientDetails As Patient
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> If ViewState("Patient") Is Nothing Then
> PatientDetails = New Patient
> viewstate.Add("Patient", PatientDetails)
> Else
> PatientDetails = New Patient
> PatientDetails = viewstate("Patient")
> End If
> Private Function SearchPAS() As Boolean ' called by a button clik
> PASmsg = PatientDetails.GetPASDetails(cSite, byCriteria, lbText.Text)
> 'which fills all the property fields
> viewstate("Patient") = PatientDetails
> end function
>
>