Link to home
Start Free TrialLog in
Avatar of nanban
nanban

asked on

create two datatable from single datatable

Hi,
 I have a dataset datatable
select ORDER ,
         DESCRIPTION,
      FIRSTNAME,
     LASTNAME,
      ADDRESS,
      ZIP
     FROM Order

I need to select only Order and description and insert in to the another datatable
then select other firstname,lastname,address,zip to another datatable and assign this two datatable to new dataset
how to do that?
regards.
Nanban
Avatar of newyuppie
newyuppie
Flag of Ecuador image

you would have to create a dataview, then you can create datatables from there:

DataView dv = new DataView(datatable);

DataTable newTable1 = dv.ToTable("OrderDesc",
             false, new string[] { "ORDER", "DESCRIPTION" });
DataTable newTable2 = dv.ToTable("Rest",
             false, new string[] { "FIRSTNAME", "LASTNAME", "ADDRESS", "ZIP" });

now you need to create a dataset and add these two new tables.
DataSet dataset1 = new DataSet("MyNewDataset");
dataset1.Tables.Add(newTable1);
dataset1.Tables.Add(newTable2);

Avatar of nanban
nanban

ASKER

What is totable?
I thought only using datarow we can get the required column and send in to table.
ASKER CERTIFIED SOLUTION
Avatar of newyuppie
newyuppie
Flag of Ecuador image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nanban

ASKER

If i want to add autoincrement column with the table how to do that?
to add an autoincrement column to a datatable you would do something like:


DataColumn workColumn = newTable2.Columns.Add("CustomerID", typeof(Int32));
workColumn.AutoIncrement = true;
workColumn.AutoIncrementSeed = 1;
workColumn.AutoIncrementStep = 1;
Avatar of nanban

ASKER

When we are using autoincrement column after totable the previous column dont have any value so how to add the b4 totable. if are adding autoincrement column b4 totable then calling totable delete the autoincrement column. what to do for that?
with the technique i posted add the autoincrement replacing newTable2 with the name of your original datatable. then proceed with the totable technique and because the way we constructed it it will drop the autocolumn and the new tables wont have it.
Avatar of nanban

ASKER

Actually what is the problem is I need to create two auto increament column one is orderid and another one is contackkey and i need to releate the newly created table with primarykey foreignkey relationship so newly created order table has four coulmns contatkey,orderid,ORDER,DESCRIPTION first i need to insert contact table and then with the autogenerated column contackkey i need to insert in to order table with relationship

Shall i create the new thread or proceed with this thread... coz already we closed the thread :-) ?
Regards
Nanban.
i believe the proper way to do it would be to create a new thread, in any case to get help from other experts as well. just put your new question and a link to this one if people want to see where you stand.
Avatar of nanban

ASKER

Hi
I have created a new thread for this...
https://www.experts-exchange.com/questions/22065240/one-dataset-table-to-multiple-dataset-table.html

thanks a lot.....

Regards,
Nanban.