ASP.NET:处理session

Shivani

Introduction

Hi When I started working with this technology I faced a problem dealing with session as in any transaction or Database oriented portal this is a must requirement to deal with.

Here is a simple example showing the way to maintain session as in the first piece of code it is taking the Author first name then opening the database i am taking authors LastName and AuthorID which is there to put in session which i can access in the next Page. It will be redirected to the next Page and in the Page_Load function only i am printing the Author full name (First and Last name) and Author ID.The only thing to be taken care is give the name which are already there in the DataBase as author first name (Example Jhonson)

Source Code PutSession.aspx, GetSession.aspx

PutSession.aspx

1@ Page language="C#" enablesessionstate=true
1@ Import Namespace="System.IO" 
1@ Import Namespace="System.Text" 
1@ Import Namespace="System.Data" 
1@ Import Namespace="System.Data.SQL" 
1<script language="javascript" src="/_aspx/1.0.2204/script/WebUIValidation.js">
2</script>
 1<script language="C#" runat="server">   
 2  
 3  
 4public String Mystr;   
 5public String adminTypeID;   
 6public String associationID;   
 7public SQLDataReader myReader;   
 8  
 9  
10public void SubmitBtn_Click(Object sender, EventArgs e) {   
11  
12if (Page.IsValid) {   
13  
14SQLConnection myConnection = new SQLConnection("server=localhost;uid=sa;pwd=;database=pubs");   
15SQLCommand myCommand = new SQLCommand("Select au_lname,au_id from Authors where au_fname ='"+firstname.Text+"'", myConnection);   
16  
17try   
18{   
19myConnection.Open();   
20myCommand.Execute(out myReader);   
21Session["aufname"] = firstname.Text;   
22  
23while (myReader.Read())   
24{   
25  
26  
27Session["aulname"] = myReader["au_lname"];   
28Session["auid"] = myReader["au_id"];   
29Response.Redirect("GetSession.aspx");   
30  
31  
32}   
33  
34}   
35catch(InvalidCastException exp)   
36{   
37Response.Write(exp.ToString());   
38}   
39}   
40}   
41  
42String GetSession(String key) {   
43return Session[key].ToString();   
44}   
45  
46</script>
 1<html>
 2<title>   
 3Maintaining Session   
 4</title>
 5<body bgcolor="#CCFFFF">
 6<form action="PutSession.aspx" method="post" runat="server">
 7<center>
 8<table border="1" cellpadding="2" cellspacing="0" width="360">
 9<tr bgcolor="#eeeeee">
10<td>Hi U R First Name as Registered is(Only for those who r already there)</td>
11<td><asp:textbox id="firstname" runat="server" size="25" value=""></asp:textbox></td>
12<td> <asp:requiredfieldvalidator controltovalidate="firstname" display="Dynamic" errormessage="You must enter your name!" runat="server/"> </asp:requiredfieldvalidator></td>
13</tr>
14<td align="right">
15<asp:button onclick="SubmitBtn_Click" runat="server" text="GoGetIt" type="submit"></asp:button>
16</td></table></center>
17</form>
18</body>
19</html>

// GetSession.aspx

1@ Page language="C#" enablesessionstate=true
1@ Import Namespace="System.IO" 
1@ Import Namespace="System.Text" 
1@ Import Namespace="System.Data" 
1@ Import Namespace="System.Data.SQL" 
1<script language="javascript" src="/_aspx/1.0.2204/script/WebUIValidation.js">
2</script>
 1<script language="C#" runat="server">   
 2  
 3public string AuthorFirstName;   
 4public string AuthorLastName;   
 5public string AuthorID;   
 6  
 7  
 8public void Page_Load(Object sender, EventArgs e) {   
 9  
10if (Page.IsValid)   
11{   
12AuthorFirstName = GetSession("aufname");   
13AuthorLastName = GetSession("aulname");   
14AuthorID = GetSession("auid");   
15}   
16}   
17String GetSession(String key) {   
18return Session[key].ToString();   
19}   
20  
21</script>
1<html>
2<title>   
3Maintaining Session   
4</title>
5<body bgcolor="#CCFFFF">
6<center>
7<p>   
8Hi Welcome ```
9=AuthorFirstName

=AuthorLastName

1&lt;/p&gt;
2&lt;p&gt;   
3  
4U r Author ID is ```
5=AuthorID

</p> </center> </body> </html>

Published At
Categories with Web编程
Tagged with
comments powered by Disqus