How to store a Global SqlConnection

Posted by in ASP.NET category on for Beginner level | Views : 10350 red flag
Rating: 5 out of 5  
 1 vote(s)

In This Article you can Know how to store a Global SqlConnection
How to store a Global Connection String?

Global Connecton String can be stored in a  web.Config .By Storing this Connection in Web.Cofig you can access this Connection String thorugh out your Application



How to Create a Web.Config file?

 1)     First  create one Defalut.asp page

 2)     Next in Menus click on WebSite-àAdd NewItem a window will open.In window select 'Web Cinfiguration File' and give the name to  Web Configuratin File and click Add Button.


When you click on Add Button your created web.config file can see in your Solution Explorer.




How to write a Connection string in Web.Config?


Open your Web.Config file (or) Double click on your web.config file.when it opens , some prewritten  XML tags you can see as follows



<?xml version="1.0"?>


    Note: As an alternative to hand editing this file you can use the

    web admin tool to configure settings for your application. Use

    the Website->Asp.Net Configuration option in Visual Studio.

    A full list of settings and comments can be found in

    machine.config.comments usually located in








            Set compilation debug="true" to insert debugging

            symbols into the compiled page. Because this

            affects performance, set this value to true only

            during development.


        <compilation debug="false" />


            The <authentication> section enables configuration

            of the security authentication mode used by

            ASP.NET to identify an incoming user.


        <authentication mode="Windows" />


            The <customErrors> section enables configuration

            of what to do if/when an unhandled error occurs

            during the execution of a request. Specifically,

            it enables developers to configure html error pages

            to be displayed in place of a error stack trace.


        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

            <error statusCode="403" redirect="NoAccess.htm" />

            <error statusCode="404" redirect="FileNotFound.htm" />






In the above Code some text is written in green color.It had written in need to worry about this.

1) Next we have to write Connection String in between the tags






2) we have to write a <connectionStrings> </connectionStrings> tags in between the <configuration> </configuration>


3) Next we have to use a <add> tag in between the <connectionStrings</connectionStrings>


4) in <add> tag we have to use attribute named connectionString and name


5) By using connectionString attribute we have to write our SqlConnection string as follows.


connectionString="Data Source=INTHIYAAZ;Initial Catalog=shakeer;User ID=sa;Password=sa"


6) To call this connection string we are giving name to this connection string by using name attribute.

The complete connection string looks as follows.




                        <add name="sqlconnectionstring" connectionString="Data  Source=INTHIYAAZ;Initial Catalog=DB;User ID=sa;Password=sa" />




Write the above lines in between the
< ?xml version="1.0"?> <appSettings/>

In the above lines I given a connection string name as name="sqlconnectionstring",and DB is my Data Base name.



How to call a Connection String in default.aspx.cs page?

The connection can be called by using


ConfigurationManager.ConnectionStrings["name of the connection given in web.cong file"].ConnectionString)


Use the above line with SqlConnection class as follows:



SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnectionstring"].ConnectionString);


   Response.Write("connection Connected sucessfully");




Here "sqlconnectionstring" is the name of the connection string used given in web.config file.



Interview question on web.config:


1)what is the use of using web.config file


Ans) By using this web.config file we can store sqlconnection string.This SqlConnection String can be acessed any where in your application.for security purpose of connection string also we can use web.config.


2) Can we have more than one web.config file in our application?


Ans) Yes,we can create or use more than one web.config file in our application.


3)How you will write a Connection String in web.Config fiel?


Ans) Then write below lines of code:




                        <add name="sqlconnectionstring" connectionString="Data  Source=INTHIYAAZ;Initial Catalog=DB;User ID=sa;Password=sa" />


Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Syed Shakeer Hussiain P
Member Level:
Member Status: Member
Member Since: 2/5/2009 3:12:18 AM
Country: India
Syed Shakeer Hussain
Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)