Tableadapter insert return value

J

J055

Hi

When I do an insert using the update(datatable) method I can get the new
identity but when I do an insert directly with the Insert method I can only
get the record count because the InsertCommand uses ExecuteNonQuery.

Can I get the Insert method to return the identity?

the sproc looks like


SET NOCOUNT OFF;
INSERT INTO ...
SELECT AccountID = SCOPE_IDENTITY()

The update method obviously uses the same sproc so what is it doing
differently to get the identity?

Thanks
Andrew
 
K

Kevin Yu [MSFT]

Hi Andrew,

The return value from the Insert method is the rows affected. The SELECT
statement will return a result set, instead of a single identity value. In
this case, the code generated by the TableAdapter designer cannot return
the identity of the newly inserted row.

If you need to get that identity, you have to rewrite the insert method.
You can use

SET NOCOUNT OFF;
INSERT INTO ...
SELECT SCOPE_IDENTITY()

and execute with ExecuteScalar method to get it.

If anything is unclear, please feel free to let me know.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top