How to Set and read ASP. NET global variables

Source: Internet
Author: User
This document describes how to use global variables in two ASP. NET projects: the Web. config file and the Global. asax file. Note the following:

Define in the web. config file
-- Settings:
In the web. the keyword key is added to the config file through the <ettings> mark, but the ettings mark is usually placed in the <system. web> ..... </system. web> mark the outside. Example:
<Configration>
<Deleetask>
<Add key = "connstring1" value = "Server = localhost; user id = sa; Pwd =; database = database name"/>
<Add key = "connstring2" value = "provider = Microsoft. Jet. oledb.4.0; Data Source = database path"/>
</Appsettings>
<System. Web>
</System. Web>
</Configration>

-- Read:
ToCodeTo reference these database connection strings, you must first add the system. configuration namespace reference, which contains the configurationsettings class, its static method configurationsettings. the deleetaskproperty can be used to obtain the web. in the config file, set the value to string. For example:

Using system. configuration;
String conn1 = configurationsettings. receivettings ["connstring1"];
String conn2 = configurationsettings. receivettings ["connstring2"];
Sqlconnection myconn1 = new sqlconnection (conn1 );
Oledbconnection myconn2 = new oledbconnection (conn2 );

In vs2005, configurationsettings. configurettings can be changed to configurationmanager. configurettings.

Defined in global. asax
-- Settings:
Method 1: use static variables defined by the Application Object in global. asax;

The Code is as follows:

<% @ Application classname = "yourname" %>

<Script language = "C #" runat = Server>

Public static float somevar = somevalue;

</SCRIPT>

Method 2: add
Protected void session_start (Object sender, eventargs E)
{
Session ["sqlconnectionstring"] = "uid = username; Pwd = password; database = mytest; server = localhost; Connect timeout = 300 ";
}

-- Read:
Application in code:
String strconnection = session ["sqlconnectionstring"]. tostring ();
Sqlconnection_1 = new sqlconnection (strconnection );

Methods other than Web. config and Global. ascx

Define static variables in a common class;
Public static int I;

Related Article

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.