Adding a row to a dataset


G

Guest

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
 
Ad

Advertisements

E

Earl

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.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top