ADO.NET questions

K

Kevin

Hi All

I have a couple of questions, I am not at a machine with
VS.NETso that is why I am asking them here

1. When working with a dataset, how do I go to a specific
record and update a field in a table in the dataset? I
know you could probably run the updateCommand for the
DataAdapter, but that would update the data directly in
the database, and not in the dataset - not so? I want to
update a field in my datasets' datatable and then somehow
tell the dataset to update the database, how do I do this?

2. This one is about dataRelations. I have set up a
datarelation between two datatables in my dataset. Now I
have a for each loop that cycles through each record in
the one datatable, inside that for loop I want another for
each loop that will get me the child rows for the parents
ID column. my code looks something like this

for each (DataRow dr in dataset.tables["tablename"])
{

for each (DataRow childdr in dr.GetChildRows(Datarelation)
{
//do some work here
}

}

The inside for each, executes, but it is as if there are
no child rows for this parent column, .... but there is,
when I check the database. Is there something else that I
might be missing. I have setup the Datarelations
correctly, I have checked them sooo many times.

Any suggestions?

Thanks for any help it's greatly appreciated, If I have
not explained myself very well please let me know, I am
new to C#.
Thanks
Kevin
 
M

Marco Martin

See inline comments
Kevin said:
Hi All

I have a couple of questions, I am not at a machine with
VS.NETso that is why I am asking them here

1. When working with a dataset, how do I go to a specific
record and update a field in a table in the dataset? I
know you could probably run the updateCommand for the
DataAdapter, but that would update the data directly in
the database, and not in the dataset - not so? I want to
update a field in my datasets' datatable and then somehow
tell the dataset to update the database, how do I do this?

to update the individual rows you can access the rows and columns like this;
Dataset1.Tables["MyTableName"].Rows[nRowNumberToUpdate].["ColumnNameYouWantT
oUpdate"] = value;

As for updating the database you can either use stored procedures, which is
what I perfer. Or, you could create an update statement in SQL which will
probaly end up beeing more work in the end.
2. This one is about dataRelations. I have set up a
datarelation between two datatables in my dataset. Now I
have a for each loop that cycles through each record in
the one datatable, inside that for loop I want another for
each loop that will get me the child rows for the parents
ID column. my code looks something like this

for each (DataRow dr in dataset.tables["tablename"])
{

for each (DataRow childdr in dr.GetChildRows(Datarelation)
{
//do some work here
}

}

try this syntax;

for each (DataRow dr in dataset.tables["tablename"].ROWS)
{
DataRow[] childRows = dr.GetChildRows(DataRelation);
if(childRows.Length > 0)
{
for each (DataRow childdr in childRows)
{
//do some work here
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top