ASP.NET: How to bind Repeater control using Stored Procedure?

Aman Sharma
0
Here, I will explain How to Bind repeater control in asp.net to display data using Stored Procedure. I will create stored proc in database and use it to get data from database.

Demo:

Follow these Steps:

1.     Create Table:
Create Table “Book” in database blog.
Column Name
Datatype


BookId
Int(Primary Key, Identity)
Book_name
Varchar(50)
Author
Varchar(50)
Description
Varchar(500)

2.     Create Stored Procedure:
Create proc Bind_repeater
as
Select * from book

3.     Create Connection In webconfig File:
<connectionStrings>
<add name="con" connectionString="Data Source=localhost; initial catalog=Blog; integrated Security=true"></add>
</connectionStrings>

4.     Design Web form:
   <fieldset style="width:350px"><legend>ASP.NET: Bind Repeater example</legend>
             <br />
              <asp:Repeater ID="rptBook" runat="server">
<ItemTemplate>
    <table style="background:#f2f2f2; width:100%">
        <tr style="background:#740808; color:white"><td>Book Name:</td><td><%#Eval("Book_Name") %></td></tr>
          <tr><td>Author</td><td><%#Eval("Author") %></td></tr>
          <tr><td>Description</td><td><%#Eval("Description") %></td></tr>
    </table>
    <br />
</ItemTemplate>
    </asp:Repeater></fieldset>

5.      Asp.NET Code:
Import Name Spaces:
C#: Add Following Namespaces.
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;

VB.NET: Import Following Namespaces.
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Write Code to Save and Display data In Repeater:
C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind_repeater();
        }
    }
    public void Bind_repeater()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlCommand cmd = new SqlCommand("Bind_Repeater", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter adpData = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adpData.Fill(dt);
        rptBook.DataSource = dt;
        rptBook.DataBind();
        con.Close();

    }

VB.NET
Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
If Not IsPostBack Then
                        Bind_repeater()
            End If
End Sub
Public Sub Bind_repeater()
    If con.State = ConnectionState.Closed Then
        con.Open()
    End If
    Dim cmd As New SqlCommand("Bind_Repeater", con)
    cmd.CommandType = CommandType.StoredProcedure
    Dim adpData As New SqlDataAdapter(cmd)
    Dim dt As New DataTable()
    adpData.Fill(dt)
    rptBook.DataSource = dt
    rptBook.DataBind()
    con.Close()

End Sub


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !