Home > ASP.NET > How to bind the repeater control from the database in Asp.net



How to bind the repeater control from the database in Asp.net

In this article we have described how to bind the repeater control from the database in Asp.net


In this article, we have described with an example code, how to bind the repeater control from the database in Asp.net. You can have a look at it and learn.

 

About the repeater control

The repeater is a data-bound control in asp.net.

Repeater is a very popular and useful control in C#. We can specify the table and customize the user interface.

Repeater automatically binds to and displays the data from a data source control.

 

Required Namespaces

You will need to use the following namespaces.

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

 

aspx page

We have used the repeater control below. You can have a look at it and learn.

<html>

<head runat="server">

    <title></title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

</head>

<body>

    <form id="form1" runat="server">

        <div class="form-row">

            <div class="form-group col-md-2">

                <label for="inputName">Name</label>

                <asp:TextBox CssClass="form-control" ID="TextBox1" runat="server" />

            </div>

            <div class="form-group col-md-2">

                <label for="InputRole">Role</label>

                <asp:DropDownList CssClass="form-control" ID="ddlRole" runat="server">

                    <asp:ListItem Value="0" Text="Select" />

                    <asp:ListItem Value="1" Text="Admin" />

                    <asp:ListItem Value="2" Text="Vice-Admin" />

                    <asp:ListItem Value="3" Text="Member" />

                </asp:DropDownList>

            </div>

        </div>

        <br />

        <asp:Button Text="Submit" CssClass="btn btn-success" runat="server" OnClick="OnSubmit" />

        <br />

        <asp:Repeater ID="rptUserinfo" runat="server">

            <HeaderTemplate>

                <table id="dataTablesExample" class="table">

                    <tr>

                        <th scope="col" style="width: 80px">Id

                        </th>

                        <th scope="col" style="width: 120px">Name

                        </th>

                        <th scope="col" style="width: 100px">Role

                        </th>

                    </tr>

            </HeaderTemplate>

            <ItemTemplate>

                <tr>

                    <td><span><%# Eval("userid")%></span>  </td>

                    <td><span><%# Eval("name")%></span></span>  </td>

                    <td><span><%# Eval("role")%></span></td>

                </tr>

            </ItemTemplate>

            <FooterTemplate>

                </table>

            </FooterTemplate>

        </asp:Repeater>

    </form>

</body>

</html>

 

C# code

We have explained below C# code with examples of some user information in this article .You can have a look at it and learn.

protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

 

            this.GetData();

        }

    }

    private void GetData()

    {

        string config = Convert.ToString(ConfigurationManager.ConnectionStrings["myconnection"]);

        SqlConnection con = new SqlConnection(config);

        SqlCommand cmd = new SqlCommand("select * from  users", con);

        SqlDataAdapter sda = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        sda.Fill(ds);

        rptUserinfo.DataSource = ds;

        rptUserinfo.DataBind();

    }

 

    protected void OnSubmit(object sender, EventArgs e)

    {

        string config = Convert.ToString(ConfigurationManager.ConnectionStrings["myconnection"]);

        SqlConnection con = new SqlConnection(config);

        con.Open();

        using (SqlCommand cmd = new SqlCommand("insert into users values(@Name,@Role)", con))

        {

            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;

            cmd.Parameters.Add("@Role", SqlDbType.VarChar).Value = ddlRole.SelectedItem.Text;

            cmd.ExecuteNonQuery();

        }

        con.Close();

        this.GetData();

    }



Note: All contents are copyright of their authors.



Comments



Leave a Reply

Comment:*
Email ID:*


Name:*