Solved

create two datatable from single datatable

Posted on 2006-11-15
10
324 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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
 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System.Speech 2 28
C#.NET and microsoft certification. 3 41
Where can I ask about my ASP.NET MVC project? 6 36
Best resource to learn entity framework 2 35
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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