Click here to Skip to main content
15,910,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to use compute method like that

C#
object sumObject;
        sumObject = dtbasket.Compute("Sum(total)", "");
        lbltotal.Text = (sumObject.ToString());



but it gives me this error
Invalid usage of aggregate function Sum() and Type: String.

Please could any one help me on this or give me another solution???

thanx in advance.
Posted

The likeliest reason is that the column total in your data table is defined with a data type that cannot be summed. For example if total is a string field, it cannot be summed. Check the underlying data type and if needed, change it to proper data type.
 
Share this answer
 
Comments
Sarah Mohammed77 25-May-12 16:49pm    
I MAKE IT decimal now it give me this error
Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier.
Wendelius 25-May-12 17:21pm    
Can you post the code
Sarah Mohammed77 25-May-12 17:22pm    
decimal total=Convert.ToDecimal (dtbasket.Rows[i]["total"]);
decimal sumObject;
sumObject = Convert.ToDecimal(dtbasket.Compute("Sum(" + total + ")", " "));
lbltotal.Text = (sumObject.ToString());
Wendelius 25-May-12 17:35pm    
Ok, What I meant is that your data table definition most likely isn't using a type that can be summed.

Have a look at the definition for the columns in your data table. The column named total should be defined as decimal (if that's the actual data type).

Refer to DataColumn Constructor (String, Type)[^] and DataColumnCollection.Add Method (String, Type)[^]
compute method not work on string datatype. if you don't want to change datatype of your field than in query you can write like that:-

SQL
select  cast(isnull(FieldName,0)as  decimal) from TableName

than you can use Compute. it will not give any error. but if in your total field you have stored non numeric value it will not work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900