Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


I have a table name USER with UserKey,username and password.
In that UserKey is primary key.
I want to insert username and password into table using asp.net web form by writing a stored procedure.

This is my SP.
SQL
Insert into USER(UserKey,username,password)  
values(@User_Key,@User_Name,@PW)


Now i need UserKey value(any number) should be inserted automatically into table while inserting username and password without entering specific value for UserKey.
Can anyone correct my the stored procedure or any other way to do this.

Thank you all,
Posted
Updated 22-May-12 23:53pm
v2

Your database should generate the 'user key'. Primary keys should be left to the database to generate.

1) Edit your user table, set the UserKey field to have the 'Identity specification'.

2) Modify your stored procedure. You don't need to insert into the identity field, the database will generate the id automatically.

SQL
Insert into [USER](username,password)
values(@User_Name,@PW)


SET @User_Key = SCOPE_IDENTITY() 


Use SCOPE_IDENTITY() to retrieve the value of the newly created Id, returns that to your application or do whatever else you need.
 
Share this answer
 
v2
Comments
Wendelius 23-May-12 16:19pm    
My 5
jaipal0908 25-May-12 2:41am    
Thank you,helped me alot
You can apply auto increment on UserKey column at the time of table creation.as
SQL
CREATE TABLE [dbo].[tblUser](
	[userKey] [int] IDENTITY(1,1) NOT NULL,
	[userid] [varchar](50) NULL,
	[password] [varchar](150) NULL
.
.
.


And you do not need to insert userkey by SP.use as

SQL
Insert into [USER](username,password)
values(@User_Name,@PW)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900