.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to store a Global Connection String?

Posted By:Syed Shakeer Hussain       Posted Date: June 16, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

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 Crate 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"?>






        <compilation debug="false" />


        <authentication mode="Windows" />





In the above Code some text is written in green color.It had written in Comments.no 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 tag in between the <connectionStrings>


4)in 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" />




4)How you will call this web.config connection string in .aspx.cs page?

Ans)Write down the belwo lines of code:


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



 Subscribe to Articles


Further Readings:

Author: Nwe Nwe         Company URL: http://www.dotnetspark.com
Posted Date: November 23, 2012

Thanks a lot for explanation of each article in detail.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend