How to combind two datatables in to one datatable in .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhappy
    New Member
    • Jul 2007
    • 139

    How to combind two datatables in to one datatable in .net

    Hai All,

    I need to combind two datatables.Firs t table contains 2 columns and 2nd table contains 3 columns, i need to combind both in one table, and the result should be in 5 columns.Is it possible...?


    Thanks,
    Aswath.
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    yes it is possible.
    Create third table have all 5 columns and populate the data with both the tables.

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      you need to combine two databases or two tables ?

      BTW what is the database that you are using ?

      Comment

      • bhappy
        New Member
        • Jul 2007
        • 139

        #4
        Hai
        Thanks for Replay,

        Im using MS SQL Server.Im getting data from Database,In code behind im using two datatables for two stored procedures.fina lly i need to combind two datatables to one and i want the result in pyarlally ie.
        datatable1 --10 rows x 2columns
        datatable2 --10 rows x 3 columns

        result table:---- 10rows x 5 columns is it possible.....?

        Thanks,
        Aswath.

        Comment

        • kunal pawar
          Contributor
          • Oct 2007
          • 297

          #5
          u can do it, just create table using Datatable, Datacolumn and then add the both tables rows in 3rd table

          Comment

          • bhappy
            New Member
            • Jul 2007
            • 139

            #6
            Hai

            Thanks for Replay,
            Creating new datatable is ok,but while adding rows from two datatables i dont't have any common columns,how can i add..? and i tried like using "datatable.load datarow()" method but im getting result as

            dt1-----10 x 2 (rows x columns)
            dt2 ----10 x 3

            for i=0 to dt2.rows.count-1
            dt1.loaddatarow (dt1.rows(i).it emarray,true)

            output is :dt1--------20 x 3
            need dt1 as 10 x 5 (rows x columns)

            Thanks,
            Aswath.

            Comment

            • bhappy
              New Member
              • Jul 2007
              • 139

              #7
              Hai All,

              Any sollution to my problem........ ..........?

              Thanks,
              Aswath.

              Comment

              • thenmozhivasanraj
                New Member
                • Dec 2007
                • 11

                #8
                Create Datatable with 6 columns

                1 Column should have a unique field which is used to update the datas from second database table.

                Query the first Database and create the rows through code.

                query the second Database and compare the each and every row compare with datatable and update the 3 columns in datatable.

                You will get 6 column Datatable. If you wants to bind with anyother control just make visible false it the unique id column.

                Comment

                • Vajrala Narendra
                  New Member
                  • Jun 2007
                  • 73

                  #9
                  Originally posted by bhappy
                  Hai
                  Thanks for Replay,

                  Im using MS SQL Server.Im getting data from Database,In code behind im using two datatables for two stored procedures.fina lly i need to combind two datatables to one and i want the result in pyarlally ie.
                  datatable1 --10 rows x 2columns
                  datatable2 --10 rows x 3 columns

                  result table:---- 10rows x 5 columns is it possible.....?

                  Thanks,
                  Aswath.
                  without having common column in those you can't combine those two tables
                  using views you can get that type of results

                  Comment

                  • CyberSoftHari
                    Recognized Expert Contributor
                    • Sep 2007
                    • 488

                    #10
                    Can you explain why you want to combine two tables? That will be helpful to the experts to point you correct direction.
                    Note: do not let us assume

                    Comment

                    • bhappy
                      New Member
                      • Jul 2007
                      • 139

                      #11
                      Hai

                      Thanks for Replay,

                      I already told that there is no common columns in two tables, so im using two datagrids(side by side) to display the information.Up to here it is ok,Now my client need the data to be exported to Excel.Here im getting problem that only one datatable data is exported to excel.That's why i need to combind two datatables.

                      Thanks,
                      Aswath.

                      Comment

                      • balabaster
                        Recognized Expert Contributor
                        • Mar 2007
                        • 798

                        #12
                        Originally posted by bhappy
                        Hai

                        Thanks for Replay,

                        I already told that there is no common columns in two tables, so im using two datagrids(side by side) to display the information.Up to here it is ok,Now my client need the data to be exported to Excel.Here im getting problem that only one datatable data is exported to excel.That's why i need to combind two datatables.

                        Thanks,
                        Aswath.
                        I would probably just do a straight parse of the two datatables...
                        • Create an instance of an Excel Workbook/Worksheet
                        • Parse table 1 dumping the contents into columns 1 and 2 of the worksheet instance
                        • Parse table 2 dumping the contents into columns 3, 4 and 5 of the worksheet instance
                        • Save the worksheet object to disk
                        • Dispose the worksheet object
                        Does that answer your question? You specify that you don't have any piece of common data, but the way you describe it, there is a tacit implication that the row number of each grid is the common key. Even though neither grid physically contains a key field, the key is implied in the manner in which you are wanting to join the tables.

                        Table1.Row0 is paired with Table2.Row0
                        Table1.Row1 is paired with Table2.Row1
                        Table1.Row2 is paired with Table2.Row2
                        etc...

                        Your common piece of data is the row number of each data table, even though it is not contained within the visible data sets.

                        Comment

                        • bhappy
                          New Member
                          • Jul 2007
                          • 139

                          #13
                          Hai
                          Thanks for Replay,
                          i dono how to parse datatable to excel.any other idea plz.........?

                          Comment

                          Working...