Introduction:
This article is helps bind gridview values
from database in datatable formats using asp.net. Page load events bind the
gridview.
Designing Page (Source view):
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title> Binding database data into GridView in ASP.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grvOutput" runat="server" AutoGenerateColumns="true">
</asp:GridView>
</div>
</form>
</body>
</html>
VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Not
Page.IsPostBack Then
Dim dtTemp As New DataTable
Dim con As New SqlConnection("server=192.168.1.100;uid=sa;pwd=sa123;database=asp_dotnet")
con.Open()
Dim cmd As New SqlCommand("SELECT * FROM studentdetails", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dtTemp)
con.Close()
grvOutput.DataSource
= dtTemp
grvOutput.DataBind()
End If
End Sub
C#.NET:
protected void Page_Load(object sender, EventArgs
e)
{
if (!Page.IsPostBack)
{
DataTable dtTemp = new DataTable();
SqlConnection con = new
SqlConnection("server=192.168.1.100;uid=sa;pwd=sa123;database=asp_dotnet");
con.Open();
SqlCommand cmd = new
SqlCommand("SELECT
* FROM studentdetails", con);
SqlDataAdapter da =new
SqlDataAdapter(cmd);
da.Fill(dtTemp);
con.Close();
grvOutput.DataSource
= dtTemp;
grvOutput.DataBind();
}
}
Demo:
0 comments:
Post a Comment