Click here to Skip to main content
14,212,594 members
Rate this:
Please Sign up or sign in to vote.
See more:
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...
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
Wes Aday 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!!
Rate this:
Please Sign up or sign in to vote.

Solution 2

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

<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..
if(Session["username"]!=null || Session["username"]!="")
{
    lblUserName.Text = Convert.ToString(Session["username"]);

}
else

{
// cODE TO FILL SESSION VALUE
}
   
v2
Comments
ramaraog96 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
ramaraog96 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.
Rate this:
Please Sign up or sign in to vote.

Solution 3

u can put try ctach here.

try
{
objloginpl.username = Session["username"].ToString();
objloginpl.password = Session["password"].ToString();
}
catch(Exception)
{
Response.Redirect("~/Login.aspx");
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

   

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



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 10 Apr 2014
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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