Bind Data With GridView Using SqlDataSource in Asp.Net

← PrevNext →

Accessing a remote database in .NET is relatively simple as compared to what we did during the Classic ASP era. One of .NET’s most powerful controls is the SqlDataSource, which will allows web controls to access the database and its object easily. Therefore, in this article we will show you how to bind a GridView control with SqlDataSource object.

In addition, we will see how these two controls, when used together, can produce amazing results on the web.

Therefore, to begin with, start Microsoft Visual Studio and select File -> New Web Site... from the top menu.

Choose New Website from File Menu

In the New Web Site window, give the site a name with a path and click ok.

Open the page you want to add the SqlDataSource and GridView control. Select the Toolbox from the task bar. Toolbox window will have a list of controls you want to use in your project. Find Data tab in the Toolbox and expand it. Doing so it will show you a list of Data Driven controls.

Asp.Net Toolbox

Well, so many of them. Don’t worry, we just need two controls from the list and it will be the GridView and SqlDataSource controls. Double click both the controls and it will be added on the web page. Close the Toolbox and click on the "SqlDataSource" control. Now, this control will hold the database tables which will then be used to bind with other controls on the webpage. Here, we’ll bind it with the GridView control.

Selecting the SqlDataSource will show a right arrow which will allow us to Configure Data Source, or in simple words, we will Connect to a Database. Choose the database and tables you want to work with and follow the steps. You can select particular columns in a table for the GridView to display.

Asp.Net SqlDataSource

Once you have selected the database and the table, close the wizard. We will now add the data source to the GridView. Select the GridView and click the small left arrow (see picture below). It will display the GridView Task window, where you can choose the data source. The Data Source is the one we have just created.

Bind GridView Control with SqlDataSource

That's it. We have bind GridView Control with a database table using Microsoft’s SqlDataSource control. That was really simple.

The last thing we should do is, open the web.config file and check the connection string. Even if you do not do this, it will work fine.

    <add name="DNA_ConnectionString" connectionString="Data Source=dna;
        Initial Catalog=DNA_CLASSIFIED;Integrated Security=True" 

← PreviousNext →