943,102 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 1940
  • VB.NET RSS
Mar 9th, 2010
0

Merging DataTables with different Columns

Expand Post »
Hi again,

This is a problem I've been struggling with for a while...

I have an access 2007 Database where I store several different "Jobs" such as repair job, data recovery job etc.

At the min I have a form where the user can view a customer's related jobs either by type, or all jobs related to the customer. I'm trying to do this by adding each job type to it's own DataTable, then merging the DataTables into one.


VB.NET Syntax (Toggle Plain Text)
  1. Private Sub rdoShowAllJobs_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoShowAllJobs.CheckedChanged
  2. '####################### Procedure Level Constants and Variables ###########################
  3. ' Dim dtDataRecoveryJobs As New DataTable
  4. ' Dim dtRepairJobs As New DataTable
  5. ' Dim dtSEOJobs As New DataTable
  6.  
  7. Dim dtDataRecoveryJobs As New DataTable
  8. Dim dtRepairJobs As New DataTable
  9. Dim dtSEOJobs As New DataTable
  10. '###########################################################################################
  11.  
  12. Try
  13.  
  14. 'CreateAllJobsDataTable(dtDataRecoveryJobs)
  15. 'CreateAllJobsDataTable(dtRepairJobs)
  16.  
  17. ' Resets the DataSet to it's original state, i.e. empty
  18. Me.dtAllJobsTable.Reset()
  19. Me.dtAllJobsTable.GetErrors()
  20.  
  21. ' Select required columns from tblJobsDataRecovery
  22. Me.daCustomerJobs = New OleDbDataAdapter("SELECT DataRecoveryJobID, CustomerID, StaffID, DateCreated, TotalCost, Quote, Completed, DateCompleted FROM tblJobsDataRecovery", connOle)
  23. Me.daCustomerJobs.Fill(dtDataRecoveryJobs)
  24.  
  25.  
  26. ' Select required columns from tblJobsRepair
  27. Me.daCustomerJobs = New OleDbDataAdapter("SELECT RepairJobID, CustomerID, StaffID, DateCreated, TotalCost, Quote, Completed, DateCompleted FROM tblJobsRepair", connOle)
  28. Me.daCustomerJobs.Fill(dtRepairJobs)
  29.  
  30. ' Select required columns from tblJobsSEO
  31. Me.daCustomerJobs = New OleDbDataAdapter("SELECT SEOJobID, CustomerID, StaffID, DateCreated, TotalCost, Quote, Completed, DateCompleted FROM tblJobsSEO", connOle)
  32. Me.daCustomerJobs.Fill(dtSEOJobs)
  33.  
  34. Me.dtAllJobsTable.Merge(dtDataRecoveryJobs, True, MissingSchemaAction.Add)
  35. Me.dtAllJobsTable.Merge(dtRepairJobs, True, MissingSchemaAction.Add)
  36. Me.dtAllJobsTable.Merge(dtSEOJobs, True, MissingSchemaAction.Add)
  37.  
  38. ' Set up DataView
  39. Me.dvCustomerJob = New DataView(Me.dtAllJobsTable)
  40. Me.dvCustomerJob.RowFilter = "CustomerID = '" & Me.txtCustomerID.Text & "'"
  41. Me.dvCustomerJob.Sort = "DateCreated"
  42.  
  43. ' Set DataGrid datasource to DataView
  44. Me.dgOrders.DataSource = Me.dvCustomerJob
  45. Me.dgOrders.Refresh()
  46.  
  47.  
  48. Catch ex As Exception
  49. MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
  50. Console.WriteLine(ex.ToString())
  51. Finally
  52. SetDataGridProperties()
  53. End Try
  54. End Sub

And this is the code I'm using to set the schema for the DataTable containing all the job details

VB.NET Syntax (Toggle Plain Text)
  1. Private Sub CreateAllJobsDataTable(ByVal aDataTable As DataTable)
  2.  
  3. Dim dcID As DataColumn = New DataColumn("ID")
  4. dcID.DataType = System.Type.GetType("System.String")
  5. dcID.AllowDBNull = True
  6. dcID.Unique = False
  7. aDataTable.Columns.Add(dcID)
  8.  
  9. Dim dcCustomerID As DataColumn = New DataColumn("CustID")
  10. dcCustomerID.DataType = System.Type.GetType("System.String")
  11. aDataTable.Columns.Add(dcCustomerID)
  12.  
  13. Dim dcStaffID As DataColumn = New DataColumn("StfID")
  14. dcStaffID.DataType = System.Type.GetType("System.String")
  15. aDataTable.Columns.Add(dcStaffID)
  16.  
  17. Dim dcDateCreated As DataColumn = New DataColumn("DateCreated")
  18. dcDateCreated.DataType = System.Type.GetType("System.DateTime")
  19. aDataTable.Columns.Add(dcDateCreated)
  20.  
  21. Dim dcTotalCost As DataColumn = New DataColumn("TotalCost")
  22. dcTotalCost.DataType = System.Type.GetType("System.Double")
  23. aDataTable.Columns.Add(dcTotalCost)
  24.  
  25. Dim dcQuote As DataColumn = New DataColumn("Quote")
  26. dcQuote.DataType = System.Type.GetType("System.Boolean")
  27. aDataTable.Columns.Add(dcQuote)
  28.  
  29. Dim dcCompleted As DataColumn = New DataColumn("Completed")
  30. dcCompleted.DataType = System.Type.GetType("System.Boolean")
  31. aDataTable.Columns.Add(dcCompleted)
  32.  
  33. Dim dcDateCompleted As DataColumn = New DataColumn("DateCompleted")
  34. dcDateCompleted.DataType = System.Type.GetType("System.DateTime")
  35. aDataTable.Columns.Add(dcDateCompleted)
  36.  
  37. End Sub

What I want is for the first column, ID, to contain the primary key from each table, but when the code runs it adds each primary key column from the tables to the end of the AllJobs DataTable! Am I trying to do something impossible here or is there a way around this?

Again, any help is much appreciated and thank you in advance!!!
Attached Thumbnails
Click image for larger version

Name:	What Is Happening.jpg
Views:	51
Size:	213.8 KB
ID:	13966   Click image for larger version

Name:	What Should Happen.jpg
Views:	49
Size:	235.2 KB
ID:	13967  
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
SCass2010 is offline Offline
68 posts
since Mar 2010
Mar 11th, 2010
0
Re: Merging DataTables with different Columns
>Merging DataTables with different Columns

Not a built-in feature. You have to use SQL-Joins that fetch data from diff. tables.
Moderator
Reputation Points: 2134
Solved Threads: 1227
Posting Genius
adatapost is offline Offline
6,524 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: receipt print problem using pos printer on vb.net
Next Thread in VB.NET Forum Timeline: Copy fields in form to database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC