ARTICLE

Storing connection string in Web.Config file

Posted by Gurjeet Singh Articles | ASP.NET using VB.NET June 20, 2011
Storing connection string in web.config file give security and better performance to your website.
Download Files:
 
Reader Level:

Storing connection string in web.config file gives many advantages.

Mostly many of people write the connection string in each page. It is not a good programming  technique, as the bellow code.

Default.aspx

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="UseOfConfigFile.aspx.cs" Inherits="UseOfConfigFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"></asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
             ConnectionString="Data Source=MCNDESKTOP10\SQLEXPRESS;Database=Master;Integrated Security=True"
            SelectCommand="SELECT * FROM All_Detail"  >         
        </asp:SqlDataSource>
     </div>
     </form>
</body>
</html>

First, It give security to your website from hacker because no one can able to view source code, that is way we save our session in web config file and web config file also help in error handling.

Second, you no need to add long connection string in each page. You need to just add the name of connection string into SqlDataSource control. Suppose if you many pages you want to change username or any other changes in connectionStrings you need not to change username from every page if you have added connection string in web.config file, only change made in web.config file automatically all pages get new changes.

Default.aspx

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="UseOfConfigFile.aspx.cs" Inherits="UseOfConfigFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
                <asp:BoundField DataField="Emp_fname" HeaderText="Emp_fname" SortExpression="Emp_fname" />
                <asp:BoundField DataField="Emp_lname" HeaderText="Emp_lname" SortExpression="Emp_lname" />
                <asp:BoundField DataField="Emp_country" HeaderText="Emp_country" SortExpression="Emp_country" />
                <asp:BoundField DataField="emp_salary" HeaderText="emp_salary"  SortExpression="emp_salary" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT * FROM All_Detail"
            ConnectionString="<%$ ConnectionStrings:Master %>">
        </asp:SqlDataSource>
     </div>
     </form>
</body>
</html>

Web.config

<?
xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="Master" connectionString="Data Source=MCNDESKTOP10\SQLEXPRESS;Initial Catalog=master;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

Output:

Untitled-1.gif

Here is master name used in Web.config file in connectionStrings and this name used by ConnectionString property of SqlDataSource in Default.aspx, see the Default.aspx code.

Third, It improve the performance of data access because same connection string used in all page it defeating the benefit of connection polling. In connection polling when connection is needed each time character by character match is made.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor