Solved

create two datatable from single datatable

Posted on 2006-11-15
10
323 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:nanban
  • 5
  • 5
10 Comments
 
LVL 13

Expert Comment

by:newyuppie
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");
dataset1.Tables.Add(newTable1);
dataset1.Tables.Add(newTable2);

0
 

Author Comment

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

Accepted Solution

by:
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
0
 

Author Comment

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

Expert Comment

by:newyuppie
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;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:nanban
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?
0
 
LVL 13

Expert Comment

by:newyuppie
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.
0
 

Author Comment

by:nanban
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 :-) ?
Regards
Nanban.
0
 
LVL 13

Expert Comment

by:newyuppie
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.
0
 

Author Comment

by:nanban
ID: 17970104
Hi
I have created a new thread for this...
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_22065240.html

thanks a lot.....

Regards,
Nanban.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is Thread.Sleep? 5 46
Need to pass a connection through class/Method 1 39
Export import database 4 42
Server Error 11 47
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now