Click here to Skip to main content
12,253,679 members (57,738 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi

I want to get the data from datatable into a dataset with there relationship.

Dataset contains number of table and datatable contains all data fetched by some query.

I want to add that datatable data to all table inside the dataset


Any idea??
Posted 28-Nov-12 3:48am
asbis1.3K
Comments
Krunal R 28-Nov-12 10:38am
   
could you please elaborate your question ?
Maksud Saifullah Pulak 28-Nov-12 14:43pm
   
DataSet customerOrders = new DataSet("CustomerOrders");

DataTable ordersTable = customerOrders.Tables.Add("Orders");

DataColumn pkOrderID =
ordersTable.Columns.Add("OrderID", typeof(Int32));
ordersTable.Columns.Add("OrderQuantity", typeof(Int32));
ordersTable.Columns.Add("CompanyName", typeof(string));

ordersTable.PrimaryKey = new DataColumn[] { pkOrderID };

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

\hope my code can help :
DataTable ordertable = new DataTable();
 

           DataColumn orderID = new DataColumn("orderID");
           DataColumn userName = new DataColumn("userName");
           DataColumn cudID = new DataColumn("cudID");
           DataColumn cusName = new DataColumn("cusName");
           DataColumn orderDate = new DataColumn("orderDate");
           DataColumn Payment = new DataColumn("payment");
           DataColumn orderTotal = new DataColumn("orderTotal");
 

           //Define DataType of the Columns
           orderID.DataType = System.Type.GetType("System.String");
           userName.DataType = System.Type.GetType("System.String");
           cudID.DataType = System.Type.GetType("System.String");
           cusName.DataType = System.Type.GetType("System.String");
           orderDate.DataType = System.Type.GetType("System.String");
           Payment.DataType = System.Type.GetType("System.String");
           orderTotal.DataType = System.Type.GetType("System.String");
 

           //Add All These Columns into Dataordertable ordertable
           ordertable.Columns.Add(orderID);
           ordertable.Columns.Add(userName);
           ordertable.Columns.Add(cudID);
           ordertable.Columns.Add(cusName);
           ordertable.Columns.Add(orderDate);
           ordertable.Columns.Add(Payment);
           ordertable.Columns.Add(orderTotal);
 

           //Create a Row in the Dataordertable ordertable
           UserInfo searchuser = new UserInfo();
           foreach (OrderInfo order in sorderlist)
           {
               DataRow row = ordertable.NewRow();
 
               //Fill All Columns with Data
               row[orderID] = order.ID;
 
               searchuser = userList.Find(user => user.ID == order.userID);
               row[userName] = searchuser.Name;
               row[cudID] = order.cusID;
               CustomerInfo search = new CustomerInfo();
               search = cusList.Find(cus => cus.ID == order.cusID);
               row[cusName] = search.Name;
               row[orderDate] = order.Date;
               row[Payment] = order.paymentType;
               row[orderTotal] = order.Total;
 

                      ordertable.Rows.Add(row);
 
           }
 
           DataTable detailstable = new DataTable();
 

           DataColumn orderdID = new DataColumn("orderdID");
           DataColumn itemID = new DataColumn("itemID");
           DataColumn itemCode = new DataColumn("itemName");
           DataColumn itemPrice = new DataColumn("itemPrice");
 

           //Define DataType of the Columns
           orderdID.DataType = System.Type.GetType("System.String");
           itemID.DataType = System.Type.GetType("System.String");
           itemCode.DataType = System.Type.GetType("System.String");
           itemPrice.DataType = System.Type.GetType("System.String");
 

           //Add All These Columns into DataTable table
           detailstable.Columns.Add(orderdID);
           detailstable.Columns.Add(itemID);
           detailstable.Columns.Add(itemCode);
           detailstable.Columns.Add(itemPrice);
 

           //Create a Row in the DataTable table

           foreach (OrderDetails order in orderDetailsList)
           {
               int index = orderlist.FindIndex(item => item.ID == order.OrderID);
 
               if (index >= 0)
               {
                   // element exists, do what you need
                   DataRow row = detailstable.NewRow();
 
                   //Fill All Columns with Data
                   row[orderdID] = order.OrderID;
                   row[itemID] = order.ItemID;
                   //  item = SQLComm.GetItemFromCode(order.ItemID.ToString());
                   ItemInfo search = new ItemInfo();
                   search = items.Find(item => item.ID == order.ItemID);
                   row[itemCode] = search.Name;
                   row[itemPrice] = search.SalePrice;
 
                   if (SSOption.LoginLevel == 0)
                   {
                       //Add the Row into Dataordertable
                       detailstable.Rows.Add(row);
                   }
                   else if (SSOption.LoginUsername ==SQLComm.GetUserFormID(SQLComm.GetUserFromOrder(order.OrderID)).Name)
                   {
                       detailstable.Rows.Add(row);
                   }
 
               }
 
           }
           //Want to bind this DataTable to a GridView?
           DataSet dsorders = new DataSet("Orders");
           dsorders.Tables.Add(ordertable);
           dsorders.Tables.Add(detailstable);
           DataColumn colchild = dsorders.Tables[1].Columns[0];
           DataRelation drOrderID = new DataRelation("ordersID", dsorders.Tables[0].Columns[0], colchild);
           dsorders.Relations.Add(drOrderID);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100