ASP.NET: Repeater Control and its Use with example.

Aman Sharma
0
In this article I will explain How to use repeater control in asp.net to show data. I will insert data in database then display in repeater control.

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 Connection In webconfig File:
<connectionStrings>
<add name="con" connectionString="Data Source=localhost; initial catalog=Blog; integrated Security=true"></add>
</connectionStrings>
3.     Design Web form:
<center>
        <fieldset style="width:350px"><legend>ASP.NET: Repeater example</legend>
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>&nbsp;</td>
               
            </tr>
            <tr>
                <td class="auto-style2">Book Name</td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
               
            </tr>
            <tr>
                <td class="auto-style2">Author</td>
                <td>
                    <asp:TextBox ID="txtAuthor" runat="server"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
              
            </tr>
            <tr>
                <td class="auto-style2">Description</td>
                <td>
                    <asp:TextBox ID="txtDes" runat="server"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
             
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>
                    <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Submit" />
                </td>
                <td>&nbsp;</td>
              
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>&nbsp;</td>
              
            </tr>
        </table>
        <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></center>

4.      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)
    {
        Bind_repeater();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlCommand cmd = new SqlCommand("Insert into Book(Book_Name, Author, Description) values(@name,@author,@des)", con);
        cmd.Parameters.AddWithValue("@name", txtName.Text);
        cmd.Parameters.AddWithValue("@author", txtAuthor.Text);
        cmd.Parameters.AddWithValue("@des", txtDes.Text);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        clear();
        con.Close();
        Bind_repeater();
        Response.Write("<script>alert('Data Saved Successfully');</script>");
    }
    public void clear()
    {
        txtName.Text = "";
        txtAuthor.Text = "";
        txtDes.Text="";
    }
    public void Bind_repeater()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataAdapter adpData = new SqlDataAdapter("select * from Book", con);
        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)
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Bind_repeater()
End Sub
    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        Dim cmd As New SqlCommand("Insert into Book(Book_Name, Author, Description) values(@name,@author,@des)", con)
        cmd.Parameters.AddWithValue("@name", txtName.Text)
        cmd.Parameters.AddWithValue("@author", txtAuthor.Text)
        cmd.Parameters.AddWithValue("@des", txtDes.Text)
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        clear()
        con.Close()
        Bind_repeater()
        Response.Write("<script>alert('Data Saved Successfully');</script>")
    End Sub
Public Sub clear()
    txtName.Text = ""
    txtAuthor.Text = ""
    txtDes.Text = ""
End Sub
Public Sub Bind_repeater()
    If con.State = ConnectionState.Closed Then
        con.Open()
    End If
    Dim adpData As New SqlDataAdapter("select * from Book", con)
    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 !