Joining 2 DataTables

Discussion in 'ASP .Net' started by JoNo216@gmail.com, May 11, 2007.

  1. Guest

    hey guys,
    I'm a little new when it comes to the ASP environment, and I need a
    little help...
    Does anyone know how to combine two separate datatables, and get them
    to display one after the other on a datagrid? I am currently trying to
    use viewstate.Add, to get both of them together but that's just not
    working, and I don't see a viewstate.merge or join or anything.
    Has anyone ever found a way to work around this? Or is there something
    I am overlooking?

    Thanks!
     
    , May 11, 2007
    #1
    1. Advertising

  2. 1. I would recommend doing it on database side if possible.
    Use "UNION" in SQL to combine 2 SELECTs

    2. If #1 is not an option, all I can come up with is manually adding rows
    from one DataTable object to another DataTable object and then bind your
    grid to that DataTable.

    Like
    DataTable dt1, dt2;
    dt1 = GetData("...sql...");
    dt2 = GetData("...sql...");

    foreach(DataRow r in dt2.Rows)
    {
    DataRow newRow = dt1.NewRow();
    newRow.ItemArray = r.ItemArray;
    dt1.Rows.Add(newRow);
    }

    George.

    <> wrote in message
    news:...
    > hey guys,
    > I'm a little new when it comes to the ASP environment, and I need a
    > little help...
    > Does anyone know how to combine two separate datatables, and get them
    > to display one after the other on a datagrid? I am currently trying to
    > use viewstate.Add, to get both of them together but that's just not
    > working, and I don't see a viewstate.merge or join or anything.
    > Has anyone ever found a way to work around this? Or is there something
    > I am overlooking?
    >
    > Thanks!
    >
     
    George Ter-Saakov, May 11, 2007
    #2
    1. Advertising

  3. vMike Guest

    <> wrote in message
    news:...
    > hey guys,
    > I'm a little new when it comes to the ASP environment, and I need a
    > little help...
    > Does anyone know how to combine two separate datatables, and get them
    > to display one after the other on a datagrid? I am currently trying to
    > use viewstate.Add, to get both of them together but that's just not
    > working, and I don't see a viewstate.merge or join or anything.
    > Has anyone ever found a way to work around this? Or is there something
    > I am overlooking?
    >
    > Thanks!
    >

    you might also look into dataset merge as long as you have a common primary
    key defined. You would create a data set with the first table and then merge
    the second table into the dataset.
     
    vMike, May 12, 2007
    #3
  4. GroupReader Guest

    I agree w/ George... and doing it on the DB side is the better
    option. If using "UNION", be careful... you probably want "UNION ALL"
    instead.
     
    GroupReader, May 13, 2007
    #4
  5. Guest

    On May 12, 11:29 pm, GroupReader <> wrote:
    > I agree w/ George... and doing it on the DB side is the better
    > option. If using "UNION", be careful... you probably want "UNION ALL"
    > instead.


    The union would not work unfortunately because it is using the same
    database.
    It is completely dependant on what the user actually inputs into the
    screen as to what I am adding to the grid.
    Basically there are 2 distinct types on data for the grid, and they
    can't be mixed up. So I was just going to create separate datatables
    and then merge them together.
    It seems like I will have to just try the looping or the merge
    method , because I don't think that it will work any other way
    really.
    Thanks guys!
     
    , May 14, 2007
    #5
  6. sloan Guest

    DataSet.Merge works well.

    ---------------
    IF you have 2 seperate tables (in your dataset) .
    Like
    ds.Employee
    ds.Dept
    -----------
    It works well on one table, but you have different PK's (as mentioned)

    like
    ds.Employee
    ds.Employee
    (perhaps the first one has full time employees, and the second
    one has part time employees)


    EmployeeDS ds1 = new EmployeeDS();
    //populate ds1 with full time emps.

    EmployeeDS ds2 = new EmployeeDS();
    //populate ds2 with parttime emps.

    EmployeeDS dsmerged = ds1.Merge( ?? ds2 //multi overloads here) ;


    ...

    Merging "the same rows based on the PK" is a totally different story, and
    this permutation doesn't play nice.





    <> wrote in message
    news:...
    > hey guys,
    > I'm a little new when it comes to the ASP environment, and I need a
    > little help...
    > Does anyone know how to combine two separate datatables, and get them
    > to display one after the other on a datagrid? I am currently trying to
    > use viewstate.Add, to get both of them together but that's just not
    > working, and I don't see a viewstate.merge or join or anything.
    > Has anyone ever found a way to work around this? Or is there something
    > I am overlooking?
    >
    > Thanks!
    >
     
    sloan, May 14, 2007
    #6
  7. vMike Guest

    "vMike" <> wrote in message
    news:DTj1i.668$...
    >
    > <> wrote in message
    > news:...
    >> hey guys,
    >> I'm a little new when it comes to the ASP environment, and I need a
    >> little help...
    >> Does anyone know how to combine two separate datatables, and get them
    >> to display one after the other on a datagrid? I am currently trying to
    >> use viewstate.Add, to get both of them together but that's just not
    >> working, and I don't see a viewstate.merge or join or anything.
    >> Has anyone ever found a way to work around this? Or is there something
    >> I am overlooking?
    >>
    >> Thanks!
    >>

    > you might also look into dataset merge as long as you have a common
    > primary key defined. You would create a data set with the first table and
    > then merge the second table into the dataset.
    >

    The more I think of it you can merge two or more tables as a union or as a
    left join, depending on the MissingSchemaAction. The merge function will
    union if the two tables have identical structure but if there are any
    duplicate primary keys it will fail. If you want to join you would need
    identical primary keys in both tables and use the msa.add.

    Mike
     
    vMike, May 15, 2007
    #7
  8. vMike Guest

    "vMike" <> wrote in message
    news:Yka2i.1819$...
    >
    > "vMike" <> wrote in message
    > news:DTj1i.668$...
    >>
    >> <> wrote in message
    >> news:...
    >>> hey guys,
    >>> I'm a little new when it comes to the ASP environment, and I need a
    >>> little help...
    >>> Does anyone know how to combine two separate datatables, and get them
    >>> to display one after the other on a datagrid? I am currently trying to
    >>> use viewstate.Add, to get both of them together but that's just not
    >>> working, and I don't see a viewstate.merge or join or anything.
    >>> Has anyone ever found a way to work around this? Or is there something
    >>> I am overlooking?
    >>>
    >>> Thanks!
    >>>

    >> you might also look into dataset merge as long as you have a common
    >> primary key defined. You would create a data set with the first table and
    >> then merge the second table into the dataset.
    >>

    > The more I think of it you can merge two or more tables as a union or as a
    > left join, depending on the MissingSchemaAction. The merge function will
    > union if the two tables have identical structure but if there are any
    > duplicate primary keys it will fail. If you want to join you would need
    > identical primary keys in both tables and use the msa.add.
    >
    > Mike

    I got the fail backwards. The left join will fail if there are more then one
    identical primary keys in the left join table. Merge will fail for one to
    many, has to be one to one. The union will ignore the identical key.
     
    vMike, May 15, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravindra

    About datatables

    Ravindra, Dec 30, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    503
    Christopher Reed
    Dec 30, 2005
  2. Richard Loupatty
    Replies:
    2
    Views:
    379
    Richard Loupatty
    Jul 18, 2003
  3. Kevin Spencer

    Re: Combining DataTables

    Kevin Spencer, Jul 21, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,118
    Kevin Spencer
    Jul 21, 2003
  4. Gene Ariani
    Replies:
    0
    Views:
    2,807
    Gene Ariani
    Aug 21, 2003
  5. Simple Simon

    Compare Two DataTables

    Simple Simon, Dec 15, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    12,879
    Simple Simon
    Dec 15, 2003
Loading...

Share This Page