On Fri, 08 Aug 2008 16:55:17 +1000, Ken Foskey wrote:
I need to trap an insert on an MS access table that uses a generated
typed table and row setup. I have to trap the insert and then hook up
and get the autonumber value for the insert. I have googled this and
cannot find a typed solution to the problem. I either abandon type
safety or solve this problem.
I have to work around 'Refresh the data table' that is greyed out.
MyDataset is from the xsd designed Dummy is your table name. You will
add the TableAdapter to your application from the toolbox. I cannot find
this easily documented anywhere.
I finally figured this out. In my initialise:
DummyTableAdapt er.Adapter.RowU pdated += InsertDummy;
Then created the event:
private void InsertDummy(obj ect sender,
System.Data.Ole Db.OleDbRowUpda tedEventArgs args)
{
if (args.Statement Type == StatementType.I nsert)
{
MyDataSet.Dummy Row ball = (MyDataSet.Dumm yRow)args.Row;
// Include a variable and a command to retrieve the
identity value from the Access database.
int newID = 0;
OleDbCommand idCMD = new OleDbCommand("S ELECT
@@IDENTITY", DummyAdapter.Co nnection);
// Retrieve the identity value and store it in the
CategoryID column.
newID = (int)idCMD.Exec uteScalar();
ball.ID = newID; // my autonumber primary key
}
}
Creative Commons.