none
cannot split dataset into tables RRS feed

  • Question

  • hi,

    i have fetched data from the server and filled it into the dataset; now i want to separately store the data i fetched into two tables inside this dataset. how can i do this?

    Wednesday, August 18, 2010 2:10 PM

Answers

  • Try like below

      DataTable dt = new DataTable();
          dt.Columns.Add(new DataColumn("ID", typeof(int)));
          dt.Columns.Add(new DataColumn("Name", typeof(string)));
          dt.Columns.Add(new DataColumn("Supervisor", typeof(string)));
          dt.Columns.Add(new DataColumn("Active", typeof(int)));
          dt.Columns.Add(new DataColumn("UnitId", typeof(int)));
          dt.Columns.Add(new DataColumn("GroupId", typeof(int)));
          dt.Columns.Add(new DataColumn("GroupName", typeof(string)));
    
          DataRow dr = dt.NewRow();
          dr["ID"] = 17;
          dr["Name"] = string.Empty;
          dr["Supervisor"] = string.Empty;
          dr["Active"] = 1;
          dr["UnitId"] = 129154;
          dr["GroupId"] = 2;
          dr["GroupName"] = "RMS Users";
          dt.Rows.Add(dr);
    
          DataView dv = new DataView(dt);
          DataTable table1=dv.ToTable(true, "ID", "Name", "Supervisor");
          DataTable table2 = dv.ToTable(true, "Active", "UnitId", "GroupId", "GroupName");
    Dataset.Tables.Add(table1); //If you want to assign to dataset
    Dataset.Tables.Add(table2); //If you want to assign to dataset 
    


    Nanda - Misys Software Solutions,Bangalore
    • Proposed as answer by Shailesh B Davara Thursday, August 19, 2010 12:35 PM
    • Marked as answer by nishanreads Thursday, August 19, 2010 12:36 PM
    Thursday, August 19, 2010 7:25 AM

All replies

  • >  into the dataset; now i want to separately store the data i fetched into two tables inside this dataset.

    Confused.  Sounds like you've already got the data in the DataSet containing the tables.  Please describe more precisely what you have and what you want.

     

    Wednesday, August 18, 2010 11:40 PM
  • And how about posting a little bit of code to illustrate what you've already done.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thursday, August 19, 2010 4:34 AM
  • u r rite...i ve got the the data into the dataset, now i want to separate it into two tables...

     

    like

    dataset.table[0] and

    dataset.table[1]

    ...the table i have fetched from the db has 8 coulmns and i want to separate it as A 3 column table and A 5 column table...

     

     

    Thursday, August 19, 2010 5:52 AM
  • Try like below

      DataTable dt = new DataTable();
          dt.Columns.Add(new DataColumn("ID", typeof(int)));
          dt.Columns.Add(new DataColumn("Name", typeof(string)));
          dt.Columns.Add(new DataColumn("Supervisor", typeof(string)));
          dt.Columns.Add(new DataColumn("Active", typeof(int)));
          dt.Columns.Add(new DataColumn("UnitId", typeof(int)));
          dt.Columns.Add(new DataColumn("GroupId", typeof(int)));
          dt.Columns.Add(new DataColumn("GroupName", typeof(string)));
    
          DataRow dr = dt.NewRow();
          dr["ID"] = 17;
          dr["Name"] = string.Empty;
          dr["Supervisor"] = string.Empty;
          dr["Active"] = 1;
          dr["UnitId"] = 129154;
          dr["GroupId"] = 2;
          dr["GroupName"] = "RMS Users";
          dt.Rows.Add(dr);
    
          DataView dv = new DataView(dt);
          DataTable table1=dv.ToTable(true, "ID", "Name", "Supervisor");
          DataTable table2 = dv.ToTable(true, "Active", "UnitId", "GroupId", "GroupName");
    Dataset.Tables.Add(table1); //If you want to assign to dataset
    Dataset.Tables.Add(table2); //If you want to assign to dataset 
    


    Nanda - Misys Software Solutions,Bangalore
    • Proposed as answer by Shailesh B Davara Thursday, August 19, 2010 12:35 PM
    • Marked as answer by nishanreads Thursday, August 19, 2010 12:36 PM
    Thursday, August 19, 2010 7:25 AM
  • hello,

    vekata has provided right solution. as in dataset if your assign your table with one of dataset table it just refers as they are references. you need to create new table and add rows to new table manually then you will have separate standalone copy. Else change in one would reflect in another one. 


    Thanks and Regards, Shailesh B. Davara sdavara.com
    Thursday, August 19, 2010 12:35 PM
  • Thanks a lot, this was exactly wat i was looking for.
    Thursday, August 19, 2010 12:37 PM
  • You still haven't posted the code that shows how you were getting the data into the DataSet, but, depending on your needs and how you plan to use the data in the DataSet, another more efficient way is to put it directly into two DataTables when you retrieve the data by using 2 selects in the SelectCommand of your DataAdapter:

    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(select a, b, c from MyTable; select d, e, f from MyTable", MyConnectionString);
    da.Fill(ds);
    
    This will create two DataTables in your DataSet (named Table and Table1). Table will contain the data from columns a,b and c and Table1 will contain the data from columns d,e and f.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thursday, August 19, 2010 3:35 PM