views:

131

answers:

4

hi my dear friends ....

why i got this error :

Object reference not set to an instance of an object.

when i put this code in my page_load.:

protected void Page_Load(object sender, EventArgs e)
{
     BackEndUtils.OverallLoader();

     string Teststr = Session["Co_ID"].ToString();
}

==========================================================================

this session is made when user logins to my web site and this session works in other areas...

thanks for your attention

==========================================================================

thanks for your answers

i removed BackEndUtils.OverallLoader(); but error still exists

i tried Teststr = Convert.ToString(Session["Co_ID"]); and error disappeared - but i don't know why that session is null

in other areas that session works perfectly = such as a button in that form

what is the matter?

my web page markup is like this :

<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/AdminBackend.Master" AutoEventWireup="true" CodeBehind="Personel.aspx.cs" Inherits="Darman.Admin.Personel" Theme="DefaultTheme" %>

=================================================================================

i put this code in a button like this :

string Teststr = Convert.ToString(Session["Co_ID"]);

when i press that button THIS code in page Load(POSTBACK) + IN Button_Click works perfectly and shows me 23 (my Co_ID)

But when i run my page in browser (first time) this code in page load shows me null.

why?

thanks a lot

+1  A: 

Probably "Co_ID" session parameter doesn't exist. Check before:

if (Session["Co_ID"] != null) 
 {
   Teststr = Session["Co_ID"].ToString();
 }
Hun1Ahpu
A: 

The exception could be raised in one of these two conditions:

  1. Something in the body of BackEndUtils.OverallLoader() raises the exception.
  2. For whatever reason Session["Co_ID"] is null, and invoking ToString() on null fails.

Use the stack trace provided by the exception to determine which one is right. If it is #1 we will have to see the source of that method to do any further trouble shooting. If #2, you might need to do some manual correction around that line in your code (you might want to do that anyway):

object id = Session["Co_ID"];
if (id == null)
{
    id = Session["Co_ID"] = LoadCoIdFromSomewhere();
}

string Teststr = id.ToString();
Jørn Schou-Rode
Hoping for an explanation of the downvote...
Jørn Schou-Rode
A: 

The error may be coming from your BackEndUtils.OverallLoader.

As for the session variable - if someone comes to this page without logging in, it will not be set. The session might also have timed out and "Co_ID" is no longer set.

Oded
+1  A: 

You could try:

Teststr = Convert.ToString(Session["Co_ID"]);

which will handle nulls; also check: have you got session-state disabled for the page?

<%@ Page language="c#" ... EnableSessionState="false" %>

(although I would have perhaps expected a more obvious error in that case)

Marc Gravell
really thanks for your attention
LostLord
but why Session["Co_ID"] is null ? should n't.in other areas it works perfectly
LostLord
@LostLord - are they logged in? Is it the same ASP.NET application instance? it is app-pool recycling? Lots of possibilities...
Marc Gravell