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

I am trying to Insert and update records in one Store procedure,My EmployeeId field is auto generated, the problem is when i use it from my aspx page it says @EmployeeID not supplied.
Code is:
SQL
create PROCEDURE usp_insertupdatepragimcrud
@EmployeeID int,
@Name varchar(50),
@Gender varchar(10),
@City varchar(50)
as begin
if not exists(select * from dbo.tblEmployeeDataAccessLayer where employeeid=@EmployeeID)
insert into tblEmployeeDataAccessLayer (Name,Gender,City) values (@Name,@Gender,@City)
else 
update tblEmployeeDataAccessLayer set Name=@Name,Gender=@Gender,City=@City where employeeid=@EmployeeID  
end

How do i fix it, as Employee id will be Auto generated.
Posted

To fix your problem, mark EmployeeId as nullable so from front end if you don't pass employee id then it will work.

For better solution:

Instead checking in SP "IsExit", pass from the @EmployeeId in case you are going to Update otherwise it will be inserted.
So you need to check only
if(@Employee id is NULL)
then Insert your record
else
Update your record
 
Share this answer
 
Comments
Member 9176543 15-Sep-13 0:47am    
Employee Id is set to is Identity,cant make it to allow nulls.
Parwej Ahamad 15-Sep-13 1:15am    
It's parameter so no relation with your identity column. But yes don't try to update or insert employee id.
Member 9176543 15-Sep-13 2:01am    
Did you mean this
alter PROCEDURE usp_insertupdatepragimcrud
@EmployeeID int = null,
@Name varchar(50),
@Gender varchar(10),
@City varchar(50)
as begin
if(@EmployeeID is null)
insert into tblEmployeeDataAccessLayer (Name,Gender,City) values (@Name,@Gender,@City)
else
update tblEmployeeDataAccessLayer set Name=@Name,Gender=@Gender,City=@City where employeeid=@EmployeeID
end
Parwej Ahamad 15-Sep-13 2:04am    
Yes, correct
Member 9176543 15-Sep-13 2:07am    
Thank You.
Your stored procedure has an argument for EmployeeID, this needs to be supplied since you use it in the stored procedure to check for the employee id existing.

You should be checking if the employee exists by name instead of employee ID. Otherwise you have to pass in an employee id to the stored procedure.
 
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