12

I have a datarow, but how can i convert it to an int ?

I tried this, but it doesn't work.

dsMovie = (DataSet)wsMovie.getKlantId();
            tabel = dsMovie.Tables["tbl_klanten"];
            eersteRij = tabel.Rows[0];
(Int32.Parse(eersteRij.ToString())
Steaphann
  • 121
  • 1
  • 1
  • 6

6 Answers6

17

A DataRow is an object; it is not an integer. A DataRow contains one or more columns of data. You can index into the DataRow to access the values of these coulmns.

If table tbl_klanten contains one column, and that column is an integer, you can do the following:

var myInt = (int)eersteRij[0];

if the column is a string containing the value of an integer,

var myInt = int.Parse(eersteRij[0]);

If the column is named klant_id...

var myInt = (int)eersteRij["klant_id"];

if the column is a string containing the value of an integer,

var myInt = int.Parse(eersteRij["klant_id"]);
  • No tbl_klanten contains more columns. but with dsMovie = (DataSet)wsMovie.getKlantId(); i only put the klant_id's in tabel. – Steaphann Mar 02 '11 at 14:19
  • 1
    @Steaphann sorry, the language barrier is hurting me. Where is the klant_id? What table, what column? –  Mar 02 '11 at 14:23
  • the table is tbl_klanten and the column is klant_id . – Steaphann Mar 02 '11 at 14:54
13

Simply use the Field method:

int result = row.Field<int>("ColName");

The Field method also supports nullable types:

The Field method provides support for accessing columns as nullable types. If the underlying value in the DataSet is Value, the returned nullable type will have a value of null.

And please notice:

The Field method does not perform type conversions. If type conversion is required, you should first obtain the column value by using the Field method. The column value should then be converted to another type.

n.y
  • 3,343
  • 3
  • 35
  • 54
  • 1
    Agreed - haven't looked too in depth, but I believe this may avoid the boxing tax of the other methods mentioned. Be careful though, will throw error if value is DBNull. See this answer for more details: https://stackoverflow.com/questions/7104675/difference-between-getting-value-from-datarow – Soturi Nov 20 '17 at 16:01
2

You'll need to know which index the object you want is on, and cast that to an int, e.g.:

int value = (int)eersteRij[0];
Femaref
  • 60,705
  • 7
  • 138
  • 176
  • -1 wont work its a DataRow not a value.. unless you have defined a [Explicit cast](http://msdn.microsoft.com/en-us/library/xhbhezf4(v=vs.80).aspx) function – Shekhar_Pro Mar 02 '11 at 14:22
  • fixed it, you'll need to know which index the data is in of course. – Femaref Mar 02 '11 at 14:26
2

Because even at that level of hierarchy you still have a DataRow and you can't convert a DataRow to int.. a value could be converted though.

And why are you converting whole DataRow to int.. usually you would like to get a value in a cell at a row so try, Somthing like this:

int value = (int)eersteRij.Items[0];

where 0 can be replaced by the cell position(int) or Column name(string)

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
2
DataSet dataSet = getDataFromDatabase();
DataRow tableRow = dataSet.Tables[0].Rows[0];

int valueAsInteger = Convert.ToInt32(tableRow["COLUMN_NAME"]);
Gil Epshtain
  • 8,670
  • 7
  • 63
  • 89
0

I tend to use IndexOf() when looping thru the dataTable.

foreach (DataRow row in DataTable.table.Rows) { int index = dataTable.Rows.IndexOf(row); }

Dave Hampel
  • 160
  • 3
  • 13