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 in C#:
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 UserName,FirstName,LastName,Email 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(); } } } |
The following examples illustrates how to view data in GridView in VB:
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() con.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 UserName,FirstName,LastName,Email 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() con.Close() End Sub End Class |
No comments:
Post a Comment