Get ready for BLACK FRIDAY shopping starting in

Forums

ASP

This topic is locked

Insert with Identity in ASP.NET

Posted 16 Dec 2004 00:42:40
1
has voted
16 Dec 2004 00:42:40 Matt Bristow posted:
I've been using Georges extension in classic asp for a while but I am inj the middle of a project that is in ASP.NET and I need to insert into a table then add the newly inserted ID into a cookie. Anyone got any ideas?

Is there an ASP.NET version in the pipeline?

Replies

Replied 17 Dec 2004 03:01:17
17 Dec 2004 03:01:17 Simon Martin replied:
If you use commands and stored procedures then after you've done your insert select @@IDENTITY and in the command dialogue set that as the returned value - you can then pass it to whatever you like in your pages (cookie, session variable etc)

Live the life you love
Love the life you live

Simon

[DWMX 2004]|[SQL]|[ASP/VBScript]|[XP-Pro]
Replied 17 Dec 2004 13:56:58
17 Dec 2004 13:56:58 Matt Bristow replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
If you use commands and stored procedures then after you've done your insert select @@IDENTITY and in the command dialogue set that as the returned value - you can then pass it to whatever you like in your pages (cookie, session variable etc)

Live the life you love
Love the life you live

Simon

[DWMX 2004]|[SQL]|[ASP/VBScript]|[XP-Pro]

<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Thanks for the reply I have tried that but it still doesn't like the code this is what I have inserted right after the mm_insert parameter:

select @@IDENTITY as Newdent

public void Page_Load(Object sender, EventArgs e){
Response.Cookies("F33_UID".Value = Newdent;
}


It doesn't like the @@IDENTITY part
Replied 17 Dec 2004 15:38:43
17 Dec 2004 15:38:43 Simon Martin replied:
Have you tried selecting @@IDENTITY in your stored procedure? From what you've said it looks like you're trying to do it on the webpage

Live the life you love
Love the life you live

Simon

[DWMX 2004]|[SQL]|[ASP/VBScript]|[XP-Pro]
Replied 17 Dec 2004 16:17:24
17 Dec 2004 16:17:24 Matt Bristow replied:
Simon

Thanks for your help as you may have noticed I'm new to asp.net!!! it still refuses to work and falls over on the @@IDENTITY line any ideas/pointer greatfully received.

Dim conNorthwind As SqlConnection
Dim strInsert As String
Dim cmdInsert As SqlCommand

conNorthwind = New SqlConnection( "Server=00.00.00.00;UID=xxxx;PWD=0000;database=000000" )
strInsert = "Tbl_Contact (Title, FirstName, Surname, Company, Tel, Email) VALUES (?, ?, ?, ?, ?, ?)"
cmdInsert = New SqlCommand( strInsert, conNorthwind )
conNorthwind.Open()
cmdInsert.ExecuteNonQuery()
conNorthwind.Close()

SELECT @@Identity AS IDVal

IDVal = intNewID

Response.Cookies("F33_UID".Value = intNewID

End Sub
Replied 17 Dec 2004 21:13:20
17 Dec 2004 21:13:20 Simon Martin replied:
Hi Matt,

In your procedure in the database I'd do something like
INSERT INTO Tbl_Contact (Title, FirstName, Surname, Company, Tel, Email)
VALUES (@Title, @FirstName, @Surname, @Company, @Tel, @Email)

DECLARE @NewID int;
SET @NewID = (SELECT @@IDENTITY)

Then when the procedure is run, your values inserted and the ID value will get stored in the new variable - that can then be passed back to the webpage in the RETURN VALUE parameter that DMX gives you in the command dialogue

Live the life you love
Love the life you live

Simon

[DWMX 2004]|[SQL]|[ASP/VBScript]|[XP-Pro]


Edited by - ganseki on 17 Dec 2004 21:16:24
Replied 19 Dec 2004 13:23:15
19 Dec 2004 13:23:15 Matt Bristow replied:
Simon

I'm not sure of the syntaxt to use can you post a complete example?

Reply to this topic