views:

718

answers:

3

Dear all,

I'm unable to retrieve the latest inserted id from my SQL Server 2000 db using a typed dataset in asp.NET

I have created a tableadapter and I ticked the "Refresh datatable" and "Generate Insert, Update and Delete statements". This auto-generates the Fill and GetData methods, and the Insert, Update, Select and Delete statements.

I have tried every possible solution in this thread

http://forums.asp.net/t/990365.aspx

but I'm still unsuccesfull, it always returns 1(=number of affected rows). I do not want to create a seperate insert method as the auto-generated insertCommand perfectly suits my needs.

As suggested in the thread above, I have tried to update the InsertCommand SQL syntax to add SELECT SCOPY_IDENTITY() or something similar, I have tried to add a parameter of type ReturnValue, but all I get is the number of affected rows.

Does anyone has a different take on this? Thanks in advance! Stijn

A: 

your query will look like...

insert into yourtable values(); SELECT SCOPE_IDENTITY()

and you need to set ExecuteMode=Scaler of insert query

Muhammad Akhtar
Thanks Muhammad but I already did this
Stijn Van Loo
Yes I also have the scope_identity part included in the queryI'm wondering dough, since I'm using the auto-generated InsertCommand, where I should specify the executemode=Scalar option?At the moment I have declared this for the fill/getData methods, which I think should be sufficient?
Stijn Van Loo
+1  A: 

I decided to give up, I can't afford to waste any more time on this. I use the Insert statement after which I do a select MAX(id) query to hget the insert ID

If anyone should have a solution, I'll be glad to read it here

Thanks Stijn

Stijn Van Loo
we always use seperate
Muhammad Akhtar
What if you use seperate insert query instead of autogenerated?
Muhammad Akhtar
using Max(id) is bad approach, if many user entering data, Wrong ID will be returned...
Muhammad Akhtar
A: 

You need to tell your table's table-adapter to refresh the data-table after update/insert operation. This is how you can do that.

  1. Open the properties of TableAdapter -> Default Select Query -> Advnaced options. and Check the option of Refresh the data table. Save the adapter now. Now when you call update on table-adapter, the data-table will be updated [refreshed] after the update/insert operation and will reflect the latest values from database table. if the primary-key or any coloumn is set to auto-increment, the data-table will have those latest value post recent update.

  2. Now you can Call the update as TableAdapterObj.Update(ds.dataTable);

  3. Read latest values from the DataTable(ds.dataTable) coloumns and assign respective values into the child table before update/insert. This will work exactly the way you want.

alt text

this. __curious_geek