Solved

create two datatable from single datatable

Posted on 2006-11-15
10
321 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If i want to add autoincrement column with the table how to do that?
0
 
LVL 13

Expert Comment

by:newyuppie
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:nanban
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

13 Experts available now in Live!

Get 1:1 Help Now