Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
if (Session["username"].ToString() == null)
              {
                  Response.Redirect("~/Login.aspx");
              }
              else
              {
                  objloginpl.username = Session["username"].ToString();
                  objloginpl.password = Session["password"].ToString();
              }  


this is my page load code...
C#
protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            lblyear26.Text = DateTime.Now.ToString("yyyy");

            string strPreviousPage = "";
            if (Request.UrlReferrer != null)
            {
                strPreviousPage = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
            }
            if (strPreviousPage == "")
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                if (Session["username"].ToString() == null)
                {
                    Response.Redirect("~/Login.aspx");
                }
                else
                {
                    objloginpl.username = Session["username"].ToString();
                    objloginpl.password = Session["password"].ToString();

                    DataTable dtlogincheck1 = new DataTable();
                    dtlogincheck1 = objloginbal.GetLogindetails(objloginpl);

                    if (dtlogincheck1.Rows[0]["admin1"].ToString() == "VM" && dtlogincheck1.Rows[0]["branch"].ToString() == "Head Office")
                    {
                        lblUserName.Text = Session["username"].ToString();
                        lblLastLogin.Text = Session["lastlogin"].ToString();
                    }
                    else if (dtlogincheck1.Rows[0]["admin1"].ToString() == "VM" && dtlogincheck1.Rows[0]["branch"].ToString() != "Head Office")
                    {
                        lblUserName.Text = Session["username"].ToString();
                        lblLastLogin.Text = Session["lastlogin"].ToString();
                    }
                    else
                    {
                        Response.Redirect("~/Login.aspx");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Log(ex.Message, ex.StackTrace);
        }
    }


finally , this line gets..an exception..
lblUserName.Text = Session["username"].ToString();
like
Object reference not set to an instance of an object.
here Session["username"].ToString() is not getting any "name" when session will expired...how to solve this..please help me..
Posted
Updated 10-Apr-14 2:28am
v3
Comments
[no name] 10-Apr-14 7:29am    
Maybe you need to explain your problem a bit better. Have you tried increasing your session timeout?
Sunasara Imdadhusen 10-Apr-14 8:27am    
do not post your question or clarification code inside the comment box. please use update or improve question option!!

u can put try ctach here.

C#
try
{
objloginpl.username = Session["username"].ToString();
objloginpl.password = Session["password"].ToString();
}
catch(Exception)
{
Response.Redirect("~/Login.aspx");
}
 
Share this answer
 
By default our websites session timeout is 20 mins after that session will gets expire suppose if we want to set our custom timeout in our applications we can set it in different ways (web.config, global.asax and in IIS)

Check below methods to set session timeout in web.config, global.asax and in iis

In Web.config file we can set session timeout like as shown below

C#
<configuration>
<system.web>
 <sessionstate mode="InProc" timeout="60">
 </sessionstate>
 </system.web>
</configuration>


reference

Asp.net set session timeout.. :)[^]


Updated


if session is already expired then you can not convert it into string from .toString()
try this..
C#
if(Session["username"]!=null || Session["username"]!="")
{
    lblUserName.Text = Convert.ToString(Session["username"]);

}
else

{
// cODE TO FILL SESSION VALUE
}
 
Share this answer
 
v2
Comments
[no name] 10-Apr-14 7:57am    
your explanation is good Nirav...but i get an exception
lblUserName.Text = Session["username"].ToString(); like
Object reference not set to an instance of an object.
Nirav Prabtani 10-Apr-14 8:06am    
see updated code
[no name] 10-Apr-14 8:41am    
if (Session["username"].ToString() != null || Session["username"] != "")
get exception this line now

Object reference not set to an instance of an object
Sunasara Imdadhusen 10-Apr-14 8:29am    
this should work.
 
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