Convert datarow to int

Steaphann picture Steaphann · Mar 2, 2011 · Viewed 51.3k times · Source

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())

Answer

user1228 picture user1228 · Mar 2, 2011

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"]);