Madelein Gerber

Madelein Gerber

  • NA
  • 5
  • 7.4k

DataTable.Compute Issue

Dec 2 2011 7:31 AM
 

Hello All,

first off I'm using Visual Studio 2010, framework 4.0

What am I trying to achieve:
well, I'm getting info from my SQL database and put it into a DataTable,  I then need to do a calculation PER ROW so I add another column to my datatable (ColumnV - datatype = decimal)

here is a sample of what my data looks like: Last Column = ColumnV


123431224ROWA6020941N9999VLIES24875.171122011800407534075940759139PKGS3536FCL4037001760282960MRKU 223 190-740FT0
12463…ROWA6045999NFLEECE
11220115407534075940759


FCL4037001760282960MRKU 223 190-740FT

 0

12483…KOSTAL IRELAND0277241AASEAT MEMORY12284.2211220116244075040752407601PAL1671.1238044079290032500MSKU 041089-140FT

 0

12493…KOSTAL IRELAND0277242AASEAT MEMORY
1122011390407504075240760



8044079490032500MSKU 041089-140FT

 0

12503…JOHNSON LAHNWERK6027134LZB STRUKTUR9377.94112201143240752407564076024PAL446428.759210367891260135088MSKU 041089-140FT14.364
12513…JOHNSON LAHNWERK6027126RZB STRUKTUR
1122011432407524075640760



210367891260135088MSKU 041089-140FT14.364


 

All good and well but now for my next challenge - I need to add another column to my datatable - and ONLY write to it once the value of the container change (3rd last column with values e.g. MSKU 041089-1)

with the below code i tried to achieve this - but NO luck

Could you please spare me some of your time and help me out with my issue:


        public System.Data.DataTable Calc(System.Data.DataTable ACTable)
        {
            string i_sGroupByColumn = "CONTAINER";

            //adding column for the row count 
            ACTable.Columns.Add("Results", typeof(decimal));

            //looping thru distinct values for the group, counting 
            foreach (DataRow dr in ACTable.Rows)
            {
                for (int i = 0; i < ACTable.Rows.Count; i++)
                {
                    System.Data.DataTable dtContainer = new System.Data.DataTable();
                    dtContainer = GetData.GetContainerSize(ACTable.Rows[i]["Type"].ToString());
                    decimal dContainerSize = Convert.ToDecimal(dtContainer.Rows[0]["Container_Size"].ToString());

                    string cExpr = "SUM(CONVERT(ColumnV, 'System.Decimal'))";
                    string cCond = i_sGroupByColumn + " = '" + dr[i_sGroupByColumn] + "'";

                    decimal dSumVal = (decimal)(ACTable.Compute(cExpr, cCond));
                    dr["Results"] = dSumVal / dContainerSize;
                }  
            } 
            return ACTable;
        }


currently its bombing out on: decimal dSumVal = (decimal)(ACTable.Compute(cExpr, cCond));
with error:

Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier.

Please please please  - i need to have this project done by 5th December :(


Answers (3)