Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hello I want to find sum of DataTable column. by using compute method.

My expression is as below :-
C#
object taxObject = tbl_Resords.Compute("Sum(SERVICE TAX)", " ");


but it throws following exception.
HTML
[System.Data.SyntaxErrorException] = {"Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier."}
Posted
Updated 28-Oct-14 0:10am
v2

Hi,

Just don't compare your INT column to an empty string!!

So use something like this:
C#
object taxObject = tbl_Resords.Compute("Sum(SERVICE TAX)", "[SERVICE TAX] > 0");

Or
C#
object taxObject = tbl_Resords.Compute("Sum(SERVICE TAX)", "[SERVICE TAX] IS NOT NULL");


Also check that the Column in your DataTable have proper DataTypes assigned

Hope this helps !! :)

Regards,
Praneet
 
Share this answer
 
v2
Comments
rhl4569 28-Oct-14 6:19am    
sir, i used this..but it gives following exception
Syntax error: Missing operand after '[SERVICE TAX]' operator.
Here two main arguments which needs to be passed to 'Compute' method of the DataTable are :-

1. First Argument - Agregate function with parameter as string - Here the function is 'Sum' but, the argument passed to it should be the DataTable column name on which we need to apply the aggregation.

2. Second argument will remain blank if you do not need to filter it as per any expression.

So if we will see here you have passed value 'SERVICE TAX' to method 'Sum' which mainly i guess is the problem here method 'Sum' will not accept it a column name which contains any white space within it.

Please pass the correct column name to get proper result.

Here is an example which worked for me :-

C#
DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                dt.Columns.Add("Name", typeof(String));
                dt.Columns.Add("Age", typeof(int));

                DataRow dr1 = dt.NewRow();
                dr1["Name"] = "name 1";
                dr1["Age"] = 20;
                dt.Rows.Add(dr1);

                DataRow dr2 = dt.NewRow();
                dr2["Name"] = "name 2";
                dr2["Age"] = 30;
                dt.Rows.Add(dr2);

                object val = dt.Compute("Sum(Age)", "");
                int agetotal = Convert.ToInt32(val);
            }


Hope this will definitely of help to you.
 
Share this answer
 
v5
Comments
SRS(The Coder) 28-Oct-14 6:45am    
If you will pass the column name as any improper value to method 'Sum' here then the same error will happen as you have mentioned in your question.
taxObject = tbl_Resords.Compute("Sum([SERVICE TAX])", " ");

this is my solution.
Thank you
 
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