Click here to Skip to main content
14,635,491 members
Rate this:
Please Sign up or sign in to vote.
I'm developing a hotel billing web application as my final year project. I'm using repeater control to show all the tables in one place and in that I'm using gridview which has dropdownlist to select item. Now the problem I'm facing is that I'm only able to fetch the first repeater gridview item details the second one won't yield any data.
Below is the code I'm using to fetch item details on dropdown selected index changed event and also the aspx markup: some of the screenshots showing the layout:

Click to view the layout

What I have tried:

protected void GetItemDetails(object sender, EventArgs e)
    {        
        foreach(RepeaterItem rptrItems in rptrTables.Items)
        {
            GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");
            foreach (GridViewRow row in gvItems.Rows)
            {
                DropDownList ddl = sender as DropDownList;
                Control ctrl = row.FindControl("drpItemName") as DropDownList;
                if (ctrl != null)
                {
                    DropDownList ddl1 = (DropDownList)ctrl;
                    if (ddl.ClientID == ddl1.ClientID)
                    {
                        Label UnitPrice = row.FindControl("lblUnitPrice") as Label;
                        Label QTTY = row.FindControl("lblQuantity") as Label;
                        Label UPrice = row.FindControl("lblUnitPrice") as Label;
                        Label LINETOTAL = row.FindControl("lblLineTotal") as Label;
                        SqlConnection conn = new SqlConnection(constring);
                        conn.Open();
                        if (ctrl != null)
                        {
                            if ((ddl1.ID == ddl.ID) && (ddl1.SelectedIndex == ddl.SelectedIndex))
                            {
                                string str = "select * from ItemMaster where ItemName='" + ddl1.SelectedItem.ToString() + "'";
                                SqlCommand com = new SqlCommand(str, conn);
                                SqlDataReader reader = com.ExecuteReader();
                                while (reader.Read())
                                {
                                    UnitPrice.Text = reader["UnitPrice"].ToString();
                                    QTTY.Text = reader["Quantity"].ToString(); ;
                                    decimal totamt = Convert.ToDecimal(QTTY.Text) * Convert.ToDecimal(UnitPrice.Text);
                                }
                                reader.Close();
                                conn.Close();
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "swal", "swal('Item already selected, you can increase the quantity instead!', 'Happy Cakes, Vijayapura', 'warning');", true);
                            }
                        }

                    }
                }
            }
        }              
    }





<pre lang="HTML"><pre><asp:Repeater ID="rptrTables" runat="server" OnItemDataBound="rptrTables_ItemDataBound">
                                    <ItemTemplate>
                                        <div class="col-md-6">
                                            <div class="card card-info">
                                                <div class="card-header with-border">
                                                    <h3 class="card-title"><asp:Label ID="lblTableNumber" runat="server" Text='<%# Eval("TableName") %>'></asp:Label></h3>
                                                    <div class="card-tools">
                                                        <button type="button" class="btn btn-tool" data-widget="collapse">class="fa fa-minus"></button>
                                                    </div>
                                                </div>
                                                <div class="card-body">
                                                    <div class="row">
                                                        <div class="col-md-12">
                                                            <div class="form-group">
                                                                <asp:GridView ID="grdOrder" runat="server" AutoGenerateColumns="false" AllowPaging="true" CssClass="table table-bordered table-hover table-responsive" GridLines="None" PageSize="10" OnRowDataBound="gvRowDataBound">
                                            <Columns>
                                                 <asp:CommandField ShowDeleteButton="true" ControlStyle-CssClass="btn btn-danger fa fa-trash" DeleteText="" HeaderText="Remove" />
                                                <asp:BoundField DataField="RowNumber" HeaderText="Sl. No." />
                                                <asp:TemplateField HeaderText="Item Name">
                                                    <ItemTemplate>
                                                        <asp:DropDownList ID="drpItemname" runat="server" CssClass="form-control select2" OnSelectedIndexChanged="GetItemDetails" AutoPostBack="true"></asp:DropDownList>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Price">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice")%>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Qty">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity")%>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                
                                                <asp:TemplateField HeaderText="Line Total">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblLineTotal" runat="server" Text="0"></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Add Item" ConvertEmptyStringToNull="False">
                                                <ItemTemplate>
                                                    <asp:Button ID="ButtonAdd" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="AddItem" CausesValidation="False" />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="card-footer">
                                                    <div class="pull-right">
                                <asp:Button ID="btnSubmit" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print KOT" />
                                <asp:Button ID="Button1" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print Final Bill" />
                                <asp:Button ID="Button2" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Complete Order" />
                            </div>
                                                </div>
                                            </div>
                                        </div>
                                    </ItemTemplate>
                                </asp:Repeater>
Posted
Updated 2-May-19 21:51pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Why on earth are people learning ASP.NET in 2019?

GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");



You create a for loop, then keep reading the first item.
   
Comments
Member 12801556 3-May-19 4:09am
   
That is how I'm doing it right now, please check my code above
Richard Deeming 9-May-19 14:46pm
   
What Christian is saying is that the quoted line should be replaced with:
GridView gvItems = (GridView)rptrItems.FindControl("grdOrder");
Christian Graus 3-May-19 4:19am
   
foreach(RepeaterItem rptrItems in rptrTables.Items)
{
GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");

rptrItems contains each item, but instead of using it, you read the first one in the items list, over and over

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100