Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dataset and in that i am getting two tables based on one table values i have to modify another table how can i achieve this??

C#
_dsexportData.Tables.Add(_dtdiagnosisData);


In _dsexportData i am getting two tables.So based one table i have to make changes for another table.
Posted

1 solution

You have to iterate trough your first tables and use the information from first table to modify the values from the second table, like in the next example:
C#
DataTable firstTable = dataSet.Tables["FirstTableName"];
DataTable secondTable = dataSet.Tables["SecondTableName"];
//
//Iterate trough first data table data
foreach(DataRow row in firstTable.Rows)
{
    ModifySecondTable(rowFromFirstTable);     
}
...
void ModifySecondTable(DataRow rowFromFirstTable, DataTable secondTable)
{
  int fk = row["ID"]; // Access the ID, or the PK from first table that is FK in the 2nd one!
  foreach(DataRow secondTableRow in secondTable.Rows)
  {
      int linkField = secondTableRow["linkField"]; //Here you should put the real name of the FK!
      if(linkField == fk)
      {
          // TO DO:
          //Put your code that use the data from rowFromFirstTable to update the data from secondTableRow!
      }   
  }
}
 
Share this answer
 
Comments
chandra sekhar 11-Aug-14 2:35am    
What if when i dont have any dependency between two tables??I mean there is no FK then how can i achieve this?
Raul Iloc 11-Aug-14 2:44am    
You said there is a dependency between your two tables, so you should use that dependency similar like in my example above, maybe is by using other field or a set of fields from both tables, or maybe is based on the rows positions in your tables.
chandra sekhar 11-Aug-14 2:53am    
I dont have a dependency i am fetching them and storing in dataset so based on one table two row values i have to change another table 1 row value.
Raul Iloc 11-Aug-14 3:01am    
Your data from second table should depends some how on data from first one, so you should use this "logical dependency" between them.
chandra sekhar 12-Aug-14 8:16am    
Do we have any alternatives for this solution? As they are independent tables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900