Krish KvR
Krish KvR

Reputation: 1054

Datatable.Compute Runtime Error

i want to get the maximum value from a column of data table!!

My data table has the values

Column1                 Date                    Column2  Column3  Column4

Electricity(KWH)    06/Jun/2013 00:00:00    99.00    43.00    56.00
Electricity(KWH)    14/Jun/2013 00:00:00    260.00   48.00    212.00

When i give

enter code here

   double a = Convert.ToDouble( dtChart.Compute("MAX(Column2)", "")); 
   variable a has value 260

But when using

         for (int i = 3; i < dtChart.Columns.Count; i++)
         {
             string a = dtChart.Columns[i].Caption; //a has value 'Column2';
             double maxYval = Convert.ToDouble(dtChart.Compute("MAX('"+a+"')",""));
         }

it shows exception as //shows error as "Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier." What to do.pls help

Upvotes: 0

Views: 458

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460108

If you want a more readable and less error-prone approach, use Linq-To-DataSet:

IEnumerable<DataRow> rows = dtChart.AsEnumerable();
double col2Max = rows.Max(r => r.Field<double>("Column2"));
double col3Max = rows.Max(r => r.Field<double>("Column3"));
double col4Max = rows.Max(r => r.Field<double>("Column4"));

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500495

I suspect it's because you're quoting it here:

"MAX('"+a+"')"

If you're trying to get just:

"MAX(Column2)"

then you need:

"MAX(" + a + ")"

Or:

string.Format("MAX({0})", a)

Upvotes: 1

Related Questions