-2

I have:

double genAve = Convert.ToDouble(txt_genave.Text);
OleDbCommand commandSec = new OleDbCommand();
commandSec.CommandText = "SELECT [SectionName], [Strand], [ReqGrade], [GradeLevel] FROM tbl_section";
OleDbDataAdapter daSec = new OleDbDataAdapter(commandSec);
commandSec.Connection = conn;

DataTable dt = new DataTable();
DataSet ds = new DataSet();
daSec.Fill(dt);
daSec.Fill(ds, "tbl_section");

How to convert ReqGrade to a double and compare to the genAve?

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
Paul Pastorin
  • 11
  • 1
  • 2
  • 2
    Possible duplicate of [difference between getting value from DataRow](https://stackoverflow.com/questions/7104675/difference-between-getting-value-from-datarow) – mjwills Aug 19 '17 at 13:50

1 Answers1

1

It would help to know how your DataRow is defined, but basically you'll need to access the specific value in the DataRow that you want to compare against. If the DataColumn for the corresponding cell is already defined as a double, you won't need to convert it, otherwise you'll have to convert it from whatever datatype it's defined in to double.

DataRow row = dt.Rows[0];
var value = row["ReqGrade"]; //or row[2]
double convertedValue = Convert.ToDouble(value);

if (convertedValue == genAve) Console.WriteLine("Winner!");

You could also access the value by columnIndex: var value = row[1]

In your case, the type of the value in the DataRow will match the type of the column as defined in your database, I believe. In any case, you should still be able to use Convert.ToDouble(), as there's an overload that takes in an object type.

John M. Wright
  • 4,477
  • 1
  • 43
  • 61