Session Var Question

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

I am developing with VS.NET 2003 on the ASP.NET 1.1 platform.

I don't know how to get around this 'Object reference not set to an instance
of an object' error.

How should I check to see if a session variable exists?
Shouldn't just erifying that the variable does not equal null do the job?



The very first time this page loads, the variable will not exists as it does
not get assigned until the user interacts with a diffrent page.

On a round trip from that other page however, the variable will exist.

As it is now, I get

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 90: DDL_BlockList.DataSource =
bizObj.Get_Distinct_Block_Lists(IB_Acct);Line91: DataBind();
Line 92: if(Session["NewList"].ToString() != "")
Line 93: DDL_BlockList.SelectedValue = Session["NewList"].ToString();
Line 94:
 
Frank said:
I am developing with VS.NET 2003 on the ASP.NET 1.1 platform.

I don't know how to get around this 'Object reference not set to an
instance of an object' error.

How should I check to see if a session variable exists?
Shouldn't just erifying that the variable does not equal null do the job?



The very first time this page loads, the variable will not exists as it
does not get assigned until the user interacts with a diffrent page.

On a round trip from that other page however, the variable will exist.

As it is now, I get

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 90: DDL_BlockList.DataSource =
bizObj.Get_Distinct_Block_Lists(IB_Acct);Line91: DataBind();
Line 92: if(Session["NewList"].ToString() != "")
Line 93: DDL_BlockList.SelectedValue = Session["NewList"].ToString();
Line 94:

In your code line 92 you are calling .ToString() on your session
variable. If it is null then ToString is not available. Try adding a
check for:

if(Session["NewList"] != null) before you check to see if the ToString is
!= "".
 
Frank,

In the code you're checking for an empty string instead of null.

you should try this first:

if (Session["NewList"] != null && Session["NewList"].ToString() !=
String.Empty)

That if condition will check if it's not null, if that's true it will
continue to check the string.
 

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

Back
Top