Using the Session Variable in DOTNET
The objective of this lecture piece is to demonstrate how to use the Session Variable with our Course Registration example to identify a specific user and pass the information to other pages. This will not be a full-fledged login system, however will demonstrate the use of Sessions and other code techniques.
Tables
The tables used for this example are the Student and PersonName Table as shown here
CREATE TABLE [dbo].[PersonName](
[NameID] [int] IDENTITY(1,1) NOT NULL,
[PersonNamePrefixText] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PersonGivenNameText] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PersonMiddleNameText] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PersonSurNameText] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PersonNameSuffixText] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_PersonName] PRIMARY KEY CLUSTERED
(
[NameID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Student](
[StudentID] [int] IDENTITY(1,1) NOT NULL,
[StudentNameID] [int] NULL,
[StudentContactInformationID] [int] NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[StudentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
The Student table has multiple constraints in the full application, in this case we are only concerned with the foreign key relationship with the PersonName table.
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [FK_Student_PersonName] FOREIGN KEY([StudentNameID])
REFERENCES [dbo].[PersonName] ([NameID])
With these two tables we are also going to create some stored procedures, the first stored procedure will answer back a full list of names of students and the associated StudentID. Note that this could also be done with a View. The other stored procedure is going to return the name of a student given the StudentID.
CREATE PROCEDURE UniqueStudents
AS
BEGIN
SELECT p.PersonSurNameText + ' ' + p.PersonGivenNameText as 'Name', S.StudentID as 'ID'
FROM Student s
INNER JOIN PersonName p ON s.StudentNameID = p.NameID
END
CREATE PROCEDURE StudentNameFromStudentID (
@StudentID int
)
AS
BEGIN
SELECT p.PersonSurNameText + ' ' + p.PersonGivenNameText as 'Name'
FROM PersonName p
WHERE p.NameID = @StudentID
END
Now that we have all these together we will create a form that will allow the user to select a student and that will also return the StudentID to be stored in a Session variable. Open your existing (or a new project) in VS 2008. You will need to drop the new stored procedures into the DataClassesStudent.dbml using the Object Explorer. There is detailed information about how to drop Tables and Stored Procedures into a project in the lecture LINQ - Using Stored Procedures and also Getting Started with LINQ.
Next step is to create a new Web FOrm On this form I am going to place a single Label and a Drop Down List (ddlSelectStudent). I will then double click into the white space of the form to bring up the Form Load. Here is the code I will use. The class that I created by using the LINQ and object explorer, and that contains references to the stored procedures is called DataClassesStudents.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dc As New DataClassesStudentsDataContext
Me.ddlSelectStudent.DataSource = dc.UniqueStudents
Me.ddlSelectStudent.DataTextField = "Name"
Me.ddlSelectStudent.DataValueField = "ID"
Me.ddlSelectStudent.DataBind()
End Sub
You should test this and see if it works. The next step is that you will need to add a Session variable when the user selects a specific student. The first requires that the AutoPostBack property of the drop down list (ddlSelectStudent) be set to True. You could do this in 2 steps using a submit button - but we are going to stay pretty simple. When the user selects a Name from the list it will trigger the SelectedIndexChanged event. You can add the StudentID to the Session variable in this code and also redirect to another screen to welcome the user. I have created a second form called Welcome.aspx with a single label on it named lblStudentName.
Items worth noting in the following code;
- Code is placed in the Page_Load. Double clicking on the page will bring up the sub that responds to the Page_Load event. This will be executed every time this page is loaded.
- The filling and binding of the drop dwon list is inside the If Not IsPostback Then. If you do not do this, every time the page is loaded it will rebind the data and the user slection will be lost. When the user selects an item it does what is called a PostBack (to the server) and reloads the form. Since you wish to keep the user selections, the code to load and bind the data to the drop down should only be executed on the first load of the form.
- SelectedIndexChanged is triggered whenever the user selects from the drop down, however for this to actually do anything the AutoPostBack property of the drop down must be set to true.
- The Response.Redirect is a preferred method of calling another page. It accepts a string argument of the page to call, though often the pages are held in variables.
The complete code from the SetStudent form is;
Partial Class SetStudent
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dc As New DataClassesStudentsDataContext
Me.ddlSelectStudent.DataSource = dc.UniqueStudents
Me.ddlSelectStudent.DataTextField = "Name"
Me.ddlSelectStudent.DataValueField = "ID"
Me.ddlSelectStudent.DataBind()
End If
End Sub
Protected Sub ddlSelectStudent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSelectStudent.SelectedIndexChanged
Session.Add("id", Me.ddlSelectStudent.SelectedValue)
Response.Redirect("Welcome.aspx")
End Sub
End Class
We will now look at the Welcome.aspx code. This is also pretty simple, it contains two Labels; lblID and lblStudentName, and the following code welcomes the student. There are really 2 things going on here, I am retrieving the id from the session variable (it should have been saved above), and I am calling the data context to use this variable. Note the long method to get the actual name - though it is stored in the return from the StudentNameFromStudentID() function - the actual object returned is NOT a string, it is a specfic type of collection. However the first item of the collection is the actual name. To access it I convert it to a list (because lists are easy to use) and then just bring back the first item of the list (indexed as 0, because indexing starts at 0).
Partial Class Welcome
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dc As New DataClassesStudentsDataContext
Dim id As Int16 = CInt(Session.Item("id"))
lblID.Text = id.ToString
lblStudentName.Text = dc.StudentNameFromStudentID(id).ToList.Item(0).Name
End Sub
End Class
Useful Lecture Links
Building a Login System Using ADO
Getting Started with LINQ
Comments (0)
You don't have permission to comment on this page.