Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a datatable in a dataset

I'm adding a datatable to a dataset like this:

DataTable dtImage = new DataTable();
//some updates in the Datatable
ds.Tables.Add(dtImage);

But the next time, when the datatable gets updated, will it be reflected in the dataset? or we need to write some code to make it reflected?

Also, I'm checking the dataset if the datatable exists in the dataset already using:

if(!ds.Tables.Contains("dtImage"))
    ds.Tables.Add(dtImage);

In the first iteration, ds.Tables.Contains("dtImage") is false, so, ds.Tables.Add(dtImage) adds the table to the dataset. But in the second iteration, ds.Tables.Contains("dtImage") is false again, but ds.Tables.Add(dtImage) throws an error:

Datatable already belongs to this dataset.

If the dataset doesn't contain the datatable named "dtImage", why is it throwing an error?

Update: Thanks, that issue got solved. Pls answer this:

But the next time, when the datatable gets updated, will it be reflected in the dataset? or we need to write some code to make it reflected?

like image 578
Manikandan Sigamani Avatar asked Aug 29 '12 13:08

Manikandan Sigamani


People also ask

What is the syntax to add table in DataSet object?

var tbl = new DataTable("dtImage");

How many DataTables are in a DataSet?

So 2^32 is the maximum number of DataTables you can hold inside a DataSet (2,147,483,648 tables, happy coding). By the way, a DataTable can contain 16,777,216 rows, but that's not what you've asked. Show activity on this post. The maximum size is limited by Int32 is 2^32, since Tables.

How can we load multiple tables in a DataSet?

Filling a DataSet with multiple tables can be done by sending multiple requests to the database, or in a faster way: Multiple SELECT statements can be sent to the database server in a single request. The problem here is that the tables generated from the queries have automatic names Table and Table1.

What is the difference between DataSet and DataTable?

A DataTable object represents tabular data as an in-memory, tabular cache of rows, columns, and constraints. The DataSet consists of a collection of DataTable objects that you can relate to each other with DataRelation objects.


1 Answers

I assume that you haven't set the TableName property of the DataTable, for example via constructor:

var tbl = new DataTable("dtImage");

If you don't provide a name, it will be automatically created with "Table1", the next table will get "Table2" and so on.

Then the solution would be to provide the TableName and then check with Contains(nameOfTable).

To clarify it: You'll get an ArgumentException if that DataTable already belongs to the DataSet (the same reference). You'll get a DuplicateNameException if there's already a DataTable in the DataSet with the same name(not case-sensitive).

http://msdn.microsoft.com/en-us/library/as4zy2kc.aspx

like image 195
Tim Schmelter Avatar answered Oct 02 '22 20:10

Tim Schmelter