Next


Web Site Configuration

• Similar to Windows Applications, in ASP.NET also, you can maintain the configuration settings in a separate file.
• As an advantage of this, the configuration settings can be modified at any time, even after installing the project on the web server also. No re-compilation is required after making necessary changes the configuration file.
• In ASP.NET web sites, the “web.config” is called as configuration file.
• When you create an ASP.NET web site, automatically it creates “web.config” file.
• Similar to “app.config” file, you can maintain the application configuration settings in <appSettings> tag in xml syntax. The <appSettings> tag can be written under <configuration> tag.
• To access the application configuration settings:
System.Configuration.ConfigurationSettings.AppSettings["key"];

Demo on “Web.config”

web config

Web.Config
  <appSettings>
      <add key="SqlServerConnectionString" value="data source=.;user id=sa;password=123;initial catalog=master"/>
      <add key="OracleConnectionString" value="provider=oraoledb.oracle.1;data source=.;user id=scott;password=tiger"/>
  </appSettings>
  
Default.aspx.cs using System.Data.SqlClient; using System.Data.OleDb; protected void btnSqlServer_Click(object sender, EventArgs e) { string cnstr = ConfigurationSettings.AppSettings["SqlServerConnectionString"]; SqlConnection cn = new SqlConnection(cnstr); cn.Open(); SqlConnection cn = new SqlConnection(cnstr); cn.Open(); Label1.Text = "Successfully Connected to Sql Server"; cn.Close(); } protected void btnOracle_Click(object sender, EventArgs e) { string cnstr = ConfigurationSettings.AppSettings["OracleConnectionString"]; OleDbConnection cn = new OleDbConnection(cnstr); cn.Open(); Label1.Text = "Successfully Connected to Oracle"; cn.Close(); }