create two datatable from single datatable

Posted on 2006-11-15
Last Modified: 2010-04-16
 I have a dataset datatable
select ORDER ,
     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?
Question by:nanban
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
LVL 13

Expert Comment

ID: 17953816
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");


Author Comment

ID: 17953880
What is totable?
I thought only using datarow we can get the required column and send in to table.
LVL 13

Accepted Solution

newyuppie earned 50 total points
ID: 17953892
totable is a method of dataview that extracts the info to a datatable. look at the MSDN docs to find out how it works.
nice thing about ToTable is that it extract only certain columns as you can see from what i posted. it avoids loops that you may need to write otherwise
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 17954479
If i want to add autoincrement column with the table how to do that?
LVL 13

Expert Comment

ID: 17955284
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;

Author Comment

ID: 17963697
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?
LVL 13

Expert Comment

ID: 17963950
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.

Author Comment

ID: 17964024
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 :-) ?
LVL 13

Expert Comment

ID: 17964052
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.

Author Comment

ID: 17970104
I have created a new thread for this...

thanks a lot.....


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help with another query 10 50
Web page design problem 3 39
.NET Redemption/Outlook Examples 3 25
C#line chart with data on Y and time on X-axis 3 28
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question