how to solve this error

Posted by Parthibansk under ASP.NET on 11/28/2012 | Points: 10 | Views : 773 | Status : [Member] | Replies : 6
private int m_companyID;

protected void Page_Load(object sender, EventArgs e)
{
m_companyID = (int)Session["CompanyID"];
}

Object reference not set to an instance of an object..
i get this when i compile..




Responses

Posted by: Hariinakoti on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
hi,
I think company id is not passing to sessions.check it out properly definitely u'll get solution.

Thanks & Regards
Hari

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
First check wheather there is data in your session
private int m_companyID; 

protected void Page_Load(object sender, EventArgs e)
{

//m_companyID = (int)Session["CompanyID"];
Session["CompanyId"] = 10;
m_companyID = (int)Session["CompanyId"];
Response.Write(m_companyID);

}

Or use if condition:
private int m_companyID; 

protected void Page_Load(object sender, EventArgs e)
{

//m_companyID = (int)Session["CompanyID"];
//Session["CompanyId"] = 10;
if (Session["CompanyId1"] != null)
{
m_companyID = (int)Session["CompanyId1"];
Response.Write(m_companyID);
}
else
{
Response.Write("No data in session");
}
}


Thanks & regards

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Parthibansk on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
i tried this i get another error

"Format of the initialization string does not conform to specification starting at index 0."





Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
The part of the error that says "index 0" means that the beginning of the connection string does not contain valid input. If you use a connection string like "0", "Data Source", or random text like "gggguuuu", it will return an error with "index 0".

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tg on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

When value is not present in the session means it gives like this exception.
use this
m_companyID = (int)(System.Web.HttpContext.Current.Session["CompanyID"].ToString());

Thanks

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 11/30/2012 [Member] Starter | Points: 25

Up
0
Down
m_companyID = Convert.ToInt32(Session["CompanyID"].ToString());

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response