Search code examples
c#asp.netentity-frameworklinqrepeater

Asp.net/C# :Passing Customer id to Linq query in a function


I'm trying to create a nested Repeaters.

i got my point an create the code properly by this guide in this article

http://www.aspsnippets.com/Articles/Implement-Nested-Repeater-Repeater-inside-Repeater-with-example-in-ASPNet-using-C-and-VBNet.aspx

i did it, but there's a problem to pass the customer id to a function.

i will explain it by code, my .aspx code:

<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="rptCustomers_ItemDataBound">
<HeaderTemplate>
    <table class="table table-hover table-striped table-condensed table-bordered table-responsive" >
        <tr>
            <th>

            </th>
            <th>
                Deposit Number 
            </th>
            <th>
                Custom Declaration
            </th>

              <th>Category</th>
                    <th>Location</th>
                    <th>Goods Description</th>
                    <th>Units Balance</th>
                    <th>WT Balance</th>
                    <th>Goods Balance Amount(LC)</th>
        </tr>
</HeaderTemplate>
<ItemTemplate>
    <tr>
        <td>
            <img alt="" style="cursor: pointer" src="images/plus.png" />
            <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                <asp:Repeater ID="rptOrders" runat="server">
                    <HeaderTemplate>
                        <table class="table table-hover table-striped table-condensed table-bordered table-responsive" >
                            <tr>
                                <th>
                                    Item No
                                </th>
                                <th>
                                    Item descr
                                </th>
                                <th>
                                    UOM
                                </th>
                                  <th>
                                    Balance Units
                                </th>
                                  <th>
                                    Balance LC
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                             <%# Eval("itemNo") %>
                            </td>
                            <td>
                            <%# Eval("itemDesc") %> 
                            </td>
                            <td>
                            <%# Eval("Uom") %> 
                            </td>
                             <td>
                            <%# Eval("balUnits") %> 
                            </td>
                            <td>
                            <%# Eval("balLc") %> 
                            </td>

                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
            </asp:Panel>
            <asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("depID") %>' />
        </td>
        <td>
            <%# Eval("depNo") %>
        </td>
        <td>
            <%# Eval("customDec") %> 
        </td>

          <td><%#Eval("category") %></td>
                   <td><%#Eval("location") %></td>
                   <td><%#Eval("goodDesc") %></td>
                   <td><%#Eval("unitsBal") %></td>
                   <td><%#Eval("wtBal") %></td>
                   <td><%#Eval("lcBal") %></td>  


    </tr>
</ItemTemplate>
<FooterTemplate>
    </table>
</FooterTemplate>

now this is the hidden field that will carry out the customer id:

 <asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("depID") %>' />

This is the code for the parent repeater in the page_load:

protected void Page_Load(object sender, EventArgs e)
{

    var query = (from cd in db.CDIndexes
                 join com in db.Companies on cd.cdin_CompanyId equals com.Comp_CompanyId
                 join ter in db.Territories on cd.cdin_Secterr equals ter.Terr_TerritoryID

                 where cd.cdin_Deleted == null &&
                 com.Comp_Deleted == null &&
                 cd.cdin_Status == "InProgress" &&
                 com.Comp_CompanyId == 408
                 select new
                 {
                     depID=cd.cdin_CDIndexID,
                     location = ter.Terr_Caption,
                     depNo = cd.cdin_Serial,
                     customDec = cd.cdin_Customdeclar,
                     category = cd.cdin_category,
                     goodDesc = cd.cdin_goodsDesc,
                     unitsBal = cd.cdin_RemainPackages,
                     wtBal = cd.cdin_RemainWT,
                     lcBal = cd.cdin_ActMortgageAmnt,
                 }

               ).ToList();


    rptCustomers.DataSource = query;
    rptCustomers.DataBind();
}

This is the ItemDataBound for the parent repeater repeater

protected void rptCustomers_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var queryItems = (from cd in  db.CDIndexes  
                      join com in db.Companies on cd.cdin_CompanyId equals com.Comp_CompanyId
                      join terr in db.Territories on cd.cdin_Secterr equals terr.Terr_TerritoryID
                      join gds in db.Goods on cd.cdin_CDIndexID equals gds.good_CDIndexId
                      join itms in db.Items on gds.good_ItemsID equals itms.item_ItemsID
                      join capt in db.Custom_Captions on gds.good_UOM equals capt.Capt_Code

                      where 

                        capt.Capt_Family== "good_UOM" &&
                        cd.cdin_Deleted == null &&
                        cd.cdin_Status== "InProgress" &&
                        cd.cdin_CompanyId==408 &&
                        cd.cdin_CDIndexID== 2506542 &&
                        itms.item_Deleted == null &&
                        cd.cdin_GoodsProperty=="01" &&
                        com.Comp_Deleted== null

                        select new
                        {
                            itemNo=itms.item_itemNo,
                            itemDesc=itms.item_Name,
                            Uom=capt.Capt_US,
                            balUnits=gds.good_RemainWT,
                            balLc=gds.good_BalanceAmtLC,
                        }
                      ).ToList();

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string customerId = (e.Item.FindControl("hfCustomerId") as HiddenField).Value;
        Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater;
        rptOrders.DataSource = queryItems;
        rptOrders.DataBind();
    }
}

What i want is how to pass the customer ID value to the LINQ query,specifically pass the id instead of the number(2506542) in the following statement:

cd.cdin_CDIndexID== 2506542

so i need to replace the datasource of the repeater in the following code:

  rptOrders.DataSource = queryItems;
            rptOrders.DataBind();

by a function call with parameter string customerId value As an example:

rptOrders.DataSource = function(customerId);
                rptOrders.DataBind();

what is the syntax of a function that will return a LINQ query.


Solution

  • Create a method which return items based on given id, create class which represents item of data your want to return

    public class DataItem
    {
        public string ItemNo { get; set; }
        public string ItemDesc { get; set; }
        public string Uom { get; set; }
        public string BalUnits { get; set; }
        public string BalLc { get; set; }
    }
    
    private IEnumerable<DataItem> GetItemsById(int customerId)
    {
        var queryItems = 
            from cd in  db.CDIndexes  
                join com in db.Companies on cd.cdin_CompanyId equals com.Comp_CompanyId
                join terr in db.Territories on cd.cdin_Secterr equals terr.Terr_TerritoryID
                join gds in db.Goods on cd.cdin_CDIndexID equals gds.good_CDIndexId
                join itms in db.Items on gds.good_ItemsID equals itms.item_ItemsID
                join capt in db.Custom_Captions on gds.good_UOM equals capt.Capt_Code
            where 
                capt.Capt_Family== "good_UOM" &&
                cd.cdin_Deleted == null &&
                cd.cdin_Status== "InProgress" &&
                cd.cdin_CompanyId==408 &&
                cd.cdin_CDIndexID== customerId && // Just use parameter instead of number
                itms.item_Deleted == null &&
                cd.cdin_GoodsProperty=="01" &&
                com.Comp_Deleted== null
            select new DataItem
                   {
                       ItemNo=itms.item_itemNo,
                       ItemDesc=itms.item_Name,
                       Uom=capt.Capt_US,
                       BalUnits=gds.good_RemainWT,
                       BalLc=gds.good_BalanceAmtLC,
                   };        
    }
    

    Then use this method in your RowDataBound method

    protected void rptCustomers_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || 
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string fieldValue= (e.Item.FindControl("hfCustomerId") as HiddenField).Value;
            int customerId = int.Parse(fieldValue);
    
            Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater;
            rptOrders.DataSource = GetItemsById(customerId).ToList();
            rptOrders.DataBind();
        }
    }