i'm using method to count total comment in my application.
This is my code :
Comment.aspx
<%#GetCountComment(Convert.ToString(Eval("IdTopic")))%>
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();
}
}
i think my code is correct.. but it can't display "0" value.. Can u help me to fix it??
thanks before...
VulpesRank #55 · 29.2K reputationPosted Apr 21, 2011, 6:20 AM EDT
kieky bouvier57 reputationPosted Apr 21, 2011, 6:33 AM EDT
your code can't dislay "0" too... :'(
oh, no! it make me confused...
VulpesRank #55 · 29.2K reputationPosted Apr 21, 2011, 6:44 AM EDT
Also do other numbers display correctly?
If nothing's been displayed at all, I'd try changing:
<%#GetCountComment(Convert.ToString(Eval("IdTopic")))%>
to:
<%=GetCountComment(Convert.ToString(Eval("IdTopic")))%>
kieky bouvier57 reputationPosted Apr 21, 2011, 7:17 AM EDT
but if comment.Count() > 0, it can display total coment correctly
VulpesRank #55 · 29.2K reputationPosted Apr 21, 2011, 7:32 AM EDT
<%=GetCountComment(Convert.ToString('<%#Eval("IdTopic")%>'))%>
EDITED: To place quotes around inner tags
Jonathas Sucupira122 reputationPosted Apr 21, 2011, 7:40 AM EDT
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());
if (value == null || value == string.Empty) {
value = "0";
}
return value;
}
catch (Exception e)
{
return e.ToString();
}
}
VulpesRank #55 · 29.2K reputationPosted Apr 21, 2011, 8:10 AM EDT
What I am confident about is that it's nothing to do with the method.
I suspect if you try this, the "0" will be displayed:
<%=GetCountComment(Convert.ToString(null))%>
kieky bouvier57 reputationPosted Apr 21, 2011, 8:23 AM EDT
and get this error :
"Preprocessor directives must appear as the first non-whitespace character on a line"
kieky bouvier57 reputationPosted Apr 21, 2011, 8:32 AM EDT
your code look correct.. but it can't display "0" too... :'(
kieky bouvier57 reputationPosted Apr 21, 2011, 8:43 AM EDT
yeahh..you're right.. with <%=GetCountComment(Convert.ToString(null))%> all value return "0".. :)
so, do u know what is the problem?
Jonathas Sucupira122 reputationPosted Apr 21, 2011, 9:04 AM EDT
I would put a break on the return line to see what the value was at that time.
VulpesRank #55 · 29.2K reputationPosted Apr 21, 2011, 9:10 AM EDT
kieky bouvier57 reputationPosted Apr 21, 2011, 9:17 AM EDT
i dont know how to check value using breakpoint..
can u tell me that way?
Jonathas Sucupira122 reputationPosted Apr 21, 2011, 9:46 AM EDT
Jonathas Sucupira122 reputationPosted Apr 21, 2011, 9:49 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 21, 2011, 10:28 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Apr 21, 2011, 10:08 PM EDT
kieky bouvier57 reputationPosted Apr 21, 2011, 10:13 PM EDT
hey.. i think it because var comment is null.. so comment.Count() does not work..
do u think it possible?
kieky bouvier57 reputationPosted Apr 21, 2011, 10:47 PM EDT
kieky bouvier57 reputationPosted Apr 21, 2011, 10:54 PM EDT
kieky bouvier57 reputationPosted Apr 21, 2011, 11:08 PM EDT
hey, i got this
'string' does not contain a definition for 'trim' and no extension method 'trim' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
VulpesRank #55 · 29.2K reputationPosted Apr 22, 2011, 3:06 AM EDT
Regarding the "cannot convert from 'string' to 'System.IFormatProvider" error, it sounds like you've omitted to change the return type of the method from string to int. The 'N0' format only works when converting ints to strings.
VulpesRank #55 · 29.2K reputationPosted Apr 22, 2011, 5:29 AM EDT
Suppose an exception is getting thrown but, for some unknown reason, e.ToString() is returning an empty string rather than the normal error message.
Going back to your original code, kieky, try this:
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 "0";
}
}
Jonathas Sucupira122 reputationPosted Apr 22, 2011, 6:15 AM EDT
kieky bouvier57 reputationPosted Apr 22, 2011, 7:49 AM EDT
its really really make me confused...
kieky bouvier57 reputationPosted Apr 22, 2011, 7:56 AM EDT
Suthish : i've tried your code.. but the output was still return empty value... :(
VulpesRank #55 · 29.2K reputationPosted Apr 22, 2011, 7:58 AM EDT
Did you try changing the method to return an int rather than a string?
Jonathas Sucupira122 reputationPosted Apr 22, 2011, 8:00 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 22, 2011, 8:03 AM EDT
It really is a baffling problem!
kieky bouvier57 reputationPosted Apr 22, 2011, 8:12 AM EDT
this is my code :
protected void GridView_Main_DataBind(int NewPageIndex)
{
try
{
id_document = Convert.ToInt32(Request.QueryString[0]);
string username = HttpContext.Current.User.Identity.Name;
DMSDataAccess db = new DMSDataAccess()
var data = db.DMSDataContext.Topics.Select(p=>p).OrderByDescending(p=>p.CreatedOn).
Where(p=>p.IdDocument == id_document);
GridView_Main.PageSize = itemPerPage;
GridView_Main.PageIndex = NewPageIndex;
GridView_Main.DataSource = data;
GridView_Main.DataBind();
}
catch{}
}
and DataKeyNames="IdTopic"
Jonathas Sucupira122 reputationPosted Apr 22, 2011, 8:32 AM EDT
then
protected void GridBound(object sender, EventArgs e)
{
string IDTopik;
IDTopik = GridView1.DataKeys[0].ToString();
GetCountComment(IDTopik);
Response.Write("
The IdTopic received was: " + IDTopik);
}
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());
Response.Write("
The Value now is: " + value);
if (value == null || value == string.Empty) {
value = "0";
}
return value;
}
catch (Exception e)
{
Response.Write("
An Exception occured: ");
return e.ToString();
}
}
Jonathas Sucupira122 reputationPosted Apr 22, 2011, 8:40 AM EDT
{
id_document = Convert.ToInt32(Request.QueryString[0]);
string username = HttpContext.Current.User.Identity.Name;
DMSDataAccess db = new DMSDataAccess()
var data = db.DMSDataContext.Topics.Select(p=>p).OrderByDescending(p=>p.CreatedOn).
Where(p=>p.IdDocument == id_document);
GridView_Main.PageSize = itemPerPage;
GridView_Main.PageIndex = NewPageIndex;
GridView_Main.DataSource = data;
GridView_Main.DataBind();
}
catch{}
}
After looking at this in more detail, are you binding the data to the grid after you calculated the value for getcommentcount?
kieky bouvier57 reputationPosted Apr 24, 2011, 10:21 AM EDT
your code not working too if comment.Count() == 0
oh, what should i do? :(
@Vulpes...
you're right.. It really is a baffling problem..:(
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts