// This method is used to bind
gridview from database
protected void
BindGridview()
{
using (SqlConnection
con = new SqlConnection("Data Source=SureshDasari;Integrated
Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select
UserName,LastName,Location FROM UserInformation", con);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt=new DataTable();
dt.Load(dr);
gvUserInfo.DataSource = dt;
gvUserInfo.DataBind();
con.Close();
}
}
|
' This method is used to bind gridview from
database
Protected Sub BindGridview()
Using con As New
SqlConnection("Data
Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("Select UserName,LastName,Location FROM
UserInformation", con)
Dim dr As SqlDataReader = cmd.ExecuteReader()
Dim dt As New DataTable()
dt.Load(dr)
gvUserInfo.DataSource = dt
gvUserInfo.DataBind()
con.Close()
End Using
End Sub
|
Column Name
|
Data Type
|
Allow Nulls
|
UserName
|
varchar(50)
|
Yes
|
LastName
|
varchar(50)
|
Yes
|
Location
|
Varchar(50)
|
Yes
|
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Convert DataReader to DataTable in asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<b>Bind Gridview with datareader object</b><br /><br />
<asp:GridView ID="gvUserInfo"
runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White"/>
</asp:GridView>
</div>
</form>
</body>
</html>
|
using System;
using System.Data.SqlClient;
using System.Data;
|
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind
gridview from database
protected void
BindGridview()
{
using (SqlConnection
con = new SqlConnection("Data Source=SureshDasari;Integrated
Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select
UserName,LastName,Location FROM UserInformation", con);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt=new DataTable();
dt.Load(dr);
gvUserInfo.DataSource = dt;
gvUserInfo.DataBind();
con.Close();
}
}
|
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class ExecuteReader
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridview()
End If
End Sub
' This method is used to bind
gridview from database
Protected Sub
BindGridview()
Using con As New SqlConnection("Data
Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("Select
UserName,LastName,Location FROM UserInformation", con)
Dim dr As SqlDataReader =
cmd.ExecuteReader()
Dim dt As New DataTable()
dt.Load(dr)
gvUserInfo.DataSource = dt
gvUserInfo.DataBind()
con.Close()
End Using
End Sub
End Class
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
1 comments :
Your blog is a perfect guidance to me through which I am learning ASP.NET. On each and every post there is something to learn which is new for me.