fromapu
asked on
Merge two datatables within the same dataset
Hi,
I have created a procedure that returns a dataset containing three datatables. The procedure is as follows:
-------------------------- ---------- ---------- ---------- --------
Public Function getAuthorTitleDataset() As DataSet
Dim ds As New DataSet("AuthorWithTitles" )
Dim daAuthor As SqlDataAdapter
Dim daTitleAuthor As SqlDataAdapter
Dim daTitle As SqlDataAdapter
Dim fkAuthor As ForeignKeyConstraint
Dim fkTitle As ForeignKeyConstraint
Try
cnn = New SqlConnection(connString)
daAuthor = New SqlDataAdapter("Select au_id, au_lname, au_fname from authors", cnn)
daAuthor.Fill(ds, "Author")
daTitleAuthor = New SqlDataAdapter("Select au_id, title_id from titleauthor", cnn)
daTitleAuthor.Fill(ds, "TitleAuthor")
daTitle = New SqlDataAdapter("Select title_id, title from titles", cnn)
daTitle.Fill(ds, "Titles")
ds.Tables("Author").Column s("au_id") .Unique = True
ds.Tables("Author").Column s("au_id") .AllowDBNu ll = False
ds.Tables("Author").Primar yKey = New DataColumn() {ds.Tables("Author").Colum ns("au_id" )}
ds.Tables("Titles").Column s("title_i d").Unique = True
ds.Tables("Titles").Column s("title_i d").AllowD BNull = False
ds.Tables("Titles").Primar yKey = New DataColumn() {ds.Tables("Titles").Colum ns("title_ id")}
fkAuthor = New ForeignKeyConstraint("auth orTitleAut hor", ds.Tables("Author").Column s("au_id") , ds.Tables("TitleAuthor").C olumns("au _id"))
ds.Tables("TitleAuthor").C onstraints .Add(fkAut hor)
fkTitle = New ForeignKeyConstraint("titl eTitleAuth or", ds.Tables("Titles").Column s("title_i d"), ds.Tables("TitleAuthor").C olumns("ti tle_id"))
ds.Tables("TitleAuthor").C onstraints .Add(fkTit le)
Return ds
Catch ex As Exception
getError = ex.ToString()
Finally
daAuthor.Dispose()
daTitleAuthor.Dispose()
daTitle.Dispose()
End Try
End Function
-------------------------- ---------- ---------- ---------- -------
Now I have two data grids showing data from the two tables. The procedure is as follows.
-------------------------- ---------- ---------- ---------- -------
Dim ds As New DataSet
ds = objDataManager.getAuthorTi tleDataset ()
With Me
.dgAuthor.DataSource = ds.Tables("Author").Defaul tView
.dgtitle.DataSource = ds.Tables("Titles").Defaul tView
End With
-------------------------- ---------- ---------- ---------- -------
I want to have a third data grid that shows the data after merging the two datatables.
Also when I update any column and click on a button named "cmdUpdate" the change should be reflected in the database as well as the respective datagrid.
Please help
I have created a procedure that returns a dataset containing three datatables. The procedure is as follows:
--------------------------
Public Function getAuthorTitleDataset() As DataSet
Dim ds As New DataSet("AuthorWithTitles"
Dim daAuthor As SqlDataAdapter
Dim daTitleAuthor As SqlDataAdapter
Dim daTitle As SqlDataAdapter
Dim fkAuthor As ForeignKeyConstraint
Dim fkTitle As ForeignKeyConstraint
Try
cnn = New SqlConnection(connString)
daAuthor = New SqlDataAdapter("Select au_id, au_lname, au_fname from authors", cnn)
daAuthor.Fill(ds, "Author")
daTitleAuthor = New SqlDataAdapter("Select au_id, title_id from titleauthor", cnn)
daTitleAuthor.Fill(ds, "TitleAuthor")
daTitle = New SqlDataAdapter("Select title_id, title from titles", cnn)
daTitle.Fill(ds, "Titles")
ds.Tables("Author").Column
ds.Tables("Author").Column
ds.Tables("Author").Primar
ds.Tables("Titles").Column
ds.Tables("Titles").Column
ds.Tables("Titles").Primar
fkAuthor = New ForeignKeyConstraint("auth
ds.Tables("TitleAuthor").C
fkTitle = New ForeignKeyConstraint("titl
ds.Tables("TitleAuthor").C
Return ds
Catch ex As Exception
getError = ex.ToString()
Finally
daAuthor.Dispose()
daTitleAuthor.Dispose()
daTitle.Dispose()
End Try
End Function
--------------------------
Now I have two data grids showing data from the two tables. The procedure is as follows.
--------------------------
Dim ds As New DataSet
ds = objDataManager.getAuthorTi
With Me
.dgAuthor.DataSource = ds.Tables("Author").Defaul
.dgtitle.DataSource = ds.Tables("Titles").Defaul
End With
--------------------------
I want to have a third data grid that shows the data after merging the two datatables.
Also when I update any column and click on a button named "cmdUpdate" the change should be reflected in the database as well as the respective datagrid.
Please help
''merge two datasets using Merge method
ds1.Merge(ds2, False, MissingSchemaAction.Ignore )
''bind Datagrid to a dataset
DataGrid1.DataSource=ds1
DataGrid1.DataBind()
ds1.Merge(ds2, False, MissingSchemaAction.Ignore
''bind Datagrid to a dataset
DataGrid1.DataSource=ds1
DataGrid1.DataBind()
Hang on a second.... what do you mean my "merge"?
I see from your example that you created a set of foreign key constraints... does that mean that you're trying to do an SQL-style JOIN operations between the two tables? (Produce a 3rd table that is the result of a join between the two existing tables?)
If so, then the best way to do this would be to perform the join at the database level (not in your code). That means creating a command object with the appropriate SQL JOIN syntax and then using that to populate a 3rd table.
Another way to do with (but it's a bit overboard) is to perform the JOIN operation in your code. I've got a tutorial and some sample code on how to do just that at http://home.hot.rr.com/graye/Articles/DataTableJoins.htm
I see from your example that you created a set of foreign key constraints... does that mean that you're trying to do an SQL-style JOIN operations between the two tables? (Produce a 3rd table that is the result of a join between the two existing tables?)
If so, then the best way to do this would be to perform the join at the database level (not in your code). That means creating a command object with the appropriate SQL JOIN syntax and then using that to populate a 3rd table.
Another way to do with (but it's a bit overboard) is to perform the JOIN operation in your code. I've got a tutorial and some sample code on how to do just that at http://home.hot.rr.com/graye/Articles/DataTableJoins.htm
ASKER
Hi graye
The solution given by you is an ideal one. But the thing is suppose I have a datagrid that shows the author first name, last name , titles. I update the last name in one row, the titles in another. Then I want to update the details to the database. How do I do it
The solution given by you is an ideal one. But the thing is suppose I have a datagrid that shows the author first name, last name , titles. I update the last name in one row, the titles in another. Then I want to update the details to the database. How do I do it
OK... I think I see what you're after
You need a 3rd DataGrid that is dynamically linked to the other 2 DataGrids (you said that in your original post, but I guess I didn't get it). You want to be able to perform an edit on either one of the "source" datagrids and have those changes shown in the merged (3rd) datagrid.
If that's what you're trying to do... then the solution provided by iboutchkine and YZlat would probably work... to create a merged table (perhaps a 3rd table... to keep the original tables "clean"). Edits to the 2 source datagrids would be applied back to the source DataTables using the normal DataAdapter Update method (one update per source table).
Another techinque to accomplish the same thing would be to "tack on" the related columns using DataColumn expressions...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatacolumnclassexpressiontopic.asp
You need a 3rd DataGrid that is dynamically linked to the other 2 DataGrids (you said that in your original post, but I guess I didn't get it). You want to be able to perform an edit on either one of the "source" datagrids and have those changes shown in the merged (3rd) datagrid.
If that's what you're trying to do... then the solution provided by iboutchkine and YZlat would probably work... to create a merged table (perhaps a 3rd table... to keep the original tables "clean"). Edits to the 2 source datagrids would be applied back to the source DataTables using the normal DataAdapter Update method (one update per source table).
Another techinque to accomplish the same thing would be to "tack on" the related columns using DataColumn expressions...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatacolumnclassexpressiontopic.asp
ASKER
Hi,
But those solutions also do not let me to update the changes in both the data table at one go
But those solutions also do not let me to update the changes in both the data table at one go
Huh?
I thought you wanted to be able to edit the 2 "source" datagrids and have the results appear in the 3rd datagrid. Are you saying that you want to edit the 3rd datagrid (the merged one) and have those changes appear in the 2 source datagrids?
I thought you wanted to be able to edit the 2 "source" datagrids and have the results appear in the 3rd datagrid. Are you saying that you want to edit the 3rd datagrid (the merged one) and have those changes appear in the 2 source datagrids?
ASKER
Hi graye,
Now you got me. This is exactly what I expect... Actually this is a dummy example for the application we are about to build. It may contain many records. A user would see record after joining many tables.... He edits the data and the data is updated in the respective tables.
Any solution would be of great help.
Now you got me. This is exactly what I expect... Actually this is a dummy example for the application we are about to build. It may contain many records. A user would see record after joining many tables.... He edits the data and the data is updated in the respective tables.
Any solution would be of great help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dim oDt1 as DataTable = oDataSet.Tables("DT1")
Dim oDt2 as DataTable = oDataSet.Tables("DT2")
Dim i as integer
For i = 0 To oDt1.Rows.Count-1
oDt2.ImportRow(oDt1.Rows(i
Next