Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
hi am trying to change password but am getting error
object referance is not set to an instance of an object..
 
page load
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           Label3.Text = Session["LoginName"].ToString();
           Label2.Visible = false;
           Label3.Visible = true;
       }
   }
 

here is my update code..
protected void btnSave_Click(object sender, ImageClickEventArgs e)
   {
       Label3.Text = Session["USERNAME"].ToString();//this line am getting error
       objPL.lnLoginName = Session["USERNAME"].ToString();
       objPL.newPassword = TextBox3.Text;
       objBLL.ChangePassword(objPL);
       if (Convert.ToInt32(objPL.isSuccess) == 1)
       {
           string strScript = "<script>";
           strScript += "alert('Password Changed');";
       }
       else
       {
           Label2.Visible = true;
       }
 
   }
can any one suggest me??
Posted 26-May-12 1:37am
ythisbug2.1K
Comments
Technoses at 26-May-12 7:01am
   
if your problem got solved
please mark it as solution
Technoses at 29-May-12 6:40am
   
is your problem got solved
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

object referance is not set to an instance of an object..
This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

Based on above code and line, as others said, you need to handle Session["USERNAME"] before using it's .ToString() method. Check if it's not null and then only use the method. Specifically for sessions, you should always have NULL check.
  Permalink  
Comments
losmac at 26-May-12 16:33pm
   
Good answer, my 5!
Sandeep Mewara at 27-May-12 3:01am
   
Thanks.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

check this:
if(session[USERNAME]!=null)
{
//write your code here
}
and if label3 visible default value set to false, at post back, you don't set it to true!
when you find this kind of error it means you need some rest, because you don't have enough focus! this came to me too many times!Smile | :)
  Permalink  
Comments
losmac at 26-May-12 16:33pm
   
Good answer, my 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

this session not exist
Session["USERNAME"]
 
use
 
 Session["LoginName"]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 26 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100