473,500 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combining datatables

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?

Jul 19 '06 #1
11 4615
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.

Jul 19 '06 #2
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

"Bonzol" <Bo****@hotmail.comschreef in bericht
news:11**********************@s13g2000cwa.googlegr oups.com...
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?

Jul 19 '06 #3
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.

Jul 19 '06 #4
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.

Jul 19 '06 #5
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.

Jul 19 '06 #6
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
"Bonzol" <Bo****@hotmail.comschreef in bericht
news:11**********************@b28g2000cwb.googlegr oups.com...
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.

Jul 20 '06 #7
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
Steven Nagy wrote:
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.
Jul 20 '06 #8
Sorry mean't to send to Bonzol.

Cheers,
Rob

Rob Panosh wrote:
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
Steven Nagy wrote:
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.
Jul 20 '06 #9
Rob,
Dim newTable as New System.Data.DataTable = dt.DefaultView.ToTable
Dim newTable as DataTable = dt.copy

does the same without thinking about the rowfilter and version.

http://msdn2.microsoft.com/en-us/lib...able.copy.aspx

I got the idea you did forever miss this one.
(However this cannot be the solution from the problem from the OP)

Cor
Jul 20 '06 #10
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.
Cor Ligthert [MVP] wrote:
Rob,
Dim newTable as New System.Data.DataTable = dt.DefaultView.ToTable
Dim newTable as DataTable = dt.copy

does the same without thinking about the rowfilter and version.

http://msdn2.microsoft.com/en-us/lib...able.copy.aspx

I got the idea you did forever miss this one.
(However this cannot be the solution from the problem from the OP)

Cor
Jul 20 '06 #11
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
Jul 21 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2043
by: Jade | last post by:
Hi, I just wanted to ask a quick question regarding datasets. I am creating 3 tables using a dataadapter. what i want to know is that is the relationship created between these datatables...
2
3235
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491...
2
4317
by: Z D | last post by:
Hello, I'm currently using Remoting (HTTP/Binary) to remote a simple object. Everything is working fine except for one function that returns an arraylist of datatables. When I call this...
6
1835
by: Red | last post by:
Hi all, I would like to ask how to combine 2 field into one column. For example I have field first name and last name. When I show it to the datagrid I want to show it as one column, for...
4
2240
by: sal | last post by:
Greets, All Converting array formula to work with datatables/dataset tia sal I finally completed a formula I was working on, see working code below. I would like to change this code so it...
1
1245
by: SMai24 | last post by:
I have two datatables in a dataset, i need to combine both of them into one table. Given that both tables have the same columns. Other than looping, anyone suggestions? Thanx in adavnce!!
2
4110
by: J055 | last post by:
Hi I need to search a number of DataTables within a DataSet (with some relationships) and then display the filtered results in a GridView. The Columns that need to be displayed come from 2 of...
0
1511
by: StefanPienaar | last post by:
Hi Guys Is there any way in c# (or vb.net) to extract a datatable of data from a dataset with multiple datatables which has relationships set up (containing combined data from the datatables)? ...
3
2820
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.