For heavens sake, this took me quite some time to get around. Okay here goes. 1,2,3 easy steps to get ID from MS SQL using datasets.
- In visual studio, under your DAL layer, you should have a dataset where you hold your table adapters.
- right click on the table adapter and click configure to get to your TABLEADAPTER CONFIGURATION Wizard.
- Click on advanced options
- Make sure Refresh the data Table option is checked
- Save the changes
- Go to configure your insert statement in the ADAPTER
- Enter a new line with — Return the applicationsID for the newly created record
SELECT SCOPE_IDENTITY() - Select the Insert Query and in the Properties box, change ExecuteMode is Scalar
- Going back to your code (or BLL) where you initiated the table adapter, you can access your id using
int x = Convert.ToInt32(myTableAdapter.InsertQuery(……..)); - Remember the convert to else it will give an object error. And Volia! You get the damned product ID.
Pardon my french but this is oddly simple and frustrating to work with.