Joining 2 DataTables

J

JoNo216

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!
 
G

George Ter-Saakov

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.
 
V

vMike

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.
 
G

GroupReader

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.
 
J

JoNo216

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!
 
S

sloan

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.
 
V

vMike

vMike said:
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
 
V

vMike

vMike said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top