hi all...i need your help..
i'm using method to count total comment in my application.

This is my code :

Comment.aspx.cs

protected String GetCountComment(string IdTopik)
    {
        try
        {
            string value = String.Empty;
            DMSDataAccess db = new DMSDataAccess();
            var comment = db.DMSDataContext.Comments.Where(p => Convert.ToString(p.IdTopic) == IdTopic);

            value = Convert.ToString(comment.Count());
            return value;
        }
        catch (Exception e)
        {
            return e.ToString();
        }
    }

Comment.aspx

<asp:TemplateField HeaderText="Comment" ItemStyle-CssClass="GridviewItem" ItemStyle-VerticalAlign="Top"
                                    ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="15%" ItemStyle-Wrap="false">
                                    <ItemTemplate>
                                        <%#GetCountComment(Convert.ToString(Eval("IdTopic")))%>
                                    </ItemTemplate>
                                    <HeaderStyle Width="15%" />
                                    <ItemStyle CssClass="GridviewItem" HorizontalAlign="Center" VerticalAlign="Top" Wrap="False" />
                                </asp:TemplateField>

i think my code is correct.. but it can't display "0" value.. Can u help me to fix it??

thanks before...

hi all...i need your help..
i'm using method to count total comment in my application.

This is my code :

Comment.aspx.cs

protected String GetCountComment(string IdTopik)
    {
        try
        {
            string value = String.Empty;
            DMSDataAccess db = new DMSDataAccess();
            var comment = db.DMSDataContext.Comments.Where(p => Convert.ToString(p.IdTopic) == IdTopic);

            value = Convert.ToString(comment.Count());
            return value;
        }
        catch (Exception e)
        {
            return e.ToString();
        }
    }

Comment.aspx

<asp:TemplateField HeaderText="Comment" ItemStyle-CssClass="GridviewItem" ItemStyle-VerticalAlign="Top"
                                    ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="15%" ItemStyle-Wrap="false">
                                    <ItemTemplate>
                                        <%#GetCountComment(Convert.ToString(Eval("IdTopic")))%>
                                    </ItemTemplate>
                                    <HeaderStyle Width="15%" />
                                    <ItemStyle CssClass="GridviewItem" HorizontalAlign="Center" VerticalAlign="Top" Wrap="False" />
                                </asp:TemplateField>

i think my code is correct.. but it can't display "0" value.. Can u help me to fix it??

thanks before...

use ArrayList

crishlay, can u tell me that way more specific?
thanks...

Why is your method type of string. You want to get a number, number of comments, so you should work and return an integer.

What represents this line of code:

var comment = db.DMSDataContext.Comments.Where(p => Convert.ToString(p.IdTopic) == IdTopic);

I see you aretrying to use Linq query over some objects. Do you get any value in the "comment" varible?
I presume that "var comment" represents an <int> object, so we can convert this to integer
if so then you can do:

private void YourMainMethod()
    {
        //get comments:
       int intComments = GetCountComment("some id");
       if(intComments == 0)
            window.alert("There is no comments found.");
    }
    
    protected int GetCountComment(string IdTopik)
    {
       string value = String.Empty;
       DMSDataAccess db = new DMSDataAccess();
       var comment = db.DMSDataContext.Comments.Where(p => Convert.ToString(p.IdTopic) == IdTopic);
       return value;       
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.