.NET Software Components
Home Products Services Downloads Support News Articles
Lanteria Solutions - SharePoint Design & Development
 
Home | Artciles | Web Application Settings -.NET Components
Storing of web application settings
Storing settings in Web.config
The more flexible variant of the storing web application settings is the using of Web.config file.

Configuration information for ASP.NET resources is contained in a collection of configuration files, each named Web.config. Each configuration file contains a nested hierarchy of XML tags and subtags with attributes that specify the configuration settings. Because the tags must be well-formed XML, the tags, subtags, and attributes are case-sensitive. Tag names and attribute names are camel-cased, which means that the first character of a tag name is lowercase and the first letter of any subsequent concatenated words is uppercase. Attribute values are Pascal-case, which means that the first character is uppercase and the first letter of any subsequent concatenated words is uppercase. Exceptions are true and false, which are always lowercase.

All configuration information resides between the <configuration> and </configuration> root XML tags.

There is a special <appSettings> Element in Web.config, which can be used for storing custom application settings. This is a predefined configuration section provided by the .NET Framework.

The <appSettings> element stores custom application configuration information such as database connection strings, file paths, XML Web service URLs, or any information stored in an application's .ini file. The key/value pairs specified in the <appSettings> element can be accessed in code using the System.Configuration.ConfigurationSettings or System.Configuration.AppSettingsReader classes.

Review the following example:

Web.config

<configuration>

<appSettings>
<!-- SQL Server Settings-->
<add key="SQL_SERVER_NAME" value="localhost" />
<add key="SQL_DB_NAME" value="mydb" />
<add key="SQL_DB_USERID" value="sa" />
<add key="SQL_DB_PASSWORD" value="" />
<!-- Following template can be used for tuning of SQL connection only
Use SQL_... parameters above to specify standard connection parameters
-->
<add key="SQL_CONNECTION_STRING_TEMPLATE" value="data source={SERVER_NAME};initial catalog={DB_NAME};user id={USER_ID};password={PASSWORD};workstation id={WORKSTATION_ID};packet size=4096;persist security info=True" />
</appSettings>


myConfigurator.cs

protected const String C_SQL_CONNSTR_TEMPLATE_KEY = "SQL_CONNECTION_STRING_TEMPLATE";
protected const String C_SQL_SERVER_KEY = "SQL_SERVER_NAME";
protected const String C_SQL_DB_NAME_KEY = "SQL_DB_NAME";
protected const String C_SQL_DB_USERID_KEY = "SQL_DB_USERID";
protected const String C_SQL_DB_PASSWORD_KEY = "SQL_DB_PASSWORD";

// return SQL connection string
public static void GetSQLConnectionParams(out String sqlServer,out String dbName, out String dbUserId, out String dbPassword, out String connStrTemplate)
{
AppSettingsReader ar = new AppSettingsReader();
try
{
//obtain connection string template from config file
connStrTemplate = ar.GetValue(C_SQL_CONNSTR_TEMPLATE_KEY, typeof(String)).ToString();
}
catch
{
//return empty string if error
connStrTemplate = "";
}

// obtain SQL connection parameters
sqlServer = ar.GetValue(C_SQL_SERVER_KEY, typeof(String)).ToString();
dbName = ar.GetValue(C_SQL_DB_NAME_KEY, typeof(String)).ToString();
dbUserId = ar.GetValue(C_SQL_DB_USERID_KEY, typeof(String)).ToString();
dbPassword = ar.GetValue(C_SQL_DB_PASSWORD_KEY, typeof(String)).ToString();
}



public static String GetSQLConnectionString()
{
const String C_SERVER_NAME_TEMPLATE = "{SERVER_NAME}";
const String C_DB_NAME_TEMPLATE = "{DB_NAME}";
const String C_USER_ID_TEMPLATE = "{USER_ID}";
const String C_PASSWORD_TEMPLATE = "{PASSWORD}";
const String C_WORKSTATION_ID_TEMPLATE = "{WORKSTATION_ID}";

//default template for building connection string
const String sConnStrTemplateDefault = "data source=" + C_SERVER_NAME_TEMPLATE + ";" + "initial catalog=" + C_DB_NAME_TEMPLATE + ";" + "user id=" + C_USER_ID_TEMPLATE + ";" + "password=" + C_PASSWORD_TEMPLATE + ";" + "persist security info=True;" + "workstation id=" + C_WORKSTATION_ID_TEMPLATE + ";" + "packet size=4096";
String sConnStr = "";
String sConnStrTemplate = "";
String sql_server = "";
String db_name = "";
String user_id = "";
String password = "";

GetSQLConnectionParams(out sql_server, out db_name, out user_id, out password, out sConnStrTemplate);

//use default template for connection string if it not present
if(sConnStrTemplate == null || sConnStrTemplate.Equals(""))
{
sConnStrTemplate = sConnStrTemplateDefault;
}
//build connection string
sConnStr = sConnStrTemplate.Replace(C_SERVER_NAME_TEMPLATE, sql_server);
sConnStr = sConnStr.Replace(C_DB_NAME_TEMPLATE, db_name);
sConnStr = sConnStr.Replace(C_USER_ID_TEMPLATE, user_id);
sConnStr = sConnStr.Replace(C_PASSWORD_TEMPLATE, password);
sConnStr = sConnStr.Replace(C_WORKSTATION_ID_TEMPLATE, System.Net.Dns.GetHostName());

return sConnStr;
}


Thus we can retrieve the values of our parameters by using the static methods of myConfigurator class and use these parameters in all places of our application.

SomePage.asax.cs

private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection myCnn;
SqlCommand myCmd;
SqlDataReader myReader;
myCnn = new SqlConnection(myConfigurator.GetSQLConnectionString());
myCmd = new SqlCommand("select * from countries", myCnn);
myCnn.Open();
myReader = myCmd.ExecuteReader();
// :
// Do something
// :
myCnn.Close();
}

Let's consider the advantages and disadvantages of using Web.config as a storage for web application parameters.

Advantages:

  1. Settings can be operatively changed
  2. No need to recompile whole application

Disadvantages:

  1. After changing of settings application restart occurs
  2. Additional system resources for file-based operations
  3. Anyone, who has access to Web.config file, can change settings
<<Previous ^Context Next>>
 
Aug-10-2007
New SharePoint Partners
Components 4U agreed about the strategic partnership in the area of MS SharePoint products and development.
Feb-26-2007
PAD Parser released
PAD Parser is a new component, designed for working with PAD compatible content

SharePoint Development and Design
SharePoint Directory
About Us Privacy Policy Links Contact Us Copyright © 2004-2007 Components 4U
All Rights Reserved