11 Replies - 535 Views - Last Post: 17 June 2013 - 02:50 AM Rate Topic: -----

#1 Keno14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-June 13

combine 2 tables into one table in (VB)

Posted 14 June 2013 - 07:57 AM

I have two tables Table A and Table B. I want to combine them and get one table (Table C) as shown below.
Table A Table B Table C
Column1 Column2 Column1 Column2 Column1 Column2
1 A 1 C 1 A
2 B 2 D 2 B
3 C
4 D

I am using Visual Studio 2010 (VB)

Is This A Good Question/Topic? 0
  • +

Replies To: combine 2 tables into one table in (VB)

#2 Keno14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-June 13

Re: combine 2 tables into one table in (VB)

Posted 14 June 2013 - 08:07 AM

I have two tables Table A and Table B. Table A has values (1,2) in column 1 and (A,B) in column 2. Table B has values (1,2) in column 1 and (C,D) in column 2.
I want to combine them and get one table (Table C) with values (1,2,3,4) in column 1 and (A,B,C,D) in column 2
I am using Visual Studio 2010 (VB)
Was This Post Helpful? 0
  • +
  • -

#3 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 497
  • View blog
  • Posts: 3,314
  • Joined: 12-January 10

Re: combine 2 tables into one table in (VB)

Posted 14 June 2013 - 08:16 AM

nice--- now do you have a question and some code that shows you have tried to do this?

nice--- now do you have a question and some code that shows you have tried to do this?
Was This Post Helpful? 0
  • +
  • -

#4 Keno14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-June 13

Re: combine 2 tables into one table in (VB)

Posted 14 June 2013 - 09:44 AM

Thanks Daren for your reply but I haven't got what you mean
Was This Post Helpful? 0
  • +
  • -

#5 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 497
  • View blog
  • Posts: 3,314
  • Joined: 12-January 10

Re: combine 2 tables into one table in (VB)

Posted 14 June 2013 - 10:01 AM

You need to show us the work you attempted in order for us to help you-- also you need to tell us the errors you are receiving
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: combine 2 tables into one table in (VB)

Posted 14 June 2013 - 10:31 AM

Are these database tables you want to join? Are they datatables? What kind of tables are these and can you rewrite your input and output results a little better? I am trying to make sense of how the values from table A and B are in table C. Are you wanting to add the columns together? Are you wanting to add the rows? More clarification is needed here.

Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#7 Keno14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-June 13

Re: combine 2 tables into one table in (VB)

Posted 14 June 2013 - 10:35 AM

this is part of the code I have

        Dim Result = _
            From A In ds.Tables("TableA").AsEnumerable _
              Join B In ds.Tables("TableB").AsEnumerable On _
               A.Field(Of Integer)("Column1") Equals B.Field(Of Integer)("Column1") _
              Select New With { _
              .Column1 = A.Field(Of Integer)("Column1"), _
              .Column2 = A.Field(Of String)("Column2"), _
              

        Dim TableC As New DataTable
        TableC.Columns.Add("Column1", GetType(String))
        TableC.Columns.Add("Column2", GetType(String))
        
        For Each row In Result
            Dim dr As DataRow = TableC.NewRow()
            dr("Column1") = row.Column1
            dr("Column2") = row.Column2
            

        Next

This post has been edited by andrewsw: 14 June 2013 - 10:40 AM
Reason for edit:: Please use CODE tags

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,489
  • Joined: 12-December 12

Re: combine 2 tables into one table in (VB)

Posted 14 June 2013 - 10:43 AM

Posts merged; please don't create duplicate topics.
Was This Post Helpful? 0
  • +
  • -

#9 Keno14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-June 13

Re: combine 2 tables into one table in (VB)

Posted 14 June 2013 - 11:10 AM

@ Martyr2

Yes they are database datatables. I want to add together the columns of Table A and Table B and then give the result in Table C. The rows should be added. If I have two rows in Table A and two rows in Table B, then I should get four rows in Table C

@ AndrewSW



The code works but it does not give what I want
Was This Post Helpful? 0
  • +
  • -

#10 Keno14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-June 13

Re: combine 2 tables into one table in (VB)

Posted 14 June 2013 - 11:17 AM

View PostMartyr2, on 14 June 2013 - 10:31 AM, said:

Are these database tables you want to join? Are they datatables? What kind of tables are these and can you rewrite your input and output results a little better? I am trying to make sense of how the values from table A and B are in table C. Are you wanting to add the columns together? Are you wanting to add the rows? More clarification is needed here.

Thanks! :)/>



Yes they are database datatables. I want to add together the columns of Table A and Table B and then give the result in Table C. The rows should be added. If I have two rows in Table A and two rows in Table B, then I should get four rows in Table C
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,489
  • Joined: 12-December 12

Re: combine 2 tables into one table in (VB)

Posted 14 June 2013 - 11:28 AM

Your description is still not clear, particularly because you have mentioned the bare-minimum of sample values.

If Table A has numbers 1,2 and so does Table B, then wouldn't the result have numbers 1,2,1,2 (rather than 1,2,3,4).

What do you intend should happen if Table A has values 1,A, 2,B, 3,A, 4,B?

So you need to clarify, using more sample data.

It might be that you want to use Linq-Union but it is not clear yet.
Was This Post Helpful? 0
  • +
  • -

#12 Keno14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-June 13

Re: combine 2 tables into one table in (VB)

Posted 17 June 2013 - 02:50 AM

View Postandrewsw, on 14 June 2013 - 11:28 AM, said:

Your description is still not clear, particularly because you have mentioned the bare-minimum of sample values.

If Table A has numbers 1,2 and so does Table B, then wouldn't the result have numbers 1,2,1,2 (rather than 1,2,3,4).

What do you intend should happen if Table A has values 1,A, 2,B, 3,A, 4,B?

So you need to clarify, using more sample data.

It might be that you want to use Linq-Union but it is not clear yet.


Thanks Andrew

The columns with values (1,2) are ID columns. These tables have primary keys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1