Convert datarow to int

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

I tried this, but it does not work.

dsMovie = (DataSet)wsMovie.getKlantId(); tabel = dsMovie.Tables["tbl_klanten"]; eersteRij = tabel.Rows[0]; (Int32.Parse(eersteRij.ToString()) 
+4
source share
4 answers

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

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

 var myInt = (int)eersteRij[0]; 

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

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

If the column has the name klant_id ...

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

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

 var myInt = int.Parse(eersteRij["klant_id"]); 
+11
source

Just 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 NULL types. If the base value in the DataSet is Value, the return nullable type will be null.

And note:

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

+5
source

You need to know what index the object you want is in and pass it to int, for example:

 int value = (int)eersteRij[0]; 
+2
source

Since even at this level of hierarchy you still have a DataRow, and you cannot convert the DataRow to int .. the value can be converted though.

And why do you convert the entire DataRow to int .. usually you would like to get the value in a cell in a row to try, somthing like this:

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

where 0 can be replaced by cell position (int) or column name (row)

+2
source

Source: https://habr.com/ru/post/1341997/


All Articles