How to session the ID
Ok now that I have insert a new database in my table how can I session the ID that are just insert into the database?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tan Kai KaiPosted Jul 9, 2013, 12:01 PM
mohan rajPosted Jul 9, 2013, 3:17 AM
Riyaz AkhtarPosted Jul 9, 2013, 2:54 AM
http://forums.asp.net/t/1893177.aspx/1
Jignesh TrivediPosted Jul 9, 2013, 12:10 AM
hi,
Please refer following link it might help you to resolve your problem
http://msdn.microsoft.com/en-us/library/ks9f57t0.aspx
http://stackoverflow.com/questions/9319532/return-value-from-sql-server-insert-command-using-c-sharp
Tan Kai KaiPosted Jul 8, 2013, 9:53 PM
cmd.ExecuteNonQuery like this
String id=cmd.ExecuteScalar().ToString();
Session["UserId"]=id;
Say that
ExecuteScalar: Connection property has not been initialized.
Iftikar HussainPosted Jul 8, 2013, 11:30 AM
First modify your Stored Procedure to include the below line after the insert statement
SELECT @@IDENTITY
Then in your code where you are executing the sql statement to insert the record in your db, there you need to modify write cmd.ExecuteScalar() instead on cmd.ExecuteNonQuery like this
String id=cmd.ExecuteScalar().ToString();
Session["UserId"]=id;
Let me know if you need more information.
Regards,
Iftikar