Object Lifetime.

C

Control Freq

Hi,
I am still new to dotnet programming, and ASP.NET in general.

Something that has been bugging me for a while is this:

In a Web Form application, using C# in the codebehind. If I have a drop
down combobox filled with a list of stuff, like employee names etc. I
can select a persons name and see the codebehind event fired. No news
flash there then.
However, if I leave the web form open for a few minutes, make a coffee
etc, then come back, and choose a different user name, I get 'blah blah
not set to an instance of an object' exception.

It looks like the garbage collector has cleaned up my Combobox.

I am missing something fundamental about dotnet programming?
I have tried a few things, like making some stuff static etc, nothing
helps.

I am using VS2005 on XP.

Regards
 
H

Hans Kesting

Hi,
I am still new to dotnet programming, and ASP.NET in general.

Something that has been bugging me for a while is this:

In a Web Form application, using C# in the codebehind. If I have a drop
down combobox filled with a list of stuff, like employee names etc. I
can select a persons name and see the codebehind event fired. No news
flash there then.
However, if I leave the web form open for a few minutes, make a coffee
etc, then come back, and choose a different user name, I get 'blah blah
not set to an instance of an object' exception.

It looks like the garbage collector has cleaned up my Combobox.

I am missing something fundamental about dotnet programming?
I have tried a few things, like making some stuff static etc, nothing
helps.

I am using VS2005 on XP.

Regards

"static" will have (possibly) surprising effects in web programming: as
the web application is a single application that serves multiple users,
*all* users will then share the same instance of that static variable.
(which you do not want in this case).

That combobox is destroyed as soon as the page finishes rendering, that
is, as soon as the html has been sent to the browser. A new request
(which can be a "postback") is served by a new instance of the page.

How long was that "few minutes"? If it was more than 20, then you have
hit the session timeout (20 minutes is the default, maybe you set it to
a lower number?). If it was not the session timeout (or you didn't use
session variables) then obviously it's something else. In that case you
need to show some of your code.

Hans Kesting
 
L

Laurent Bugnion [MVP]

Hi,

Control said:
Hi,
I am still new to dotnet programming, and ASP.NET in general.

Something that has been bugging me for a while is this:

In a Web Form application, using C# in the codebehind. If I have a drop
down combobox filled with a list of stuff, like employee names etc. I
can select a persons name and see the codebehind event fired. No news
flash there then.
However, if I leave the web form open for a few minutes, make a coffee
etc, then come back, and choose a different user name, I get 'blah blah
not set to an instance of an object' exception.

It looks like the garbage collector has cleaned up my Combobox.

I am missing something fundamental about dotnet programming?
I have tried a few things, like making some stuff static etc, nothing
helps.

I am using VS2005 on XP.

Regards

We would need to see some code (if possible trimmed to the max in order
to get the most information from the smallest amount of code possible
:) to say more, but if waiting without doing anything else creates such
a big difference, the first suspect is a session timeout.

If you wait long enough (default: 20 minutes) without "stimulating" a
web application, the Session is recycled. It means that everything you
stored in it is destroyed.

However, since the session ID is stored in the client, the next time you
connect (for example by firing an event), a new Session object is
created. If your code gets stuff from the Session without checking first
if it is null, you may very well get that exception.

HTH,
Laurent
 

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