How to add multiple tables in a single Dataset?

This code snippet is used to add multiple tables in a single data set.
Source Code:


DataSet ds = new DataSet();
DataTable dt = new DataTable("Student"); //Creating first table named "Student"
ds.Tables.Add(dt); //Adding the first table "Student" in dataset object "ds"

DataSet ds1 = new DataSet();
DataTable dt1 = new DataTable("Emp"); //Creating second table named "Emp"
ds1.Tables.Add(dt1); // Adding the second table "Emp" in dataset object "ds1"

DataTable tmpDt = dt; //Creating a temporary table to store all the tables
ds.Tables.Remove(dt); //Removing the table "Student" from it's assigned ds

ds1.Tables.Add(tmpDt); //Adding the table "Student" to other dataset ds1 by
//replacing the previous dataset ds.

Response.Write("The Following tables are present in dataset ds1

");

//Checking and displaying the tables inserted in dataset ds1
foreach (DataTable t in ds1.Tables)
{
Response.Write(t.TableName + "
");
}

// The output for the above code will be:

The Following tables are present in dataset ds1
Emp
Student

Code Description:
Here in the code we are replacing the previous dataset to add both the tables in another dataset.


Comments

Author: Kirankumar Potnuru08 Jun 2009 Member Level: Gold   Points : 1

Hi This article seems confusing .
Can you explain in theoretical after that give sample example.

Author: Laxmi Farswan09 Jun 2009 Member Level: Silver   Points : 2

Hi,

The sample code provided above can be referred where there would be a requirement of adding more than one table in a single dataset object.

As you must be familiar with the fact that we do create different dataset objects in order to store different tables.

In case you would get a situation to display records from different tables in a single DataGrid/Gridview. There you would require to club mutiple tables in a single dataset object.

Regarding my code above, once you will get both the tables (Emp, Student) in dataset ds1. You can easily bind both the tables to DataGrid/GridView with the help of a single dataset object ds1.

Hope you will understand now whatever I tried to explain.

Please feel free to ask if any further clarification required.

Thanks & Regards,
Laxmi

Guest Author: Usman11 Feb 2013

HI I have multiple Tables in one Form,Such As Peron, Education1, Education2, etc,,

How can add these to Data Base when user enters information..

Thanks

Guest Author: Praveena27 Feb 2013

Thanks it helped me

Author: praveena27 Feb 2013 Member Level: Bronze   Points : 0

Thanks it helped me



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: