Click here to Skip to main content
15,889,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello please answer me
i have used a master page in which i have design a login form for administrator i used the session variable on master page when admin give right login name and password it mean i store the login name in session["admin"] variable then i access this variable in content page onload() event but when i check this variable a run time exception occur
(Object reference not set to an instance of an object.)
this is my onload() event of the content page
C#
protected void Page_Load(object sender, EventArgs e)
   {
       //TextBox admin = Master.FindControl("txtAdmin") as TextBox;
       //TextBox pass = Master.FindControl("txtPassword") as TextBox;
       //Button log = Master.FindControl("btnLogin") as Button;
       this.Title = "NU-Computer Science";
       if (IsPostBack)
       {
           if (Session["admin"].ToString().Trim() != "" || Session["admin"] != null)
           {
               pnlAdmin.Visible = true;
           }
       }
           FillRepeater();

   }

and this is my master page code

C#
protected void btnLogin_Click(object sender, EventArgs e)
    {
        Admin admin = new Admin();  // this is a class where i check the user name and password
        admin.UserName = this.txtAdmin.Text;  // set the user name property
        admin.Password = this.txtPassword.Text; // set the password property
        string user = admin.adminName();  // adminName() is a function which return user name
        if ((user.Length == 0 || user == ""))
        {
            lblError.ForeColor = System.Drawing.Color.Red;
            lblError.Text = "Wrong user or password";
        }
        else
        {
            Session["admin"] = txtAdmin.Text;
        }
    }

Please help me in this Issue
thanks in Advance
give me answer here or sent to my email <email deleted to limit spam>

Note: I know that the content pageload() event rise before the master pageload event i sure that this is the problem but tell me that what i do? with this essue
Posted
Updated 27-Apr-10 21:59pm
v3

wazir khan ahmadzai wrote:
if (Session["admin"].ToString().Trim() != "" || Session["admin"] != null)


This will always be true. Actually, it will blow up if Session["admin"] is null, because of the order you place it in, but if it were to not blow up, then if the string = "", then it won't equal null, and vice versa. Therefore, this code will either blow up, or always succeed.

The error message means the value is null, and therefore blowing up.

You should learn to use a debugger, and also read a basic book on programming because you are playing with reasonably complex concepts ( although not rocket science ) and you couldn't work out the most basic error from a clear error message. The other reply you got was also absolute nonsense.
 
Share this answer
 
hi take care of below 4 points.

1> First of all, plase check the session time out,
2> Then when u fill the session with txtAdmin.Text, convert it into string
like
Session["admin"]=txtAdmin.Text.ToString();

This second point is obviously nonsense. txtAdmin.Text IS a string

3> check that the content page containing the same master file in which you are filling the Session.

Master pages have no connection to the session, the session exists no matter which master page you use

4> estimate the time for checking the valid user, if it is more then session will expire.

If it is, your database is very broken.

thanks,
mahesh patel
 
Share this answer
 
v3

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