In this tutorial we have learn How to get Connection String from Web.Config in Asp.Net C#,when you design a big software you should set the connection string for once and use it in many forms. In this method you use the name of the Connection String instead of the connection string text.
Every time you want to change the connection string just change the main connection string in the web.Config file.
By this method you don’t need to change all of the forms in your project, just change the Connection String in the web.Config.
First of all you should set the connection string in the web.Config file. For example I set the connection string for my database as you see here
<compilation debug=”true” targetFramework=”4.0″ />
<add name=”constr” connectionString=”Data Source=HOME-PC; Initial Catalog=YourDatabaseName;Integrated Security=True;” providerName=”system.data.sqlclient” />
In your forms you set references that you want to use:
Then you can get the Connection String from the App.Config by using the ConnectionStrings property.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“constr“].ConnectionString);
- Auto Refresh Partial View in ASP.NET MVC
- What is ASP.NET Core
- Difference between TempData keep() And Peek() in Asp.Net MVC
- Difference between viewbag,viewdata and tempdata in asp.net mvc
- ASP.NET MVC With AngularJS
- Retrieving Data Using Form Collection and Inserting Into ASP.Net MVC
- MVC CRUD Operations Using Entity Framework
- Search Functionality in ASP.NET MVC
- How to create a User Registration page using asp.net mvc
- Store Multiple Checkbox state from cookie using Jquery
- Cascading Dropdownlist using Ajax in Asp.Net Mvc with city state country
- Insert, Update, Delete In GridView Using ASP.Net C#
- Binding Dropdownlist With Database In Asp.Net MVC
- Search and Filter data in Gridview using Asp.net MVC
- Select Insert, Update And Delete With ASP.NET MVC
- Display Data in GridView Using Asp.net MVC
- Validation in ASP.NET MVC Razor view
- CRUD Operation Using 3-Tier Architecture In ASP.NET
- How to get Connection String from Web.Config in Asp.Net C#
- Login page using 3-Tier Architecture in ASP.Net
- Asp.Net Image Upload in 3-Tier Architecture and store in sql database