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


Top 5 Contributors of the Month
david stephan

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

<configuration>

    <appSettings/>

    <connectionStrings/>

    <system.web>

       

        <compilation debug="false" />

       

        <authentication mode="Windows" />

       

    system.web>

configuration>

 

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

<configuration>

 

configuration>

 

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>

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.

 <configuration>

            <connectionStrings>

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

            connectionStrings>

configuration>

 

 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);

   conn.Open();

   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:

 

<configuration>

            <connectionStrings>

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

            connectionStrings>

configuration>

 

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);

   conn.Open();

 


 Subscribe to Articles

     

Further Readings:

Responses
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