In my previous tutorials, I’d explained about custom asp.net repeater control with datapager, exporting gridview data to word excel text and pdf, gridview inline insert update delete and other more cracking tutorials on Repeater, Gridivew, Asp.net here.
Now here in this tutorial, I’ll explain how you can load the data and bind asp.net repeater control using ado.net executereader with sample example code in .net using c# as well as vb.net.
To explain further about how to bind asp.net repeater control using execute reader, we need to create database table to read data and bind retrieved resultset to repeater, so simply execute following script to sql query editor to create database table and then add few records manually.
Here is the script to create “SubjectDetails” table:
(
[SubjectId] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[SubjectName] [nvarchar](100) NULL,
[Marks] [nvarchar](50) NULL
)
HTML Markup To Bind Asp.net Repeater Control – [.aspx]
Following is the complete HTML Markup code that I used for this demonstration to bind asp.net repeater control for my .aspx page:
<head id=”Head1″ runat=”server”>
<title>Bind Repeater Control Example in Asp.net</title>
<style type=”text/css”>
/*Style your Repeater Control*/
#rptCustom tr td
{
text-align: center;
vertical-align: middle;
padding: 3px;
}
#rptCustom tr
{
border: 1px solid #e2e2e2;
}
.rptHeader > th
{
background: #ff6600;
font-weight: bold;
color: White;
width: 120px;
height: 28px;
}
.rptDataRows:nth-child(odd)
{
background: #f5f5f5;
}
/*Style your DataPager Control*/
.pager
{
float: left;
height: 40px;
margin-left: -5px;
margin-top: 15px;
}
.pager ul li a, .pagerbuttons, .selected
{
float: left;
font-size: 12px;
font-weight: bold;
height: 18px;
margin: 0 5px;
min-width: 20px;
padding: 5px;
text-align: center;
vertical-align: middle;
color: White;
}
.pager ul li a, .pagerbuttons
{
background: #ff6600;
}
.pager ul li a.active, .pager ul li a:hover, .selected, .pager a:hover
{
background: #000;
}
</style>
</head>
<body>
<form id=”form1″ runat=”server”>
<div style=”padding: 10px;”>
<h4>Bind Repeater Control Example in Asp.net</h4>
<asp:Repeater ID=”rptCustomRepeater” runat=”server”>
<HeaderTemplate>
<table id=”rptCustom”>
<tr class=”rptHeader”>
<th>SubjectId</th>
<th>SubjectName</th>
<th>Marks</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class=”rptDataRows”>
<td><%#DataBinder.Eval(Container.DataItem, “SubjectId”)%> </td>
<td><%#DataBinder.Eval(Container.DataItem, “SubjectName”)%> </td>
<td><%#DataBinder.Eval(Container.DataItem, “Marks”)%> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Now check the below sample code snippet to bind asp.net repeater control with executereader.
Bind Asp.net Repeater Control in C# – [.cs]
Add the following namespace that is required to connect with sql server:
After that add the following code to .aspx.cs page:
{
if (!IsPostBack)
{
BindData();
}
}
//bind subject details to repeater control
private void BindData()
{
try
{
string strConn = @”Data Source=datasource;Integrated Security=true;Initial Catalog=yourDB”;
using (SqlConnection sqlConn = new SqlConnection(strConn))
{
using (SqlCommand sqlCmd = new SqlCommand())
{
sqlCmd.CommandText = “SELECT * FROM SubjectDetails”;
sqlCmd.Connection = sqlConn;
sqlConn.Open();
SqlDataReader objDataReader = sqlCmd.ExecuteReader();
rptCustomRepeater.DataSource = objDataReader;
rptCustomRepeater.DataBind();
sqlConn.Close();
}
}
}
catch { }
}
Bind Asp.net Repeater Control in Vb.net – [.vb]
Add the following namespace that is required to connect with sql server:
After that add the following code to .aspx.vb page:
If Not IsPostBack Then
BindData()
End If
End Sub
‘bind subject details to repeater control
Private Sub BindData()
Try
‘specify your connection string here..
Dim con As String = “Data Source=datasource;Integrated Security=true;Initial Catalog=yourDB”
Using sqlConn As New SqlConnection(con)
Using sqlCmd As New SqlCommand()
sqlCmd.CommandText = “SELECT * FROM SubjectDetails”
sqlCmd.Connection = sqlConn
sqlConn.Open()
Dim objDataReader As SqlDataReader = sqlCmd.ExecuteReader()
rptCustomRepeater.DataSource = objDataReader
rptCustomRepeater.DataBind()
sqlConn.Close()
End Using
End Using
Catch
End Try
End Sub