3 Replies - 4533 Views - Last Post: 19 May 2011 - 08:07 AM Rate Topic: -----

#1 rani_240887  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-January 11

Error:system.nullreferenceexception object reference not set to an ins

Posted 19 May 2011 - 03:57 AM

hi this is rani
i m getting dis error (system.nullreferenceexception object reference not set to an instance of an object. c sharp)which i am not able to solve it.here is my code:

DataSet ds = new DataSet();

// error occurs here
us.Username = this.Session["UserId"].ToString();

ds=us.get_id(us.Username);
us.Username = txtsubscription.Text;
us.get_id(us.Username);
string lm = ds.Tables[0].Rows[0]["UserId"].ToString();


i have created class file also but getting error ovr here

Is This A Good Question/Topic? 0
  • +

Replies To: Error:system.nullreferenceexception object reference not set to an ins

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Error:system.nullreferenceexception object reference not set to an ins

Posted 19 May 2011 - 04:51 AM

There's not enough code there to really tell what's going on. Are you sure that Session["UserId"] has a value? Where is the "us" object created at?

If you step through this with the debugger it should be pretty easy to see which object is null here.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Error:system.nullreferenceexception object reference not set to an ins

Posted 19 May 2011 - 07:48 AM

ToString will throw a NullReferenceException if it's called on a null object.

If the session variable doesn't exist, the Session will return null.

Therefore, your session variable UserId is either does not exist in the session, or it's value is null.
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Error:system.nullreferenceexception object reference not set to an ins

Posted 19 May 2011 - 08:07 AM

Whatever "us" is could also be null
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1