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
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"]
Try this
int Contract_id = Convert.ToInt32(dsDiscounts.Tables[1].Rows[0]["CONTACT_ID"]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With