Click on the image to enlarge |
In this article I have explained with example How to Bind data from Sql server database table in Repeater data control in asp.net using both C# and Vb.Net languages.
Implementation: Let's create an asp.net web application to understand.
- First create a Database in Sql server and name it "MyDataBase". Create a table in Sql server and name it "Book_Details" as shown in fig:
- In the web.config file create the connectionstring as:
<connectionStrings>
<add name="conStr" connectionString="Data Source=LocalServer;Initial Catalog=MyDataBase;Integrated
Security=True"/>
</connectionStrings>
Note: Replace the
Data source and initial catalog(database name) as per your application.
Source Code:
Source Code:
- In the design page(.aspx) place a repeater control as:
<fieldset style="width:314px">
<legend>Repeater Example</legend>
<asp:Repeater ID="reptBook"
runat="server">
<HeaderTemplate>
<table style=" border:1px solid #c1650f; width:300px" cellpadding="0">
<tr style="background-color:#c1650f; color:White">
<td colspan="2">
<b><center>Book Details</center></b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #c1650f; width:300px" >
<tr>
<td>
<b>Book Name:</b>
<asp:Label ID="lblBookName" runat="server" Text='<%#Eval("Book_Name") %>'/>
</td>
</tr>
<tr>
<td>
<b>Author:</b>
<asp:Label ID="lblAuthor" runat="server" Text='<%#Eval("Author") %>'/>
</td>
</tr>
<tr>
<td>
<b>Publisher:</b>
<asp:Label ID="lblPublisher" runat="server" Text='<%#Eval("Publisher") %>'/>
</td>
</tr>
<tr>
<td>
<b>Price:</b>
<asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price") %>'/>
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</fieldset>
C#.NET Code to bind repeater data control from Sql server database in asp.net
- In the code behind file(.aspx.cs) write the code as:
First include the following namespaces:
using System.Data;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
then write the code as:
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeater();
}
}
protected void BindRepeater()
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString.ToString());
SqlCommand cmd = new
SqlCommand("Select
* from Book_Details", con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataTable dt = new
DataTable();
SqlDataAdapter adp = new
SqlDataAdapter(cmd);
adp.Fill(dt);
reptBook.DataSource = dt;
reptBook.DataBind();
con.Close();
}
VB.NET Code to bind repeater data control from sql server database in asp.net
- In the code behind file(.aspx.cs) write the code as:
First import the following namespaces:
Imports System.Data
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
then write the code as:
Protected Sub
Page_Load(ByVal sender As
Object, ByVal e
As System.EventArgs)
Handles Me.Load
If Not
Page.IsPostBack Then
BindRepeater()
End If
End Sub
Protected Sub BindRepeater()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr").ConnectionString.ToString())
Dim cmd As New SqlCommand("Select * from Book_Details", con)
If con.State = ConnectionState.Closed
Then
con.Open()
End If
Dim dt As New DataTable()
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(dt)
reptBook.DataSource = dt
reptBook.DataBind()
con.Close()
End Sub
Now over to you:
"If you like my work; you can appreciate by leaving your comments,
hitting Facebook like button, following on Google+, Twitter, Linked in and
Pinterest, stumbling my posts on stumble upon and subscribing for receiving
free updates directly to your inbox . Stay tuned for more technical
updates."
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..