Sahil
Wed May 25 17:06:38 CDT 2005
Phil,
You can do something like this -
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable dt = GetDataTable();
dt.LoadDataRow(new Object[] { 0, "One", "Two" },
LoadOption.Upsert);
dt.LoadDataRow(new Object[] { (int)dt.Compute("MAX (PK) + 1",
""), "One", "Two" }, true);
dt.LoadDataRow(new Object[] { (int)dt.Compute("MAX (PK) + 1",
""), "One", "Two" }, true);
dt.LoadDataRow(new Object[] { (int)dt.Compute("MAX (PK) + 1",
""), "One", "Two" }, true);
}
static DataTable GetDataTable()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("PK");
dc.AutoIncrement = true;
dc.Unique = true;
dt.Columns.Add(dc);
dt.Columns.Add(new DataColumn("X"));
dt.Columns.Add(new DataColumn("Y"));
return dt;
}
}
}
Do note however that this will run hella slow - considering it is computing
the max everytime.
Enjoy !!
- Sahil Malik [MVP]
http://codebetter.com/blogs/sahil.malik/
"Phil Galey" <pagaley@starcalif.com.nospam> wrote in message
news:eC2QKwWYFHA.3712@TK2MSFTNGP09.phx.gbl...
> I am trying to add a row to a table in a dataset. The table contains an
ID
> field, which is set up as an AutoIncrement field, with AutoIncrementSeed =
1
> and AutoIncrementStep = 1. I like the convenience of the following
syntax:
>
> MyDataset.Tables("MyTable").Rows.Add(New Object(){"data for field1", "data
> for field2", "data for field3"...})
>
> but because the first field is an AutoIncrement field, I'm not sure what
to
> put in place of the first field. I tried vbNull, but that doesn't help.
It
> complains on the second add that the ID already exists. How do I deal
with
> the AutoIncrement field in an add such as the one above? Thanks.
>
>