3

In my project there are two datatables dtNames and dtDetails.

I dont know about SQL. Here I am connecting to XML and Textfiles.

dtNames

EmpName    EmpRole

   a         2
   b         3
   c         4

dtDetails

 Empid     EmpName   EmpCity   EmpRole

  101        a         abc       3  //EmpRole not equal to above table EmpRole
  102        b         abc       3
  103        c         xyz       4
  104        d         pqr       5
  105        e         rst       6

I want to relate these two datatables based on the EmpName and EmpRole (here comparing to dtNames) and store the result in a DataSet dsMain(table from dtDetails) and then divide the two datatables in according to comparison like matchedDataTable and unmatchedDataTable.

I know this can be done using DataRelation or RowFilter but i cant get a thought how to do this because there are two columns to be compared with other datatable two columns which i dont know.(I am beginner in .net)

I tried the below code: (not working)

            DataSet dsMain = new DataSet();
            DataRelation newRelation = new DataRelation("processData"
                , dtNames.Columns["EmpName"], dtDetails.Columns["EmpName"]);
            dsMain.Relations.Add(newRelation);

As you can see In the above code I am just comparing one column with other column. How compare with two. I am very close

please assist.

3
  • Check this - msdn.microsoft.com/en-us/library/bb386998.aspx Commented Oct 18, 2012 at 13:30
  • I think this is linq? if it is then i dont want it. I know how to do using linq but my project is strictly non-linq. cant it be done using dataset rowfilters or datarelations.. Commented Oct 18, 2012 at 13:32
  • As a side note: you might want to re-evaluate your database design (or your retrieval queries), as the datatables at the moment are not normalized (i.e. you are repeating the EmpRole data in both tables). Commented Oct 18, 2012 at 13:49

2 Answers 2

7

Use the constructor that accepts column arrays:

DataSet dsMain = new DataSet();
DataRelation newRelation = new DataRelation("processData",
    new DataColumn[] { dtNames.Columns["EmpName"], dtNames.Columns["EmpRole"] },
    new DataColumn[] {dtDetails.Columns["EmpName"], dtDetails.Columns["EmpRole"]}
);
dsMain.Relations.Add(newRelation);
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, how to divide it into two tables like matchedtable and unmatchedtable ?
The relation will actually prevent unmatched rows to be added. To separate "matched" from "unmatched" you'll have to use plain old foreach loop.
4

Did you check the possible constructors of DataRelation ?

Just pass an array of DataColumn, and you can combine multiple columns. Like so:

DataRelation newRelation = new DataRelation("test", new DataColumn[] { dt1.Columns[0], dt1.Columns[1] },
                                                    new DataColumn[] { dt2.Columns[0], dt2.Columns[1] });

2 Comments

thank you, how to divide it into two tables like matchedtable and unmatchedtable ?
'Matching' of data (if I understand you correctly) should happen at database level, so before the data reaches your application. Of course, this depends on your specific situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.