Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Session expires and redirect to login page


Message #1 by "CMY" <my62202@y...> on Tue, 8 Oct 2002 05:26:35
Hi,

Let's say I put my session to expire in 5 mins and I would like to 
redirect the user back to the login page.

Here's what I did.

In my WebForm1.aspx, I put : <meta http-equiv="refresh" content="300">
Then in,

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load

        Dim strUserName As String = CStr(Session("UserName"))

        If Not IsDBNull(Session("UserName")) Then
            Response.Redirect("login.aspx", True)
        End If

        If Not IsPostBack Then

            strConn = "data source=local;initial 
catalog=northwind;password=;user id=sa"
            conn = New SqlConnection
            ds = New DataSet()

            strSql = "select name, id from table1"
            da = New SqlDataAdapter(strSql, conn)
            da.Fill(ds)

        End If
End Sub

I tried to run my code but after 5 mins, it did not redirect to 
login.aspx. Instead it give me an error page saying I had error making a 
SQL connection.

Am I correct to check for null value for session object first before the 
post back event ? Or is it totally wrong with my coding ?
Message #2 by "ramprasad upadhyaya" <rpu_forum@y...> on Tue, 8 Oct 2002 07:11:22
Hi,

    You need not have to check dbnull. Check below code, it should work.

    If trim(Session("UserName"))="" Then
            Response.Redirect("login.aspx", True)
    End If

    Implement above,Let me know status of u r problem.


Ramprasad
=========================================================================



> Hi,

> Let's say I put my session to expire in 5 mins and I would like to 
r> edirect the user back to the login page.

> Here's what I did.

> In my WebForm1.aspx, I put : <meta http-equiv="refresh" content="300">
T> hen in,

> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
S> ystem.EventArgs) Handles MyBase.Load

>         Dim strUserName As String = CStr(Session("UserName"))

>         If Not IsDBNull(Session("UserName")) Then
 >            Response.Redirect("login.aspx", True)
 >        End If

>         If Not IsPostBack Then

>             strConn = "data source=local;initial 
c> atalog=northwind;password=;user id=sa"
 >            conn = New SqlConnection
 >            ds = New DataSet()

>             strSql = "select name, id from table1"
 >            da = New SqlDataAdapter(strSql, conn)
 >            da.Fill(ds)

>         End If
E> nd Sub

> I tried to run my code but after 5 mins, it did not redirect to 
l> ogin.aspx. Instead it give me an error page saying I had error making a 
S> QL connection.

> Am I correct to check for null value for session object first before the 
p> ost back event ? Or is it totally wrong with my coding ?
Message #3 by "CMY" <my62202@y...> on Tue, 8 Oct 2002 09:39:38
Hi,

I use :

If (Session("UserName")) Is Nothing Then
  Response.Redirect("login.aspx", True)
End If

and it works. Thanks a lot.

But there is 1 think I'm not very sure. In the web.config file, under 
sessionState, I put timeout="5".

So does this means the whole application, no matter which page we are in, 
will expire in 5 minutes ?

Then in every page header, should I put this <meta http-equiv="refresh" 
content="300"> ? And in every page, Page_Load event, I should check for 
the session expire ?

#############################
> Hi,

>     You need not have to check dbnull. Check below code, it should work.

>     If trim(Session("UserName"))="" Then
 >            Response.Redirect("login.aspx", True)
 >    End If

>     Implement above,Let me know status of u r problem.

> 
R> amprasad
=> 
========================================================================

> 

> > Hi,

> > Let's say I put my session to expire in 5 mins and I would like to 
r> > edirect the user back to the login page.

> > Here's what I did.

> > In my WebForm1.aspx, I put : <meta http-equiv="refresh" content="300">
T> > hen in,

> > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
S> > ystem.EventArgs) Handles MyBase.Load

> >         Dim strUserName As String = CStr(Session("UserName"))

> >         If Not IsDBNull(Session("UserName")) Then
 > >            Response.Redirect("login.aspx", True)
 > >        End If

> >         If Not IsPostBack Then

> >             strConn = "data source=local;initial 
c> > atalog=northwind;password=;user id=sa"
 > >            conn = New SqlConnection
 > >            ds = New DataSet()

> >             strSql = "select name, id from table1"
 > >            da = New SqlDataAdapter(strSql, conn)
 > >            da.Fill(ds)

> >         End If
E> > nd Sub

> > I tried to run my code but after 5 mins, it did not redirect to 
l> > ogin.aspx. Instead it give me an error page saying I had error 
making a 
S> > QL connection.

> > Am I correct to check for null value for session object first before 
the 
p> > ost back event ? Or is it totally wrong with my coding ?

  Return to Index