Sunday , 25 June 2017
Home » C# » Database Connection in ASP.NET with C#

Database Connection in ASP.NET with C#

Database Connection in ASP.NET with C#

In this article you are going to learn how to connect into the database from ASP.NET using C#. First of all you need to create a new empty web application. In the web form add a button and grid view. You are going to load the grid view with data from database on button click. Your connection string will be stored in your web.config file of your project. In connection string you will specify the name of connection string, connection string itself and provider name. So in the web.config file type the following code.

Double click on the button to view the code and create a public string for your connection string. Since you are going to use the Configuration Manager command you need to add a using System.Configuration at the top of your code.

After you add  using System.Configuration you can create your public string variable which you will be using in your connection string.

You can name public variable whatever you want but in the connection string name you should give the same name you gave in the web.config file.

Now on button click event handler type the following code to load the data from database to the grid view.

Run the application by hitting Ctrl+F5.
When you click on your button the data is going to show in the grid view.
So this is one of many ways you can connect to your database from ASP.NET using C# code.

Video Example (Database Connection in ASP.NET with C#)

Database Connection in ASP.NET with C#

Leave a Reply

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