Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Joining 2 DataTables

Reply
Thread Tools

Joining 2 DataTables

 
 
JoNo216@gmail.com
Guest
Posts: n/a
 
      05-11-2007
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!

 
Reply With Quote
 
 
 
 
George Ter-Saakov
Guest
Posts: n/a
 
      05-11-2007
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.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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!
>



 
Reply With Quote
 
 
 
 
vMike
Guest
Posts: n/a
 
      05-12-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.



 
Reply With Quote
 
GroupReader
Guest
Posts: n/a
 
      05-13-2007
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.

 
Reply With Quote
 
JoNo216@gmail.com
Guest
Posts: n/a
 
      05-14-2007
On May 12, 11:29 pm, GroupReader <(E-Mail Removed)> 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!

 
Reply With Quote
 
sloan
Guest
Posts: n/a
 
      05-14-2007

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.





<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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!
>



 
Reply With Quote
 
vMike
Guest
Posts: n/a
 
      05-15-2007

"vMike" <(E-Mail Removed)> wrote in message
newsTj1i.668$(E-Mail Removed)...
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> 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


 
Reply With Quote
 
vMike
Guest
Posts: n/a
 
      05-15-2007

"vMike" <(E-Mail Removed)> wrote in message
news:Yka2i.1819$(E-Mail Removed)...
>
> "vMike" <(E-Mail Removed)> wrote in message
> newsTj1i.668$(E-Mail Removed)...
>>
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>>> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About datatables Ravindra ASP .Net 1 12-30-2005 12:33 PM
Compare Two DataTables Simple Simon ASP .Net 3 12-15-2003 05:36 AM
Datatable based on two other datatables (inner join) Gene Ariani ASP .Net 0 08-21-2003 10:46 PM
Re: Combining DataTables Kevin Spencer ASP .Net 0 07-21-2003 05:07 PM
set up a datagrid of multiples datatables in 1 dataset Richard Loupatty ASP .Net 2 07-18-2003 08:54 PM



Advertisments