The basic concept of displaying data from a database is an essential core concept of ASP.Net. At that core, we have the GridView, which is one of the most used Table-type display devices in ASP.Net

Here, we will show the basic code needed to get this accomplished on an ASP.Net web page and since the most common database used in this scenario is SQL Server, we’ll present the code in that fashion. First, we must import the namespaces necessary to get this underway.

Imports System.Data
Imports System.Data.SQLClient
using System.Data;
using System.Data.SQLClient;

Next, we must make a connection to the database, create a SQL query and create a DataSet:

Dim sqlQuery as String="Your SQL Query Goes Here"
Dim cs As String = ConfigurationManager.ConnectionStrings
Dim ds As New DataSet()
Dim connection As New SqlConnection(cs)
Dim adapter As New SqlDataAdapter(sqlQuery, cs)
string sqlQuery = "Your SQL Query Goes Here";
string cs = ConfigurationManager.ConnectionStrings
DataSet ds = new DataSet();
SqlConnection connection = new SqlConnection(cs);
SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, cs);

This is assuming, of course, that you have your ConnectionString (“YourConString”) stored in the web.config file, using the connectionStrings section. Also – the connection strings line was split into 2 lines due to space on the page.

The Fill method of the DataAdapter populates a DataSet with the results of the query assigned to it – in our case – ‘sqlQuery’.

Then the final part is to connect this DataSet to the GridView:

GridView1.DataSource=ds (add a ';' for C# - next line also)

Now, there are some pluses of using a DataSet over a DataReader to populate your GridView. With a DataSet, which is an ‘in-memory’ representation of the data received fromthe database, you can now use Paging/Sorting/Updating/Deleting, etc). You can’t do that with a DataReader.