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...
kieky bouvierPosted Apr 24, 2011, 2:21 PM
your code not working too if comment.Count() == 0
oh, what should i do? :(
@Vulpes...
you're right.. It really is a baffling problem..:(
Jonathas SucupiraPosted Apr 22, 2011, 12:40 PM
{
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?
Jonathas SucupiraPosted Apr 22, 2011, 12:32 PM
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();
}
}
kieky bouvierPosted Apr 22, 2011, 12:12 PM
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"
VulpesPosted Apr 22, 2011, 12:03 PM
It really is a baffling problem!
Jonathas SucupiraPosted Apr 22, 2011, 12:00 PM
VulpesPosted Apr 22, 2011, 11:58 AM
Did you try changing the method to return an int rather than a string?
kieky bouvierPosted Apr 22, 2011, 11:56 AM
Suthish : i've tried your code.. but the output was still return empty value... :(
kieky bouvierPosted Apr 22, 2011, 11:49 AM
its really really make me confused...
Jonathas SucupiraPosted Apr 22, 2011, 10:15 AM
VulpesPosted Apr 22, 2011, 9:29 AM
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";
}
}
VulpesPosted Apr 22, 2011, 7:06 AM
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.
kieky bouvierPosted Apr 22, 2011, 3:08 AM
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?)
kieky bouvierPosted Apr 22, 2011, 2:54 AM
kieky bouvierPosted Apr 22, 2011, 2:47 AM
kieky bouvierPosted Apr 22, 2011, 2:13 AM
hey.. i think it because var comment is null.. so comment.Count() does not work..
do u think it possible?
Suthish NairPosted Apr 22, 2011, 2:08 AM
VulpesPosted Apr 21, 2011, 2:28 PM
Jonathas SucupiraPosted Apr 21, 2011, 1:49 PM
Jonathas SucupiraPosted Apr 21, 2011, 1:46 PM
kieky bouvierPosted Apr 21, 2011, 1:17 PM
i dont know how to check value using breakpoint..
can u tell me that way?
VulpesPosted Apr 21, 2011, 1:10 PM
Jonathas SucupiraPosted Apr 21, 2011, 1:04 PM
I would put a break on the return line to see what the value was at that time.
kieky bouvierPosted Apr 21, 2011, 12:43 PM
yeahh..you're right.. with <%=GetCountComment(Convert.ToString(null))%> all value return "0".. :)
so, do u know what is the problem?
kieky bouvierPosted Apr 21, 2011, 12:32 PM
your code look correct.. but it can't display "0" too... :'(
kieky bouvierPosted Apr 21, 2011, 12:23 PM
and get this error :
"Preprocessor directives must appear as the first non-whitespace character on a line"
VulpesPosted Apr 21, 2011, 12:10 PM
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))%>
Jonathas SucupiraPosted Apr 21, 2011, 11:40 AM
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();
}
}
VulpesPosted Apr 21, 2011, 11:32 AM
<%=GetCountComment(Convert.ToString('<%#Eval("IdTopic")%>'))%>
EDITED: To place quotes around inner tags
kieky bouvierPosted Apr 21, 2011, 11:17 AM
but if comment.Count() > 0, it can display total coment correctly
VulpesPosted Apr 21, 2011, 10:44 AM
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 bouvierPosted Apr 21, 2011, 10:33 AM
your code can't dislay "0" too... :'(
oh, no! it make me confused...
VulpesPosted Apr 21, 2011, 10:20 AM