C # Session Summary

Source: Internet
Author: User

C # Notes on the usage of session "(string)", ". tostring ()", and "Convert. tostring" in C #

In actual operations, we often encounter converting the session value into a string to determine whether it is null or whether it has the permission to access a page, if the conversion process is improperly used, an exception is thrown, which brings a poor user experience to visitors. Here I write it as a note for your reference.

1. When session ["A"] = NULL,

Session ["A"]. tostring () throws an exception;

(String) session ["A"] is null;

Convert. tostring (session ["A"]) is "".

2. When session ["A"] =,

Their values are "".

Therefore, if ". tostring ()" is used to determine whether session ["A"] has a value, it must be written in the following format and sequence:

If (session ["A"]! = NULL & session ["A"]. tostring ()! = "")

Here, pay attention to the order of determination: first judge whether it is null, and then judge whether it is empty. If session ["A"] is null, session ["A"]! = If null is false, the. tostring () statement is not executed and no error is reported. If session ["A"] is not null, The. tostring () Statement is executed and no error is reported.

Similarly, if (session ["A"] = NULL | session ["A"]. tostring () = "") is also valid.

The format of writing with the. tostring () method is relatively fixed. If it is replaced with (string), it will be relatively free:

If (string) session ["A"]! = NULL & (string) session ["A"]! = "")

If (session ["A"]! = NULL & (string) session ["A"]! = "")

Both methods are feasible, and there is no relationship between null and the judgment sequence of empty.

The simplest method is to use convert. tostring.

If (convert. tostring (session ["AAA"]) = "")

Whether session ["A"] is null or empty, convert. tostring (session ["AAA"]) is empty.

The default session validity period is 20 minutes. The content comes from www.chinahtml.com, the Chinese Webmaster. This time indicates that after a user logs on, if there is no action, there is no request on the web. the validity period of the identity of the user is 20 minutes. If the user has been requesting operations with the web, the time is not counted as 20 minutes, that is, if a user logs on to the server for an hour, the operation will not time out unless the connection is dropped between the user and the server.
Session configuration information in the web. config file
Mode = "inproc" stateconnectionstring = "TCPIP = 127.0.0.1: 42424" sqlconnectionstring = "Data Source = 127.0.0.1; trusted_connection = yes" cookieless = "false" timeout = "20"/>

Use the session object to pass values between pages

The Session object can be used to pass values between pages. However, you must note that you cannot store too much data in the session object. Otherwise, the server will be overwhelmed. In addition, when you no longer need a seesion object, the object should be released in a timely manner.

For example, the session object is used to transmit the user's login name. On the other page, the user's login name is displayed.

The code for saving the user login name using the session object is as follows:

Session. Remove ("username"); Session ["username"] = txtname. Text; response. Redirect ("navigatepage. aspx ");

On the navigatepage. ASPX page, display the session object value on the interface. The Code is as follows:

If (session ["username"] = NULL) {response. redirect ("default. aspx ");} else {label1.text = session [" username "]. tostring ();}

Use session to verify User Logon

In ASP. net, it is very easy to use the session object to verify whether a user is logged on. For example, in this instance, when a user is logged on safely, the following code can be used to save the user's login name and jump to navigatepage. in Aspx.

If (txtname. TEXT = "Mr" & txtpassword. TEXT = "mrsoft") {session ["username"] = txtname. text. trim (); response. redirect ("navigatepage. aspx ");}

On the navigatepage. ASPX page, when the page is loaded, first determine whether there is a value in the session object. If there is a value, perform other operations; otherwise, return to the logon page. The main code is as follows:

If (session ["username"] = NULL) response. Redirect ("default. aspx"); else {// other operations}

Note: The default expiration time of the session object is 20 minutes. You can also set it in Web. config.

 

 

---------------------------------------------------------------------- Conclusion 2 --------------------------------------------------------

At present, I understand the session as a user variable stored on the server. I can set a value for the session on one page and access it on another page.

The method for attaching a session value is as follows:

Int userid = 1;
String username = "test ";
String userpwd = "e10adc3949ba59abbe56e057f20f883e"

Session ["userid"] = userid;
Session ["username"] = username;
Session ["userpwd"] = userpwd;

Or use the add method of the session.

Session. Add ("userid", userid );
Session. Add ("username", username );
Session. Add ("userpwd", userpwd );

This is a bit like creating a hash table.

Add the above Code to the page_load event in webform1.cs, and then add the following code to the page_load event of webform2.cs:

Lable1.text = session ["userid"]. tostring ();
Lable2.text = session ["username"]. tostring ();
Lable3.text = session ["userpwd"]. tostring ();

Next, first open webform1.aspx, and then open webform2.aspx, you can see the value attached to the session variable in webform1.cs.

 

C # Session Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.