ASP.NET Tutorial: Using web.config to store and use connection strings

ASP.NET Tutorial: Using web.config to store and use connection strings

ASP.NET Tutorial: Using web.config to store and use connection strings

ASP.NET is a server side scripting language from Microsoft. It works with the .NET framework so can be coded in C#, VB. Now in many projects it is needed to connect to the database i.e. MSSQL Server. And typing the connection string each and every time the connection has to be made is very time consuming task.

Storing the Connection string in web.config file is the solution for the problem. And here is the guide for using web.config to store and use connectionstrings and use it whenever it is required.

The initial 3 steps include the creation of a basic ASP.NET project. You can skip these if you are making changes to your project or you already have a sample project to work on.

Step 1: Create a new Project

Click on File and then click on New Project to create a new ASP.NET project.

Add new Project

Add New Project

Name it as per your requirement. I am taking name as DatabaseDemo for the project.

Select proper template and name it

Select proper template and name it

Step 2: Add some aspx pages to the project.

Step 3: Add a new project to this existing project which will act as a database access layer to access data from database. Say the layer is named as the DataLayer

Add new Project

Add new Project

Select Class Library and name it

Select Class Library and name it

Step 4: Now open the web.config file and add following XML code to the file

name: this attribute will act as identifier for the connection string when used in the code. You can store various conncetionStrings with different names.

connectionString: this attribute in above code holds the connection string, which will be used later to make connection to databases. Place your connection string over there.

providerName: This attribute is used to inform that which type of data provider is used.

Step 5: Now you want to use the connectionString. So to do so add a reference of System.Configuration to the project. Now after adding the reference; add the using string to the code file where you want to use that string.

Add reference of System.Configuration in Class Library

Add reference of System.Configuration in Class Library



Now you can retrieve the connectionString by following code:

Now as we have created a separate project to handle the database actions; there we will create a class with static method to get the SqlConnection object whenever we want to make connection to the database. And this class will be having access to the System.Configuration for the connection strings.

Here we have named the class as DBUtility.

Now we can create a reference of DataLayer project in our main project DatabaseDemo.

Add Class Library reference to Main Project

Add Class Library reference to Main Project

And then calling the Static method of the DBUtility class directly to create the SqlConnection object.

Now we are done…We have successfully created and used ConnectionString in web.config.

Download



3 thoughts on “ASP.NET Tutorial: Using web.config to store and use connection strings

Add Comment Register



Got Something To Say:

Your email address will not be published. Required fields are marked *


*