Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi am trying to change password but am getting error
object referance is not set to an instance of an object..

page load
C#
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..
C#
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
Comments
Technoses 26-May-12 7:01am    
if your problem got solved
please mark it as solution
Technoses 29-May-12 6:40am    
is your problem got solved

check this:
C#
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!:)
 
Share this answer
 
Comments
Maciej Los 26-May-12 16:33pm    
Good answer, my 5!
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.
 
Share this answer
 
Comments
Maciej Los 26-May-12 16:33pm    
Good answer, my 5!
Sandeep Mewara 27-May-12 3:01am    
Thanks.
this session not exist
C#
Session["USERNAME"]


use

C#
Session["LoginName"]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900