Setting variables to a value in a dataset.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm new to C# but learning quickly.

What i have is a prog that does a connection to a database and pulls in a
(untyped) dataset during runtime. I have some bound data to textboxes etc and
those work fine, but when i attempt to set some dataset values to variables,
it throws the prog to catch (using try and catch) By the way, None of this
was ceated with the VS wizards.

I got the command from MSDN i thought i needed
string s = (string) ds.Tables["myTable"].Rows[0]["mycolumn"];

I would like a single value from the dataset to be set to a variable, but i
guess I would also be happy with the entire row in an array perhaps.

Everything in the DB that iu'm pulling into the dataset are strings
I have found many other postings on this but have not had any luck. Does
anyone have any ideas? Thanks in advance.
 
I'm new to C# but learning quickly.

What i have is a prog that does a connection to a database and pulls in a
(untyped) dataset during runtime. I have some bound data to textboxes etc and
those work fine, but when i attempt to set some dataset values to variables,
it throws the prog to catch (using try and catch) By the way, None of this
was ceated with the VS wizards.

I got the command from MSDN i thought i needed
string s = (string) ds.Tables["myTable"].Rows[0]["mycolumn"];

I would like a single value from the dataset to be set to a variable, but i
guess I would also be happy with the entire row in an array perhaps.

Everything in the DB that iu'm pulling into the dataset are strings
I have found many other postings on this but have not had any luck. Does
anyone have any ideas? Thanks in advance.

What kind of exception do you get?

Try to split the statement into multiple statements like:

DataTable dtab = ds.Tables["myTable"];
DataRow drow = dtab.Rows[0];
DataColumn dcol = drow["mycolumn"];
string s = (string)dcol;

Then debug and see where the exception iks thrown.
 
Ludwig Stuyck said:
I'm new to C# but learning quickly.

What i have is a prog that does a connection to a database and pulls in a
(untyped) dataset during runtime. I have some bound data to textboxes etc and
those work fine, but when i attempt to set some dataset values to variables,
it throws the prog to catch (using try and catch) By the way, None of this
was ceated with the VS wizards.

I got the command from MSDN i thought i needed
string s = (string) ds.Tables["myTable"].Rows[0]["mycolumn"];

I would like a single value from the dataset to be set to a variable, but i
guess I would also be happy with the entire row in an array perhaps.

Everything in the DB that iu'm pulling into the dataset are strings
I have found many other postings on this but have not had any luck. Does
anyone have any ideas? Thanks in advance.

What kind of exception do you get?

Try to split the statement into multiple statements like:

DataTable dtab = ds.Tables["myTable"];
DataRow drow = dtab.Rows[0];
DataColumn dcol = drow["mycolumn"];
string s = (string)dcol;

Then debug and see where the exception iks thrown.


Thanks for the help.

Now, the third line of code you gave me gives me this error.

Cannot implicitly convert type 'object' to 'System.Data.DataColumn'
I'll research that and try and find out what I'm doing wrong.
 
Back
Top