ObjectDataSource

S

shapper

Hello,

I have an ObjectDataSource with Save defined as insert method.

I need return the ID of the inserted record so I can use it in my
ObjectDataSource after the item has been inserted.

How can I do this?

This is my code:

<DataObjectMethodAttribute(DataObjectMethodType.Insert, True)> _
Public Shared Sub Insert(ByVal name As String)

Dim database As New CodeDataContext
Dim tag As New Tag
tag.Name = name
tag.TagID = Guid.NewGuid()
database.Tags.InsertOnSubmit(tag)
database.SubmitChanges()

End Sub ' Insert

This is how I am defining my insert parameter

odsTags.InsertParameters.Add(New Parameter("Name",
TypeCode.String))

Thanks,

Miguel
 
M

Michael Nemtsev [MVP]

Hello shapper,


U can use "select @@identity" to get the if of the latest insert row to
sql table

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


s> Hello,
s>
s> I have an ObjectDataSource with Save defined as insert method.
s>
s> I need return the ID of the inserted record so I can use it in my
s> ObjectDataSource after the item has been inserted.
s>
s> How can I do this?
s>
s> This is my code:
s>
s> <DataObjectMethodAttribute(DataObjectMethodType.Insert, True)> _
s> Public Shared Sub Insert(ByVal name As String)
s> Dim database As New CodeDataContext
s> Dim tag As New Tag
s> tag.Name = name
s> tag.TagID = Guid.NewGuid()
s> database.Tags.InsertOnSubmit(tag)
s> database.SubmitChanges()
s> End Sub ' Insert
s>
s> This is how I am defining my insert parameter
s>
s> odsTags.InsertParameters.Add(New Parameter("Name",
s> TypeCode.String))
s>
s> Thanks,
s>
s> Miguel
s>
 
E

Eliyahu Goldin

If you are with SqlServer, use either @@identity or scope_identity().

And you insert statement will look like
insert ...;select scope_identity()

Different databases likely offer similar ways of getting last identity.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top