locked
Merge 2 dataset into single dataset. how to do RRS feed

  • Question

  • User-148788041 posted

    Hi,

    I have 2 dataset how  to merge or join 2 datset into single one.

    DataSet ds  = new DataSet()
    DataSet ds1= new DataSet()
    DataSet dsResult = new DataSet()
    dsResult= ds & ds1
    Wednesday, November 29, 2017 3:11 AM

All replies

  • User991499041 posted

    Hi Guhananth,

    Hi,

    I have 2 dataset how  to merge or join 2 datset into single one.

    DataSet ds  = new DataSet()
    DataSet ds1= new DataSet()
    DataSet dsResult = new DataSet()
    dsResult= ds & ds1

    You can use the Merge method to merge the contents of a DataSet, DataTable, or DataRow array into an existing DataSet.

    DataSet ds = new DataSet();
    DataSet ds1 = new DataSet();
    ds.Merge(ds1);

    Regards,

    zxj

    Wednesday, November 29, 2017 5:57 AM
  • User-707554951 posted

    Hi guhananth,

    From your description, you want copy datatable in your two datasets to a single dataset;

    If that the case, following working sample for your reference:

     <asp:GridView ID="GridView1" runat="server"></asp:GridView>
                <hr />
                <asp:GridView ID="GridView2" runat="server"></asp:GridView>
      protected void Page_Load(object sender, EventArgs e)
            {
                DataTable t1 = new DataTable();
                t1.Columns.Add("key1");
                t1.Columns.Add("c1");
                t1.PrimaryKey = new[] { t1.Columns["key1"] };
                t1.Rows.Add(1, "data1");
                t1.TableName = "table1";
    
                // This is table two. 
                DataTable t2 = new DataTable();
                t2.Columns.Add("key1");
                t2.Columns.Add("c2");
                t2.PrimaryKey = new[] { t2.Columns["key1"] };
                t2.TableName = "table2";
                t2.Rows.Add(1, "data2");
                DataSet dataSet = new DataSet("dataSet");
                dataSet.Tables.Add(t1);
                DataSet dataSet2 = new DataSet("dataSet");
                dataSet2.Tables.Add(t2);
                DataSet dsResult = new DataSet("dsResult");
                foreach(DataTable dt in dataSet.Tables)
                {
                    dsResult.Tables.Add(dt.Copy());
                }
                foreach (DataTable dt2 in dataSet2.Tables)
                {
                     dsResult.Tables.Add(dt2.Copy());
                }
                GridView1.DataSource = dsResult.Tables[0];
                GridView1.DataBind();
                GridView2.DataSource = dsResult.Tables[1];
                GridView2.DataBind();
    }

    Output:

    Besides, you could learn basic knowledge by referring to the following links;

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/

    https://www.dotnetperls.com/dataset

    http://csharp.net-informations.com/dataset/csharp-dataset-tutorial.htm

    Best regards

    Cathy

    Thursday, November 30, 2017 5:35 AM