E xErcIsE 2 Storing Data in the Session Object in C#.NET

Generate QR in C#.NET E xErcIsE 2 Storing Data in the Session Object

E xErcIsE 2 Storing Data in the Session Object
Making QR Code ISO/IEC18004 In C#.NET
Using Barcode creation for .NET Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Scanning QR-Code In C#.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
In this exercise, you explore the use of the Session object.
Bar Code Generation In C#.NET
Using Barcode encoder for .NET Control to generate, create bar code image in VS .NET applications.
www.OnBarcode.com
Scan Barcode In Visual C#
Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
1. 2.
Denso QR Bar Code Printer In .NET
Using Barcode drawer for ASP.NET Control to generate, create Denso QR Bar Code image in ASP.NET applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Encoder In .NET
Using Barcode printer for .NET framework Control to generate, create QR Code 2d barcode image in .NET framework applications.
www.OnBarcode.com
Continue editing the project you created in the previous exercise. Alternatively, you can open the completed Lesson 3, Exercise 1 project in the samples installed from the CD. Open the Global.asax file. Add code to the Session_Start method to initialize a session variable named session_clicks. This variable should be set to zero when the session is first initiated. The following shows an example.
Making QR In Visual Basic .NET
Using Barcode generator for VS .NET Control to generate, create QR Code JIS X 0510 image in Visual Studio .NET applications.
www.OnBarcode.com
Matrix 2D Barcode Creation In C#
Using Barcode encoder for Visual Studio .NET Control to generate, create 2D Barcode image in .NET applications.
www.OnBarcode.com
Sample of Visual Basic Code Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Session("session_clicks") = 0 End Sub
Make Barcode In C#.NET
Using Barcode drawer for .NET framework Control to generate, create barcode image in .NET applications.
www.OnBarcode.com
ECC200 Printer In C#.NET
Using Barcode drawer for VS .NET Control to generate, create DataMatrix image in .NET applications.
www.OnBarcode.com
ChAPTER 3
Creating UCC-128 In C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create GTIN - 128 image in Visual Studio .NET applications.
www.OnBarcode.com
Paint MSI Plessey In C#
Using Barcode printer for .NET Control to generate, create MSI Plessey image in Visual Studio .NET applications.
www.OnBarcode.com
Handling Events and Managing State
EAN / UCC - 13 Maker In None
Using Barcode creation for Word Control to generate, create EAN128 image in Word applications.
www.OnBarcode.com
Recognizing GTIN - 13 In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Sample of C# Code void Session_Start(object sender, EventArgs e) { Session["session_clicks"] = 0; }
Painting EAN 128 In None
Using Barcode maker for Online Control to generate, create EAN 128 image in Online applications.
www.OnBarcode.com
Paint Code 128 In None
Using Barcode creation for Word Control to generate, create USS Code 128 image in Microsoft Word applications.
www.OnBarcode.com
3. 4.
Drawing Data Matrix ECC200 In None
Using Barcode encoder for Online Control to generate, create DataMatrix image in Online applications.
www.OnBarcode.com
Bar Code Drawer In Java
Using Barcode printer for Eclipse BIRT Control to generate, create barcode image in BIRT applications.
www.OnBarcode.com
Open Default.aspx in Source view. Add a new Label control under the existing one. Name this control Label2. Do the same for Default2.aspx. In the Page_Load method for both Default.aspx and Default2.aspx, add code to increment the number of clicks for the user s session. Also, add code to display the value in Label2. The following code shows how your Page_Load event should now look (the new code is shown in bold).
Code 128C Recognizer In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Generate Barcode In Objective-C
Using Barcode maker for iPad Control to generate, create bar code image in iPad applications.
www.OnBarcode.com
Sample of Visual Basic Code Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Application.Lock() Application("clicks") = CInt(Application("clicks")) + 1 Application.UnLock() Label1.Text = String.Format("Application clicks: {0}", _ Application("clicks").ToString()) Session("session_clicks") = CInt(Session("session_clicks")) + 1 Label2.Text = String.Format("Session clicks: {0}", _Session("session_ clicks").ToString()) End Sub Sample of C# Code protected void Page_Load(object sender, EventArgs e) { Application.Lock(); Application["clicks"] = ((int)Application["clicks"]) + 1; Application.UnLock(); Label1.Text = string.Format("Application clicks: {0}", Application["clicks"].ToString()); Session["session_clicks"] = (int)Session["session_clicks"] + 1; Label2.Text = string.Format("Session clicks: {0}", Session["session_ clicks"].ToString()); }
Build your website and visit the Default.aspx page. Click the hyperlink several times to switch between pages and verify that both the Application and Session click counters increment.
Lesson 3: Using Server-Side State Management
ChAPTER 3
From a different browser, open the same page. Notice that the Application click count includes the clicks you made from the first browser, because the Application object is shared among all user sessions. However, the Session click counter includes only clicks made from one browser. Restart your web server. If you are running locally, you can right-click the server instance in the system tray and choose Stop. If you are running in IIS, open the IIS Manager and start and stop the server. Now visit the same page again. Notice that both click counts are reset; the Application and Session objects are not persisted between application restarts.
Lesson Summary
You can use the Application collection to store information that is accessible from all webpages but is not user specific. To initialize Application variables, respond to the Application_Start event in your Global.asax file. You can use the Session collection to store user-specific information that is accessible from all webpages. To initialize Session variables, respond to the Session_Start event in your Global.asax file. You can store session information in the server s memory by using the InProc session state mode, store it in an ASP.NET State Service server by using the StateServer mode, store it in a database by using the SQLServer mode, implement your own custom session state storage by using the Custom mode, or turn session state off completely.
Lesson Review
You can use the following questions to test your knowledge of the information in Lesson 3, Using Server-Side State Management. The questions are also available on the companion CD in a practice test, if you prefer to review them in electronic form.
Note AnsWERs
Answers to these questions and explanations of why each answer is correct or incorrect are located in the Answers section at the end of the book.
You need to store state data that is accessible to any user who connects to your web application and ensure that it stays in memory. Which collection object should you use
Copyright © OnBarcode.com . All rights reserved.