Abdul Qayyum
Abdul Qayyum

Reputation: 39

How to insert one empty record after every 5 records in the repeater output?

actually i am need to insert one empty record after every 5 records in the repeater output.

For example Repeater original output is following 1 Asif 2 Bilal 3 Abdul 4 Ali 5 Babar 6 Waqas 7 Asghar

Our desired output is following 1 Asif 2 Bilal 3 Abdul 4 Ali 5 Babar

6 Waqas 7 Asghar

Aspx page code is:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("CustomerName") %>'></asp:Label>
                    <br />
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>
</html>

And backend code is:

SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        protected void Page_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "Data Source=PC\\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass";
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "SELECT CustomerName FROM Customers";
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            Repeater1.DataSource = ds;
            Repeater1.DataBind();
            con.Close();
        }

Upvotes: 1

Views: 156

Answers (2)

Abdul Qayyum
Abdul Qayyum

Reputation: 39

    protected void Page_Load(object sender, EventArgs e)
                {
                    con.ConnectionString = "Data Source=PC\\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass";
                    con.Open();
                    cmd.Connection = con;
                    cmd.CommandText = "SELECT CustomerName FROM Customers";
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
    if (ds != null && ds.Tables[0].Rows.Count > 4)  
    {  
        for (int i = 1; i < ds.Tables[0].Rows.Count - 1; i++)  
        {  
            var row = ds.Tables[0].NewRow();  
            row["CustomerName"] = string.Empty;  

            if (i % 5 == 0)  
            {  
                ds.Tables[0].Rows.InsertAt(row,i);  
                ds.AcceptChanges();  
            }  
        }  
    }
Repeater1.DataSource = finalTable;
                Repeater1.DataBind();
                con.Close();
}

This piece of code helps me it achieving the desires output.

Upvotes: 0

Jasmin Patel
Jasmin Patel

Reputation: 188

        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        protected void Page_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "Data Source=PC\\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass";
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "SELECT CustomerName FROM Customers";
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);


            Repeater1.DataSource = ds;
            Repeater1.DataBind();
            con.Close();
        }

just add this code in your existing code

            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            protected void Page_Load(object sender, EventArgs e)
            {
                con.ConnectionString = "Data Source=PC\\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass";
                con.Open();
                cmd.Connection = con;
                cmd.CommandText = "SELECT CustomerName FROM Customers";
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                DataTable finalTable = new DataTable();
                if (ds.Tables.Count > 0)
                {
                    int i = 1;
                    DataTable firstTable = ds.Tables[0];
                    foreach (DataRow row in firstTable.Rows)
                    {
                        if (i == 5)
                        {
                            firstTable.NewRow();
                            i = 0;
                        }

                        finalTable.Rows.Add(row);
                        i++;
                    }
                }

                Repeater1.DataSource = finalTable;
                Repeater1.DataBind();
                con.Close();
            }

Upvotes: 3

Related Questions