0

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...

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Mitja Bonca
0

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

0

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;       
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.