ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to display retrieved data from database in GridView?

Updated on March 5, 2015

GridView is a control that can be used for viewing the records in a tabular format. A GridView can be created using the following syntax:

<asp:gridview id="UserDetails" runat="server"></asp:gridview>

The following examples illustrates how to view data in GridView.

C#

Example 1:

using System.Data;
using System.Data.SqlClient;

namespace Registration
{
 public partial class User_Details : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
   SqlConnection connection=new SqlConnection("server=myDB;
   uid=sa;pwd=password;database=master);
   connection.Open();
   SqlCommand command=new SqlCommand("select * from 
     User_Registration",connection);
   SqlDataAdapter da=new SqlDataAdapter();
   da.SelectCommand=command;
   DataSet ds=new DataSet();
   da.Fill(ds,"User_Registration");
   UserDetails.DataSource=ds.Tables["User_Registration"].DefaultView;
   UserDetails.DataBind();
   connection.Close();
  }
 }
}

Example 2:

using System.Data;
using System.Data.SqlClient;

namespace Registration
{
 public partial class User_Details : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
   SqlConnection connection=new SqlConnection("server=myDB;
   uid=sa;pwd=password;database=master");
   connection.Open();
   SqlCommand command=new SqlCommand("select User_name,First_Name,
   Last_Name,Email_ID from User_Registration",connection);
   DataTable dt=new DataTable();
   dt.Columns.Add("User Name");
   dt.Columns.Add("First Name");
   dt.Columns.Add("Last Name");
   dt.Columns.Add("Email ID");
   DataRow dr=dt.NewRow();
   SqlDataReader dataReader=command.ExecuteReader();
     while(dataReader.Read())
      {
       dr["User Name"]=dataReader.GetValue(0).ToString();
       dr["First Name"]=dataReader.GetValue(1).ToString();
       dr["Last Name"]=dataReader.GetValue(2).ToString();
       dr["Email ID"]=dataReader.GetValue(3).ToString();
       dt.Rows.Add(dr);
       dr=dt.NewRow();
       UserDetails.DataSource=dataReader;
       UserDetails.DataBind();
      }
   dataReader.Close();
   connection.Close();
  }
 }
}

In the above programs, UserDetails is a GridView id.

VB

Example 1:

Imports System.Data
Imports System.Data.SqlClient

Partial Public Class User_Details
  Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object,
    ByVal e As System.EventArgs)Handles Me.Load

  Dim connection As SqlConnection=New SqlConnection("server=myDB;
   uid=sa;pwd=password;database=master")
  connection.Open()
  Dim command As SqlCommand=New SqlCommand("select * from  
   User_Registration",connection)
  Dim da As SqlDataAdapter=New SqlDataAdapter()
  da.SelectCommand=command
  Dim ds As DataSet=New DataSet()
  da.Fill(ds,"User_Registration")
  UserDetails.DataSource=ds.Tables("User_Registration").DefaultView
  UserDetails.DataBind()
  connection.Close()
End Sub
End Class 

Example 2:

Imports System.Data
Imports System.Data.SqlClient

Partial Public Class User_Details
   Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, 
   ByVal e As System.EventArgs)Handles Me.Load

  Dim connection As SqlConnection=New SqlConnection("server=myDB;
    uid=sa;pwd=password;database=master")
  connection.Open()
  Dim command As SqlCommand=New SqlCommand("Select 
     User_Name,First_Name,Last_Name,Email_ID from   
     User_Registration",connection)
  Dim dtable As DataTable=New DataTable()
  dtable.Columns.Add("User Name") 
  dtable.Columns.Add("First Name")
  dtable.Columns.Add("Last Name")
  dtable.Columns.Add("Email ID")
  Dim drow As DataRow=dtable.NewRow()
  Dim dataReader As SqlDataReader=command.ExecuteReader()
   While(dataReader.Read())
     drow("User Name")=dataReader.GetValue(0).ToString()
     drow("First Name")=dataReader.GetValue(1).ToString()
     drow("Last Name")=dataReader.GetValue(2).ToString()
     drow("Email ID")=dataReader.GetValue(3).ToString()
     dtable.Rows.Add(drow)
     drow=dtable.NewRow()
   End While
  UserDetails.DataSource=dtable
  UserDetails.DataBind()
  dataReader.Close()
  connection.Close()
End Sub
End Class

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article