1) Stored procedure:
The body of the stored procedure should look like this:
INSERT INTO MyTable (C1, C2, C3) VALUES (@c1, @c2, @c3); SELECT SCOPE_IDENTITY();
The important part here is to use SELECT SCOPE_IDENTITY () rather than RETURN SCOPE_IDENTITY (). Edit: using RETURN will work if you call a function instead of a stored procedure.
2) Table adapter:
Right-click on your table adapter and select "Add Query" from the context menu. The Query Configuration Wizard is created:
Select the type of command: select "Use an existing stored procedure."
Select the data form returned by SP: select "Single value".
Enter the appropriate name for the method, for example. InsertXYZ
3) Your code:
Now, when you call the InsertXYZ method in your table adapter, it returns an object that you can pass to Int32. This value is the identifier of the new record!
source share