Avatar of doramail05
doramail05Flag for Malaysia

asked on 

count total records from another table related in datalist

i have 2 tables, one is a_category and one is article,  a_category.id = article.a_categoryid

article table contains article based on a_category table.

if article eg. c# contains 1 post, in the datalist for the  <%# DataBinder.Eval(Container.DataItem, "countacid")%>
it will show 1 post in it.
<asp:Datalist ID="dlarticle" runat="server" Height="33px" Width="600px" 
                                                    onitemdatabound="dlarticle_ItemDataBound">
                                                            <ItemTemplate>
                                                            <fieldset>
                                                            <table>
                                                                <tr>
                                                                    <td width="20px" background="images/round_rectangletopleft.png"></td>
                                                                    <td></td>
                                                                    <td width="20px" background="images/round_rectangletopright.png"></td>
                                                                </tr>
                                                                <tr>
                                                                    <td width="20px" background="images/round_rectangleleft.png"></td>
                                                                    <td>
                                                                    <div style="float:left; padding-left:10px">
                                                                    
                                                                    <%# DataBinder.Eval(Container.DataItem, "a_categoryname")  %>
                                                                    
                                                                    <asp:Label ID="lblid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "id") %>' Visible="false" />  

                                                                    
                                                                    </div>

                                                                    <div style="padding-left:100px">
                                                                    
                                                                    <%# DataBinder.Eval(Container.DataItem, "a_desc") %>
                                                                    
                                                                    

                                                                    </div>

                                                                     <div style="padding-left:150px">
                                                                    
                                                                    <%# DataBinder.Eval(Container.DataItem, "countacid")%>
                                                                    
                                                                    

                                                                    </div>


                                                                    
                                                                    <div style="float:left;padding-left:650px">
                                                                    <HeaderTemplate>
                                                                        View
                                                                    </HeaderTemplate>

                                                                    <ItemTemplate>
                        
                                                                        <asp:LinkButton ID="lbCreatePost" runat="server" 
                                                                                CommandName="CREATEPOST" CssClass="note" CommandArgument='<%#
                                                                                DataBinder.Eval(Container.DataItem, "id") %>'></asp:LinkButton>
                                                                                
                                                                                
                                                                               
                                                                    </ItemTemplate>
                                                                    </div>
                                                                    <div style="float:left;padding-left:720px;">
                                                                    <HeaderTemplate>
                                                                        Create
                                                                    </HeaderTemplate>

                                                                    <ItemTemplate>
                                                                    <asp:LinkButton ID="lbEditPost" runat="server" 
                                                                                CommandName="EDITPOST" CssClass="editnote" CommandArgument='<%#
                                                                                DataBinder.Eval(Container.DataItem, "id") %>'></asp:LinkButton>
                                                                    </ItemTemplate>
                                                                    </div>

                                                                    </td>
                                                                    <td width="20px" background="images/round_rectangleright.png"></td>
                                                                </tr>
                                                                <tr>
                                                                    <td width="20px" background="images/round_rectanglebottomleft.png"></td>
                                                                    <td></td>
                                                                    <td width="20px" background="images/round_rectanglebottomright.png"></td>
                                                                </tr>
                                                            </table>

                                                             </fieldset>
                                                           </ItemTemplate>
                                                        </asp:Datalist>

Open in new window

ASP.NETMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
doramail05
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Hi doramail05,
So, what is your exact problem? If just need to output the total records of datalist, you can try DatalistName.Rows.Count.
Avatar of doramail05
doramail05
Flag of Malaysia image

ASKER

was thinking if works this method
code-behind
-----------
 protected void dlarticle_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
         e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label _lblid = (Label)e.Item.FindControl("lblid");

            if (_lblid.Text != String.Empty)
            {
                using (SqlConnection sqlconn = new SqlConnection(strconnstring))
                {
                    DataTable dt = new DataTable();
                    string strSQL = "Select COUNT(*) as countacid from article WHERE a_categoryid = " + Convert.ToInt32(_lblid.Text);
                    SqlDataAdapter adpt = new SqlDataAdapter(strSQL, sqlconn);
                    adpt.Fill(dt);

                    if (dt.Rows.Count > 0)
                    {
                        // Display countacid in <%# DataBinder.Eval(Container.DataItem, "countacid")%>                                                              

                    }

                }
            }
        }


    }
aspx
----
<div style="padding-left:150px">
                                                                 <%# DataBinder.Eval(Container.DataItem, "countacid")%>                                                                </div>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of doramail05
doramail05
Flag of Malaysia image

ASKER

it work that way,
but unfortunately, every row's total show (eg. 1) even if that current row is 0 or NULL.  
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Try check each returned categoryid(_lblid.Text), it should give the total row respectively.
Avatar of doramail05
doramail05
Flag of Malaysia image

ASKER

not quite understand :/

the id is getting from different id from each row right?

 <asp:Label ID="lblid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "id") %>' Visible="false" />  
Avatar of doramail05
doramail05
Flag of Malaysia image

ASKER

now got it,


using (SqlConnection sqlconn = new SqlConnection(strconnstring))
            {
                DataTable dt = new DataTable();
                string strSQL = "Select COUNT(*) as countacid from article WHERE a_categoryid = " + Convert.ToInt32(_lblid.Text);
                SqlDataAdapter adpt = new SqlDataAdapter(strSQL, sqlconn);
                adpt.Fill(dt);

                if (dt.Rows.Count > 0)
                {

                    Label _lblTotalRec = (Label)e.Item.FindControl("lblTotal");//assign total record to a label called - lblTotal
                    _lblTotalRec.Text = dt.Rows[0]["countacid"].ToString();//display total record by category id
                }
               
                
            }

Open in new window

ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo