ASP.net with Session.
for web developers...

Now days plenty of mail services are exist for an example Gmail, Hotmail, Yahoo, and AOL… etc. I am quite sure every one has an email address.

They have their own emailid with the relevant password. When they type the proper email address and password then they will get the new page/ logged in page.

Friends just copy the URL from that page and close that page, then without log in the email just paste the URL in a new page.Will you get the same page as previous logged in page???

 

You can’t get. Some times you can get the same page but when you access (Just click over any links in that page) page it won’t work properly it will go to the log out stage…

 

This task is done by the Session objects.

 

Now we will have a look on the sessions…

  

Deafult.Aspx Backend coding:< xml="true" ns="urn:schemas-microsoft-com:office:office" prefix="o" namespace="">

 

Imports System.Data

Imports System.Data.SqlClient

Imports SessionDemo

 

Partial Class _Default

    Inherits System.Web.UI.Page

 

#Region "Declare the variables"

    Dim con1 As SqlConnection

    Dim ad1 As SqlDataAdapter

    Dim ds1 As DataSet

    Dim st1 As String

#End Region

 

#Region " UI Events"

 

Protected Sub BtnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click

 

    If txtuname.Text = String.Empty Then

            Response.Write("enter the User name")

        Else

  con1 = New SqlConnection("Server=SQL-Server;Database=Master;user id=sa;password=abc123")

  con1.Open()

  ad1 = New SqlDataAdapter("select Passoword from Users where username=" & "'" & txtuname.Text & "'", con1)

  ds1 = New DataSet

  ad1.Fill(ds1, "Users")

  st1 = ds1.Tables("Users").Rows(0).Item("Passoword").ToString

  If txtpassword.Text = st1 Then

  CreateUserSession(txtuname.Text.Trim, txtpassword.Text.Trim)

                Response.Redirect("default2.aspx")

            Else

                Response.Write("error ")

            End If

        End If

      

End Sub

 

Protected Sub btnclear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnclear.Click

        txtuname.Text = ""

        txtpassword.Text = ""

        txtuname.Focus()

End Sub

 

#End Region

 

#Region "Session method"

 

Private Sub CreateUserSession(ByVal Uname As String, ByVal Passowrd As String)

        Dim userSession As New UserSession

        userSession.UserName = Uname

        userSession.Password = Passowrd

        Session.Add("UserSession", userSession)

    End Sub

 

#End Region

   

End Class

 

 

Default2.Aspx coding

 

Imports SessionDemo.UserSession

Partial Class Default2

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not SessionExist(Session("UserSession")) Then Response.Redirect("Default.aspx")

    End Sub

 

    Protected Sub btnHome_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHome.Click

        Session("UserSession") = Nothing

        Response.Redirect("Default.aspx")

    End Sub

 

    Protected Sub btnView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnView.Click

        Response.Write(TxtName.Text)

    End Sub

End Class

 

 

User Session class coding…

 

#Region "NameSpace"

Imports Microsoft.VisualBasic

#End Region

 

#Region "Session Implementation"

 

Namespace SessionDemo

 

Public Class UserSession

 

    Public UserName As String = String.Empty

    Public Password As String = String.Empty

 

Public Shared Function SessionExist(ByVal SessionObject As Object) As Boolean

     If SessionObject Is Nothing Then

        Return False

        Else

              Return True

        End If

    End Function

 

 End Class

End Namespace

#End Region

 

 

Here I am using the SQL server as a back end data base

 

1. Go to run

2.Then type: ISQLW

3.Choose your server and log in

4. Then press Ctrl+ U

5. Select the Master Data base

6. Copy the following coding and press F5

 

Create table Users

(

Username char (16) primary key,

Passoword nvarchar (20),

)

 

Insert into Users values ('Teva','abc123')