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#" 开发者_Python百科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
You could try:
Teststr = Convert.ToString(Session["Co_ID"]);
which will handle null
s; 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)
Probably "Co_ID" session parameter doesn't exist. Check before:
if (Session["Co_ID"] != null)
{
Teststr = Session["Co_ID"].ToString();
}
You should perhaps see the error. Error says .ToString()
doesn't work with objects and moreover, you need not to write .ToString()
here. It will work without it as shown below
protected void Page_Load(object sender, EventArgs e)
{
BackEndUtils.OverallLoader();
string Teststr = Session["Co_ID"].ToString();
}
The exception could be raised in one of these two conditions:
- Something in the body of
BackEndUtils.OverallLoader()
raises the exception. - For whatever reason
Session["Co_ID"]
is null, and invokingToString()
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();
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.
精彩评论