ASP.Net Object Life

N

njk966

I have a dynamically loaded control that queries a database and fills
the results into a new object.

Class Person
...class properties
...class methods
End Class

Dim Person As New Person
Person.GetPersonInformation(Session("PersonId"))

Problem: Person Object will persist upon posting back, even after
creating a new Person

Question: I thought state is not held between postbacks
 
J

Juan T. Llibre

G

Guest

Actually, I am not sure you have posted sufficient code to illustrate your
problem.
If you are getting this object into Session and then accessing it again from
Session each time, why would you expect it NOT to persist across postbacks
(that's what Session State is for). If you need a new object each time, then
you should consider replacing the one in Session with your new one.
Peter
 
N

njk966

Thanks for writing back...

Juan, This is my own simple object I instantiate, therefore I do not
have a dispose method???

Peter, I'm not saving the object to Session at anytime. Here are some
steps:
1. ASPXPage.LoadControl("PersonControl.ascx")
2. Call Sub BuildPerson - Create Person, Retrieve Age
3. Post Back and step through
a. Create Person --Age persists

Class Person
Property Age
Public Function GetPersonInformation()
...get age from database
... set property Age
End Function
End Class

Sub BuildPerson
Dim Person As New Person
Person.GetPersonInformation()
dim age=Person.Age
End Sub
 
J

Juan T. Llibre

re:
Peter, I'm not saving the object to Session at anytime.

You are retrieving a value from Session, according to what you wrote :

Dim Person As New Person
Person.GetPersonInformation(Session("PersonId"))

So, you must have *previously* saved Session("PersonId"), right ?

re:
Juan, This is my own simple object I instantiate,
therefore I do not have a dispose method?

If your object implements IDisposable ( and quite a few created objects do ),
unless you dispose of the object, you'll have to wait until GC ( garbage collection )
gets rid of it for you...unless you dispose of it with objectname.dispose().

You might need to *implement* .dispose() for your object.

See :
http://zones.advisor.com/doc/16573
....in particular, and :

http://www.informit.com/articles/article.asp?p=25751&seqNum=3&rl=1
http://www.informit.com/articles/article.asp?p=101592&seqNum=4&rl=1
http://www.velocityreviews.com/forums/t78250-re-dispose-pattern-in-aspnet-pages.html
....as background info.





Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
----- Original Message -----
From: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Sent: Thursday, July 27, 2006 4:28 PM
Subject: Re: ASP.Net Object Life
 
G

Guest

If I understand what you wrote correctly, then it appears you're building the
Person class on each Page_Load event execution of the PersonControl.ascx
page. That would explain why it appears to be persisting accross postbacks.
 
N

njk966

Juan, great articles, thanks.

re: Session... I'm saving just an Id in session not the Person object

re: Dispose... I do close the SQLconnection immediately in my Person
object. ( its my Person "object that lives between postbacks" per your
last article
--http://www.velocityreviews.com/forums/t78250-re-dispose-pattern-in-aspnet-pages.html
)

Demetri has it nearly correct when he says "it appears you're building
the
Person class on each Page_Load event execution of the
PersonControl.ascx
page. That would explain why it appears to be persisting accross
postbacks."
Its not actually in the Page_Load event, but I am building the Person
Class each time I load my control
(ASPXPage.LoadControl("PersonControl.ascx")

Question: Per your last article and Demetri's comment ("...across
postbacks"). Would it be reliable to use that data that persists across
postbacks, thereby eliminating the need
to create Person object and hit the database connection again. Because
I actually have another Sub that uses Person data upon postback or is
there a way to not persist between postbacks. Thanks

Sub AnotherSub {
Dim Person As New Person
if Person.Age = Nothing then Person.GetPersonInformation()
else
dim age=Person.Age
}
 
N

njk966

By the way... I did "Implements IDisposable" in Person Object and still
persists data..

Class Person
Implements IDisposable

Property Age as integer
Sub GetPersonData()
SQLCn.Open
Me.Age = DS.Item("Age")
SQLCn.Close
End Sub

Private disposedValue As Boolean = False
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when explicitly called
End If
' TODO: free shared unmanaged resources
End If
Me.disposedValue = True
End Sub

Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class
 
N

njk966

Sorry to be so "persistant", but this effects how I design my whole
app.

Demetri seemed to hit the nail on the head when he wrote,

"If I understand what you wrote correctly, then it appears you're
building the Person class in each Page_Load event execution of the
PersonControl.ascx page. That would explain why it appears to be
persisting across postbacks."


Question 1: Where is it persisting the Person object?
Question 2: How long does it persist?
Question 3: Would it be reliable to use that data?
Question 4: If not reliable, how do I destroy the object?
 
N

njk966

I answered 3 out of 4 questions from a discussion I found from Kevin
Spencer,
Microsoft MVP.

"State refers to the persistence of an object (class instance or
primitive) in memory Whenever an object is instantiated (created), it
must reside in memory somewhere. The lifetime of an object is limited
to the lifetime of the container in which it resides. A method is
instantiated, just like a variable, whenever you call it. And just like
a variable, when it returns, it is available for garbage collection.
The only way to persist an instance is to put it into something else
that persists. Hence, ASP.Net, which operates using HTTP, which is
stateless, has mechanisms for maintaining state, such as Session,
Application, and ViewState."

Question 1: Where is it persisting the Person object? --on the stack
Question 2: How long does it persist? --until GC or I dispose
Question 3: Would it be reliable to use that data? --No
Question 4: If not reliable, how do I destroy the object?

As indicated from my previous posts, I did try to Implements a dispose
on the object. Does anyone see what I am doing wrong?
 
N

njk966

FYI: I was able to determine why the object was persisting between
postbacks, but still don't understand why or why I couldn't despose
with IDisposable.

I mistakenly declared the variable Age as Private Shared instead of
just Private. Now the GC does the job correctly. Thanks to all that
replied.
 

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

Top