SQLDataSource and Identity column

V

Vik

How can I retrieve an Identity value after inserting a new row with an
SQLDataSource?

Thanks.

Victor
 
E

Eliyahu Goldin

End your InsertCommand with ";set @NewId = scope_identity()".

Handle the SqldataSource Inserted event like this:

protected void myDataSource_Inserted(object sender,
SqlDataSourceStatusEventArgs e)
{
// get Id of the new record
this.newId = Convert.ToInt32(e.Command.Parameters["@NewId"].Value);
}


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


Vik said:
How can I retrieve an Identity value after inserting a new row with an
SQLDataSource?

Thanks.

Victor



__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4068 (20090512) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4068 (20090512) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
D

David

I stumbled on this while trying to answer the same sort of question. I just
wanted to add, for anyone else who stumbles on this as well, that I also had
to make the markup in the InsertParameters look like this:

<asp:parameter Name="NewEventID" Type="Int32" Direction="Output"/>

Only the intial Parameter Name attribute was included by default.

type="Int32

Eliyahu Goldin said:
End your InsertCommand with ";set @NewId = scope_identity()".

Handle the SqldataSource Inserted event like this:

protected void myDataSource_Inserted(object sender,
SqlDataSourceStatusEventArgs e)
{
// get Id of the new record
this.newId = Convert.ToInt32(e.Command.Parameters["@NewId"].Value);
}


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


Vik said:
How can I retrieve an Identity value after inserting a new row with an
SQLDataSource?

Thanks.

Victor



__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4068 (20090512) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4068 (20090512) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 

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,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top