Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a column value from a table in dataset

I have a dataset with two tables.I want to get the value of first column from second table and initialize it to an int variable.
The name of that column was CONTACT_ID

I tried like this.

  int Contract_id = Convert.ToInt32(dsDiscounts.Tables[1].Columns[0]);

but it was showing an error:

Unable to cast object of type 'System.Data.DataColumn' to type 'System.IConvertible'.

can anyone help me please

like image 559
sindhu jampani Avatar asked May 14 '14 07:05

sindhu jampani


Video Answer


2 Answers

dsDiscounts.Tables[1].Columns[0] returns column definition (data type, caption, etc defined by DataColumn instance). Of course column definition conversion to integer fails.

What you need is cell value from some row of table (assume first row). You should use Rows collection to get access to table rows. After you get required DataRow by it's index, you can access cells in row by index, column name, column object, etc. E.g. getting first row's cell value by column name:

 dsDiscounts.Tables[1].Rows[0]["CONTACT_ID"]
like image 128
Sergey Berezovskiy Avatar answered Oct 13 '22 22:10

Sergey Berezovskiy


Try this

int Contract_id = Convert.ToInt32(dsDiscounts.Tables[1].Rows[0]["CONTACT_ID"]);
like image 45
Sachin Avatar answered Oct 13 '22 22:10

Sachin