C# .NET - How to add a datatable to an existing dataset?

Asked By Anish V on 13-Nov-08 01:59 AM
Dear All, Does anyone know "How to add a datatable to an existing dataset?". If yes please help me at the earliest. Thanks in advance Anish

see this - Binny ch replied to Anish V on 13-Nov-08 02:02 AM

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 };

RE - Web Star replied to Anish V on 13-Nov-08 02:08 AM

you cannot CONVERT DataTable to DataSet. DataSet is a http://www.velocityreviews.com/forums/t302098-datatable-to-dataset.html#
that can contain 0 or more DataTable. If you have to place a DataTable into
a DataSet, you can

DataSet ds=new DataSet();
ds.Tables.Add(myDataTable);

REply - Binny ch replied to Anish V on 13-Nov-08 02:08 AM

'datatables repository
            Dim dsAddresses As DataSet = New DataSet
Dim Relation As DataRelation

'Create the Address datatable
dsAddresses.Tables.Add(GetAddress(Case_Number))
dsAddresses.Tables(0).TableName = "Address"

'Create Registered Agent Datatable
dsAddresses.Tables.Add(GetRegisteredAgent(Case_Number))
dsAddresses.Tables(1).TableName = "RegisterAgent"

'Create Lawyer Datatable
dsAddresses.Tables.Add(GetAttorney(Case_Number))
dsAddresses.Tables(2).TableName = "Lawyer"

'create relation betwen Address and Registered Agent tables
Relation = New DataRelation("CASE_NUMBER", dsAddresses.Tables("Address").Columns("CASE_NUMBER"), dsAddresses.Tables("RegisterAgent").Columns("CASE_NUMBER"))
dsAddresses.Relations.Add(Relation)

'creates relation between address and lawyer tables
Relation = New DataRelation("CASE_NUMBER_Lawyer", dsAddresses.Tables("Address").Columns("CASE_NUMBER"), dsAddresses.Tables("Lawyer").Columns("CASE_NUMBER"))
dsAddresses.Relations.Add(Relation)

'return the datatables repository
Return dsAddresses


See thses links:
http://msdn.microsoft.com/en-us/library/6zd7cwzh.aspx

http://www.experts-exchange.com/Programming/Languages/.NET/Q_23842003.html



How to add a datatable to an existing dataset? - Perry replied to Anish V on 13-Nov-08 02:10 AM

You can use below line:

ds.Tables.Add(dt);

Also see  http://msdn.microsoft.com/en-us/library/aeskbwf7.aspx  for explanation.

Re :: Add a datatable to an existing dataset - Shailendrasinh Parmar replied to Anish V on 13-Nov-08 04:24 AM

if you already have a dataset and want to append more datatables you can use:

MyDataset.Tables.Add(NewTable);

also see the following article (But it is for adding first table, but you can use the same way to add more tables) ::

http://www.expertrating.com/courseware/DotNetCourse/DotNet-ASP.Net-9-2.asp

Hope it helps.

read this - C_A P replied to Anish V on 13-Nov-08 08:50 AM
Dim ds as New Dataset
Dim dt as New DataTable

'setup your datatable here

ds.Tables.Add (dt)
'you can now destroy the datatable and access the same set of data from the created dataset

http://msdn.microsoft.com/en-us/library/aeskbwf7.aspx
http://www.eggheadcafe.com/community/aspnet/14/57037/create-a-dataset-and-add.aspx
try this code - C_A P replied to Anish V on 13-Nov-08 08:55 AM
You need 2 relations, one that connects address and registeredagent and a second one to connect Address to Lawyer

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
  'datatables repository
Dim dsAddresses As DataSet = New DataSet
Dim Relation As DataRelation

'Create the Address datatable
dsAddresses.Tables.Add(GetAddress(Case_Number))
dsAddresses.Tables(0).TableName = "Address"

'Create Registered Agent Datatable
dsAddresses.Tables.Add(GetRegisteredAgent(Case_Number))
dsAddresses.Tables(1).TableName = "RegisterAgent"

'Create Lawyer Datatable
dsAddresses.Tables.Add(GetAttorney(Case_Number))
dsAddresses.Tables(2).TableName = "Lawyer"

'create relation betwen Address and Registered Agent tables
Relation = New DataRelation("CASE_NUMBER", dsAddresses.Tables("Address").Columns("CASE_NUMBER"), dsAddresses.Tables("RegisterAgent").Columns("CASE_NUMBER"))
dsAddresses.Relations.Add(Relation)

'creates relation between address and lawyer tables
Relation = New DataRelation("CASE_NUMBER_Lawyer", dsAddresses.Tables("Address").Columns("CASE_NUMBER"), dsAddresses.Tables("Lawyer").Columns("CASE_NUMBER"))
dsAddresses.Relations.Add(Relation)

'return the datatables repository
Return dsAddresses

source from http://www.experts-exchange.com/Programming/Languages/.NET/Q_23842003.html
Add Data table to dataset - Raja muthukrishnan replied to Anish V on 08-Jan-09 07:13 AM

string cstring = string.Empty;

cstring = @"data source=localhost;user id=sa;password=sa123; initial catalog=SkillSet;";

SqlConnection sqlCnn = new SqlConnection(cstring);

SqlCommand cmdPr = new SqlCommand("select * from projects", sqlCnn);

SqlCommand cmdLW = new SqlCommand("select * from LastWorked", sqlCnn);

DataSet ds = new DataSet("SkillSet");

DataTable tdPr = new DataTable("Projects");

DataTable tdLW = new DataTable("LastWorked");

SqlDataAdapter ProjectsDA = new SqlDataAdapter();

SqlDataAdapter LastWorkedDA = new SqlDataAdapter();

ProjectsDA.SelectCommand = cmdPr;

LastWorkedDA.SelectCommand = cmdLW;

ProjectsDA.Fill(tdPr);

LastWorkedDA.Fill(tdLW);

ds.Tables.Add(tdPr);

ds.Tables.Add(tdLW);