You must Sign In to post a response.
  • Category: ASP.NET

    Combine Three tables within dataset

    How to combine two tables in dataset and make it single

    Example..

    My dataset has records in ds.Tables[0] and ds.Tables[1] and ds.Tables[2].

    I want all these records in single dataset(ds.Tables[0])
  • #768132
    Hi,

    You have many ways to join datatables into single dataset.

    if you are using adapter coding you can fill the table by following way,

    adapter.Fill(ds,"Table(1)"); //ds is dataset and table name is the next parameter
    adapter.Fill(ds,"Table(2)");


    You can also use Merge function to join the tables in Dataset

    ds.Tables[2].Merge(ds.Tables[1]);

    Thanks,
    Mani

  • #768133
    Hi Lily,

    If both datasets table columns are similar in structure, we can make use of dataset.Merge method.
    Refer below..

    For Ex:
    DataSet ds1 = new DataSet();
    DataSet ds2 = new DataSet();
    SqlDataAdapter da= new SqlDataAdapter();
    da= new SqlDataAdapter("SELECT * FROM FirstTable", Conn);
    da.Fill(ds1, "Table");
    da= new SqlDataAdapter("SELECT * FROM SecondTable", Conn);
    da.Fill(ds2, "Table");
    ds1.Merge(ds2);

    Is this you are expecting. or something different, Hope this will help you

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring

  • #768354
    You can Combine Three tables within dataset like this. Before that please verify that the tables which you are going to merge have the similar structure.

    //DataSet ds <-- you dataset

    foreach(var t in ds.Tables.Skip(1))
    {
    t.Merge(ds.Tables[0]);
    //All the tables should me merged to the one table which is at the position 0
    }

    You can see more details about Merge method from here.
    http://msdn.microsoft.com/en-us/library/system.data.datatable.merge(v=vs.110).aspx
    For any other .net related query, you can also contact our asp.net developer http://www.teaminindia.co.uk/hire-asp.net-developer.htm

  • #768362
    There are multiple methods exist to combine multiple dataset to datatables and vice versa.
    you can use DataSet.Merge Method which Merges a specified DataTable and its schema into the current DataSet, or DataTable.Merge Method which Merge the specified DataTable with the current DataTable.
    fetch all datatable all dataset and merge them all
    check out below snippet

    private void BindGridWithMergeTables()
    {
    DataTable dt1 = new DataTable();
    DataTable dt2 = new DataTable();

    dt1.Columns.Add("ID");
    dt2.Columns.Add("ID");

    DataRow dr1 = dt1.NewRow();
    DataRow dr2 = dt2.NewRow();

    dr1["ID"] = "1";
    dr2["ID"] = "2";
    dt1.Rows.Add(dr1);
    dt2.Rows.Add(dr2);

    DataTable dtAll = new DataTable();
    dtAll = dt1.Copy();
    dtAll.Merge(dt2, true);
    dataGridView1.DataSource = dtAll;
    dataGridView1.DataBind();
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768369
    Hi,
    Try this:

    ds.Tables[0].Merge(ds.Tables[1]);
    ds.Tables[0].Merge(ds.Tables[2]);
    --Then Use
    ds.Tables[0] ;
    Or
    DataTable dt = ds.Tables[0];

  • #768370
    You can use this code snippet to combine three tables within dataset
     private void DemonstrateMerge() 
    {
    DataSet dataSet = new DataSet("dataSet");
    DataTable table = new DataTable("Product");
    DataColumn idColumn = new DataColumn("id",
    Type.GetType("System.Int32"));
    idColumn.AutoIncrement=true;
    DataColumn itemColumn = new DataColumn("Product", Type.GetType("System.Int32"));
    DataColumn[] keyColumn= new DataColumn[1];
    DataRow row;
    DataSet changeDataSet;
    table.Columns.Add(idColumn);
    table.Columns.Add(itemColumn);
    dataSet.Tables.Add(table);
    keyColumn[0]= idColumn;
    table.PrimaryKey=keyColumn;
    for(int i = 0; i <10;i++)
    {
    row=table.NewRow();
    row["Product"]= i;
    table.Rows.Add(row);
    }


Sign In to post your comments