ASP.NET Store connection string in Web.Config file

Storing connection string in web.config file give security and better performance to your website.
  • 1579
 

 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 (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.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.