Display record in Repeater inside DataList based on Column Names using C# in ASP.Net

RichardSa
 
on Aug 27, 2021 04:49 PM
Sample_196815.zip
582 Views

I recently learned how to display records from database in a nested repeater control. However, I tried to display it based on the Admin and Election Name, but when viewed in browser, it is not showing based on these columns

How can I display these records in a Repeater Control inside DataList Control, based on the Admin and Election Name?

Here is how my Database Table Data looks like

Id

AdminName

ElectionName

BallotQuestion

CandidateName

Shortbio

1

Richard

2021 Youth Elections

President

Peter Num

He has a vision

2

Richard

2021 Youth Elections

President

Stanton Victor

He is cool

3

Richard

2021 Youth Elections

President

Francis Duru

Very brilliant

4

Richard

2021 Youth Elections

Secretary

Linda Mike

She will be a leader

5

Tim

SUG Election

President

Queen Donald

Awesome person

6

Tim

SUG Election

President

Vivian Charles

Unique choice

7

Tim

SUG Election

Secretary

Mariam Peace

Good persona

8

Richard

P.R.O Elections

President

Dorathy Etim

Visionary

9

Richard

P.R.O Elections

President

Godwin Freeman

Perfect choice

10

Richard

P.R.O Elections

Vice President

King Anthony

Very intelligent

11

Tim

SUG Election

Vice President

Xavier Chuck

Vote him

12

Tim

SUG  Election

Vice President

Jordan Frank

Good Listener

Here is my HTML and Code

                <asp:Label ID="Adminlbl" runat="server" Text="Richard" />
                <asp:Label ID="electlbl" runat="server" Text="2021 Youth Elections" />
                <asp:DataList runat="server" ID="dlquestion" CssClass="row" CellPadding="4" OnItemDataBound="dlquestion_ItemDataBound" Width="100%">
                    <ItemTemplate>
                        <div class="card p-3 mb5 bg-white rounded" style="border: 1px solid #d1d5d4; margin: 0 auto; padding: 10px; width: 100%; border-radius: 10px;">
                            <asp:Label ID="lblQuestion" Text='<%# Eval("BallotQuestion") %>' runat="server" Font-Bold="true" Font-Names="Varela Round" Font-Size="16pt" />
                            <asp:Repeater ID="rptOptions" runat="server">
                                <HeaderTemplate></HeaderTemplate>
                                <ItemTemplate>
                                    <div runat="server" id="innerTable">
                                        <div class="" id="middleTable">
                                            <img alt="" src="images/home/lock.png" height="80" />
                                            <br />
                                            <div>
                                                <asp:Label ID="Label1" runat="server" Text="NAME: " Font-Bold="true" Font-Size="13pt"></asp:Label>
                                                <asp:Label ID="candlbl" runat="server" Text='<%# Eval("CandidateName") %>' />
                                            </div>
                                            <br />
                                            <div>
                                                <asp:Label ID="Label2" runat="server" Text="BIO: " Font-Bold="true" Font-Size="13pt"></asp:Label>
                                                <asp:Label ID="biolabel" Text='<%# Eval("Shortbio") %>' runat="server" />
                                            </div>
                                            <br />
                                            <br />
                                        </div>
                                        <div class="row">
                                            <br />
                                            <asp:Button ID="Button2" runat="server" Text="Edit" CssClass="btn btn-info" Font-Size="10pt" />
                                            <asp:Button ID="Button3" runat="server" Text="Delete" CssClass="btn btn-danger" Font-Size="10pt" />
                                        </div>
                                        <hr />
                                    </div>
                                </ItemTemplate>
                            </asp:Repeater>
                            <asp:Button ID="Button1" runat="server" Text="Add Option" CssClass="btn btn-primary" />
                        </div>
                    </ItemTemplate>
                </asp:DataList>

C#

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = GetData("SELECT DISTINCT BallotQuestion FROM Questions WHERE AdminName = '" + Adminlbl.Text + "' AND ElectionName = '" + electlbl.Text + "'");
        dlquestion.RepeatColumns = dt.Rows.Count;
        dlquestion.DataSource = dt;
        dlquestion.DataBind();
        dlquestion.RepeatColumns = 1;
        dlquestion.RepeatDirection = RepeatDirection.Vertical;
    }

    protected void dlquestion_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string BallotQuestion = (e.Item.FindControl("lblQuestion") as Label).Text;
            Repeater rptOptions = e.Item.FindControl("rptOptions") as Repeater;
            rptOptions.DataSource = GetData("SELECT CandidateName,Shortbio FROM Questions WHERE BallotQuestion = '" + BallotQuestion + "'");
            rptOptions.DataBind();
        }
    }

    private DataTable GetData(string query)
    {
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 28, 2021 03:57 AM

Hi RichardSa,

Check this example. Now please take its reference and correct your code.

Database

CREATE TABLE Election(Id INT,AdminName VARCHAR(100),ElectionName VARCHAR(100),BallotQuestion VARCHAR(100),CandidateName VARCHAR(100),Shortbio VARCHAR(100))
INSERT INTO Election VALUES(1,'Richard','2021 Youth Elections','President','Peter Num','He has a vision')
INSERT INTO Election VALUES(2,'Richard','2021 Youth Elections','President','Stanton Victor','He is cool')
INSERT INTO Election VALUES(3,'Richard','2021 Youth Elections','President','Francis Duru','Very brilliant')
INSERT INTO Election VALUES(4,'Richard','2021 Youth Elections','Secretary','Linda Mike','She will be a leader')
INSERT INTO Election VALUES(5,'Tim','SUG Election','President','Queen Donald','Awesome person')
INSERT INTO Election VALUES(6,'Tim','SUG Election','President','Vivian Charles','Unique choice')
INSERT INTO Election VALUES(7,'Tim','SUG Election','Secretary','Mariam Peace','Good persona')
INSERT INTO Election VALUES(8,'Richard','P.R.O Elections','President','Dorathy Etim','Visionary')
INSERT INTO Election VALUES(9,'Richard','P.R.O Elections','President','Godwin Freeman','Perfect choice')
INSERT INTO Election VALUES(10,'Richard','P.R.O Elections','Vice President','King Anthony','Very intelligent')
INSERT INTO Election VALUES(11,'Tim','SUG Election','Vice President','Xavier Chuck','Vote him')
INSERT INTO Election VALUES(12,'Tim','SUG Election','Vice President','Jordan Frank','Good Listener')

SELECT * FROM Election

HTML

<asp:Label ID="Adminlbl" runat="server" Text="Richard" />
<asp:Label ID="electlbl" runat="server" Text="2021 Youth Elections" />
<asp:DataList runat="server" ID="dlquestion" CssClass="row" CellPadding="4" OnItemDataBound="dlquestion_ItemDataBound" Width="100%">
    <ItemTemplate>
        <div class="card p-3 mb5 bg-white rounded" style="border: 1px solid #d1d5d4; margin: 0 auto; padding: 10px; width: 100%; border-radius: 10px;">
            <asp:Label ID="lblQuestion" Text='<%# Eval("BallotQuestion") %>' runat="server" Font-Bold="true" Font-Names="Varela Round" Font-Size="16pt" />
            <asp:Repeater ID="rptOptions" runat="server">
                <ItemTemplate>
                    <div runat="server" id="innerTable">
                        <div class="" id="middleTable">
                            <img alt="lock" src="images/home/lock.png" height="80" />
                            <br />
                            <div>
                                <asp:Label ID="Label1" runat="server" Text="NAME: " Font-Bold="true" Font-Size="13pt"></asp:Label>
                                <asp:Label ID="candlbl" runat="server" Text='<%# Eval("CandidateName") %>' />
                            </div>
                            <br />
                            <div>
                                <asp:Label ID="Label2" runat="server" Text="BIO: " Font-Bold="true" Font-Size="13pt"></asp:Label>
                                <asp:Label ID="biolabel" Text='<%# Eval("Shortbio") %>' runat="server" />
                            </div>
                            <br />
                            <br />
                        </div>
                        <div >
                            <br />
                            <asp:Button ID="Button2" runat="server" Text="Edit" CssClass="btn btn-info" Font-Size="10pt" />
                            <asp:Button ID="Button3" runat="server" Text="Delete" CssClass="btn btn-danger" Font-Size="10pt" />
                        </div>
                        <hr />
                    </div>
                </ItemTemplate>
            </asp:Repeater>
            <asp:Button ID="Button1" runat="server" Text="Add Option" CssClass="btn btn-primary" />
        </div>
    </ItemTemplate>
</asp:DataList>

Namespaces

using System.Data;
using System.Data.SqlClient;

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string query = "SELECT DISTINCT BallotQuestion FROM Election WHERE AdminName = '" + Adminlbl.Text + "' AND ElectionName = '" + electlbl.Text + "'";
        DataTable dt = GetData(query);
        dlquestion.RepeatColumns = dt.Rows.Count;
        dlquestion.DataSource = dt;
        dlquestion.DataBind();
        dlquestion.RepeatColumns = 1;
        dlquestion.RepeatDirection = RepeatDirection.Vertical;
    }
}

protected void dlquestion_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string BallotQuestion = (e.Item.FindControl("lblQuestion") as Label).Text;
        Repeater rptOptions = e.Item.FindControl("rptOptions") as Repeater;
        string query = "SELECT CandidateName, Shortbio FROM Election WHERE BallotQuestion = '" + BallotQuestion + "' AND AdminName = '" + Adminlbl.Text + "' AND ElectionName = '" + electlbl.Text + "'";
        rptOptions.DataSource = GetData(query);
        rptOptions.DataBind();
    }
}

private DataTable GetData(string query)
{
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection("Server=.;DataBase=Test;UID=sa;PWD=pass@123;"))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
                return dt;
            }
        }
    }
}

Screenshot