Combining datatables

  • Thread starter Thread starter Bonzol
  • Start date Start date
B

Bonzol

Hey there, just starting out using data tables, can anyone tell me how
to combine 2 tables?

Ive tried just coyping the info from 1 table to another, which both
have exactly the same table layout
data is a datatable
data1 is a datatable

data.Rows.Add(data1.Rows(0).Item(0))

but it dosnt work, can anyone tall me what im doing wrong?
 
Not sure if I understand completely, but are you asking how you can
copy the CONTENTS of one datatable into another? Or do you want to
MERGE the two tables, creating a new table that has columns from both?
How are both tables constructed previously? Post code if necessary.
 
Bonzol,

One of the things you have to know when working with datarows and datatables
is that you never can have a datarow in more than one table. Simple because
of the fact that the datarow has a property that references to the table it
is in.

However you can use copies of datarow.

But first answer the question from Steven.

Cor
 
Ahh sorry for not being clear,

I want to copy the contents of one table to another

I have been playing around and have now tried

Dim itemadd As DataRow = data.NewRow()
itemadd = data1.Rows(0).Item(0)
data.Rows.Add(itemadd)

but does not work.
 
tables were previously created via


Public Function jWildCard(ByVal table1 As String, ByVal returncolumn
As String, ByVal table2 As String, ByVal checkcolumn As String, ByVal
table1joinField As String, ByVal table2joinField As String, ByVal
checkvalue As String) As DataTable
'
Dim StringToReturn As String

StringToReturn = ""
Dim SQL As String
SQL = "SELECT " + table1 + "." + returncolumn + " FROM " +
table1 + " INNER JOIN " + table2 + " ON " + table1 + "." +
table1joinField + " = " + table2 + "." + table2joinField + " WHERE(" +
table2 + "." + checkcolumn + " LIKE '%" + checkvalue + "%')"
Dim dataAdapter As System.Data.OleDb.OleDbDataAdapter
dataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQL,
Me.OleDbConnection1)

Dim dt As System.Data.DataTable
dt = New System.Data.DataTable
dataAdapter.Fill(dt)


Return dt


this is done twice and both tables have the same Select table and
return coloumn.
 
Ok try this:

Dim newRow as datarow = secondtable.NewRow()
newRow.ItemArray = oldRow.Itemarray()
secondtable.Rows.add(newRow)

.... where oldRow is a row from your old table.
This code would be inserted in a 'for each' loop to get each row from
the old table.

Good luck.
 
Bonzol,

Your select is impossible with a Option Strict on.

Therefore it can give unpredictiable results depending on the values that
are in your variables.

Therefore I doubt if it is wise to start with trying to use methods as
merge, importrow etc, if you have not solved this before and know what you
are doing.

Just my thought,

Cor
 
Steven,

If you are using the 2.0 framwork you can to the following:

Dim newTable as New System.Data.DataTable = dt.DefaultView.ToTable

Just make sure you don't have any rowfilters set on the default view.

Cheers,
Rob Panosh
 
Sorry mean't to send to Bonzol.

Cheers,
Rob

Rob said:
Steven,

If you are using the 2.0 framwork you can to the following:

Dim newTable as New System.Data.DataTable = dt.DefaultView.ToTable

Just make sure you don't have any rowfilters set on the default view.

Cheers,
Rob Panosh
 
Yes the OP asked about combining 2 tables.
We don't want to overwrite the second table with a copy of the first.
Thus I think my solution might still be the best...
Its untested code though so am not sure how the datarow will handle the
itemarray if any of its copied contents do not match the columns
collection in the parent table.
 
Steven,
Yes the OP asked about combining 2 tables.
We don't want to overwrite the second table with a copy of the first.
Thus I think my solution might still be the best...
Its untested code though so am not sure how the datarow will handle the
itemarray if any of its copied contents do not match the columns
collection in the parent table.
Simple it throws an error and if not catched it stops.

The items in a datarow have forever to be confirm the datacolumn colletion
in a datatable.

Although that your first solution can be the solution will than the
"importrow" methode probably be better because that keeps the rowstate,
while yours is setting that as added.

Cor
 
Back
Top