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

I am trying to insert into employees table in northwind and return the scope_identity but it is always giving me 0 and this is my code :

in stored procedure:
SQL
ALTER PROCEDURE [dbo].[InsertmployeeAndGetScope] 
	-- Add the parameters for the stored procedure here
	@LastName nvarchar(20)=null,
	@FirstName nvarchar(10),
	@EmplyeeId int= null output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	insert into dbo.Employees(LastName,FirstName) values(@LastName,@FirstName);
	set @EmplyeeId=SCOPE_IDENTITY();
	return @EmplyeeId;
END


and the c# code:

C#
public int insertemployeeandgetid()
        {
            string connStr = ConfigurationManager.ConnectionStrings["north"].ToString();
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand dCmd = new SqlCommand("InsertmployeeAndGetScope", conn);
            dCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dCmd.Parameters.AddWithValue("@LastName", "zarif");
                dCmd.Parameters.AddWithValue("@FirstName", "mhamad");
                dCmd.Parameters.AddWithValue("@EmplyeeId", null);
                return Convert.ToInt32(dCmd.ExecuteScalar());
            }
            catch
            {
                throw;
            }
            finally
            {
                dCmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }

So what is the problem ?
Posted
Updated 2-Feb-12 4:51am
v2

try to change parameter declaration like this.

SqlParameter p = cmd.Parameters.Add("@MyParam", SqlDbType.Int);

p.Direction = ParameterDirection.Output;


Thanks
--RA
 
Share this answer
 
Please set the parameter direction for EmployeeID in your C# code.

Thanks
 
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