Return a column value from a table in a dataset

I have a dataset with two tables. I want to get the value of the first column from the second table and initialize it with an int variable. Name of this column: CONTACT_ID

I tried like this.

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

but he found an error:

Unable to create an object of type 'System.Data.DataColumn' for input of type 'System.IConvertible'.

can someone help me.

+8
c # datatable
source share
2 answers

dsDiscounts.Tables[1].Columns[0] returns the definition of the column (data type, signature, etc., defined by the DataColumn instance). Of course, converting a column definition to an integer fails.

You need a cell value from some row in the table (suppose the first row). You must use Rows to access table rows. After you get the required DataRow using the index, you can access the cells in the row by index , column name, column object, etc. For example, getting the cell value of the first row by column name:

  dsDiscounts.Tables[1].Rows[0]["CONTACT_ID"] 
+18
source share

try it

 int Contract_id = Convert.ToInt32(dsDiscounts.Tables[1].Rows[0]["CONTACT_ID"]); 
+2
source share

All Articles