Here I will show you how to
display records from database using table control.
Program
Default.aspx
code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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 runat="server">
<title>Untitled
Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Display Records
Through table control" />
<asp:Table ID="Table1" runat="server" BackColor="#FFFF80" BorderColor="#FF8080"
BorderStyle="Solid" BorderWidth="5px" ForeColor="Red" GridLines="Both">
<asp:TableRow runat="server">
<asp:TableCell runat="server">Id</asp:TableCell>
<asp:TableCell runat="server">Name</asp:TableCell>
<asp:TableCell runat="server">Address</asp:TableCell>
<asp:TableCell runat="server">Marks</asp:TableCell>
<asp:TableCell runat="server">Year</asp:TableCell>
<asp:TableCell runat="server"></asp:TableCell>
</asp:TableRow>
</asp:Table><br />
</div>
</form>
</body>
</html>
Default.aspx.vb code
Imports
System.Data.SqlClient
Imports
System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String =
System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim sqlda As
SqlDataAdapter
Dim com As
SqlCommand
Dim ds As
DataSet
Dim dt As
DataTable
Dim str As String
Protected Sub
Button1_Click(ByVal sender
As Object,
ByVal e As
System.EventArgs) Handles Button1.Click
Dim con As New SqlConnection(strConnString)
Try
con.Open()
str =
"select * from student "
com =
New SqlCommand(str, con)
ds =
New DataSet
sqlda =
New SqlDataAdapter(com)
sqlda.Fill(ds,
"student")
dt =
ds.Tables("student")
For i = 0 To
dt.Rows.Count - 1
Dim tr As New TableRow
For j = 0 To
dt.Columns.Count - 1
Dim tc As New TableCell
tc.Text = dt.Rows(i).ItemArray(j)
tr.Cells.Add(tc)
Table1.Rows.Add(tr)
Next
Next
con.Close()
Catch ex As
Exception
Response.Write(ex.Message)
End Try
End Sub
End Class
Output
