Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a application with 2 master pages one for admin and one for user. I am having problem setting MasterPageFile in Page_preInit. I have tried the following:

C#
protected void LoginButton_Click(object sender, EventArgs e)
        {
            SqlConnection con=new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Login where UserName=@UserName and Password=@Password", con);
            cmd.Parameters.AddWithValue("@UserName", UserName.Text.Trim() );
            cmd.Parameters.AddWithValue("@Password", Password.Text.Trim());
                        
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
           // DataSet dt = new DataSet();
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count>0)
            {
                Session["UserName"] = UserName.Text;
                
                Session.Timeout = 10;
                if (dt.Rows[0]["SuperUser"].ToString() == "True")
                {
                    Response.Redirect("/Home.aspx");
                    
                }


                else
                {

                    Response.Redirect("/Appointment.aspx");

                }
                
               
            }
            else 
            {
                 Response.Write("Invalid username and password");
            }
            con.Close();
        }
        protected void page_PreInit(Object sender, EventArgs e)
        {
            if (Session["SuperUser"].ToString() != "true")
            {
                MasterPageFile = "~/Site1.Master";
            }
            else
            {
                MasterPageFile = "~/Site2.Master";
            }
        }

        
    }


i got error:

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 59:         protected void page_PreInit(Object sender, EventArgs e)
Line 60:         {
Line 61:             if (Session["SuperUser"].ToString() != "true")
Line 62:             {
Line 63:                 MasterPageFile = "~/Site1.Master";

Source File: C:\Users\Ashish\Documents\Visual Studio 2010\Projects\tp\tp\Account\Login.aspx.cs    Line: 61 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   tp.Login.page_PreInit(Object sender, EventArgs e) in C:\Users\Ashish\Documents\Visual Studio 2010\Projects\tp\tp\Account\Login.aspx.cs:61
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Page.OnPreInit(EventArgs e) +8871966
   System.Web.UI.Page.PerformPreInit() +31
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +328



please help me. i got stuck here.
Posted

1 solution

Actually your Session["SuperUser"] is null and you are checking Session["SuperUser"].ToString()
which is not converting the null value to ToString()

you have to manage like this:

C#
 if(Session["SuperUser"]!=null)
     {
        if (Session["SuperUser"].ToString() != "true")
          {
              MasterPageFile = "~/Site1.Master";
          }
          else
          {
              MasterPageFile = "~/Site2.Master";
          }
     }
else
{
  //do something or show Site2.Master
}
 
Share this answer
 
v3
Comments
Member 9764132 15-Apr-14 10:44am    
Thank you very much for answer and your quick reply.
Sanket Saxena 15-Apr-14 14:32pm    
Most Welcome.... :)

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