I have a need in my application to add a dummy row to a dataset object after
I have filled it with a call to the database. Trying to find a solution I
thought that doing a dataset merge might work, however its doesn't seem to
be. Following examples I found in some MSDN articles I came up with the
following code:

DataSet ds = new DataSet();
DataSet dsTemp = new DataSet();
DataRow dr;
DataTable dt = new DataTable("tempTable");
DataColumn dc = new DataColumn("VALUE",Type.GetType("System.String"));
dt.Columns.Add(dc);
DataColumn dc1 = new DataColumn("TEXT",Type.GetType("System.String"));
dt.Columns.Add(dc1);
dr = dt.NewRow();
dr["VALUE"] = "test";
dr["TEXT"] = "test";
dt.Rows.Add(dr);
dsTemp.Tables.Add(dt);
dsTemp.AcceptChanges();

ds = functionThatReturnsData();
ds.Merge(dsTemp);
return ds;

In the above example, my expectations where that ds would contain both its
data and the data from dsTemp. However it only contains the data returned
from the db call. If I switch it around to: dsTemp.Merge(ds), dsTemp only
contains its original data and nothing from ds.

Am I just missing something here or am I way off base?

thanks

Re: Adding a row to a dataset by Earl

Earl
Sat May 28 02:50:30 CDT 2005

If you are just adding a row of data to a table, you do not need a temp
table. Your dataset "knows" the schema for your existing table, so smply add
a row as follows:

Instantiate a new row;
Add values to the EXISTING columns in the row;
Add the new row to the EXISTING table;

Note that if you add columns, you are either extending out the existing
table or in the case of the temp table, creating an entirely different table
structure. I do not use Merge, but this is likely why your merge did not
work.

"Mike" <Mike@discussions.microsoft.com> wrote in message
news:03D9DEA3-C598-405D-908D-7C625DE79697@microsoft.com...
>I have a need in my application to add a dummy row to a dataset object
>after
> I have filled it with a call to the database. Trying to find a solution I
> thought that doing a dataset merge might work, however its doesn't seem to
> be. Following examples I found in some MSDN articles I came up with the
> following code:
>
> DataSet ds = new DataSet();
> DataSet dsTemp = new DataSet();
> DataRow dr;
> DataTable dt = new DataTable("tempTable");
> DataColumn dc = new DataColumn("VALUE",Type.GetType("System.String"));
> dt.Columns.Add(dc);
> DataColumn dc1 = new DataColumn("TEXT",Type.GetType("System.String"));
> dt.Columns.Add(dc1);
> dr = dt.NewRow();
> dr["VALUE"] = "test";
> dr["TEXT"] = "test";
> dt.Rows.Add(dr);
> dsTemp.Tables.Add(dt);
> dsTemp.AcceptChanges();
>
> ds = functionThatReturnsData();
> ds.Merge(dsTemp);
> return ds;
>
> In the above example, my expectations where that ds would contain both its
> data and the data from dsTemp. However it only contains the data returned
> from the db call. If I switch it around to: dsTemp.Merge(ds), dsTemp only
> contains its original data and nothing from ds.
>
> Am I just missing something here or am I way off base?
>
> thanks
>



Re: Adding a row to a dataset by Cor

Cor
Sat May 28 03:34:41 CDT 2005

Mike,

In addition to Earl, have a look at the datatable.clone

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatatableclassclonetopic.asp

I hope this helps,

Cor