locked
"Procedure or function 'AddProject' expects parameter '@email', which was not supplied." except I did supply it? RRS feed

  • Question

  • First, I want to apologize because my question involves c#, but I can't figure out whether the source of the error is from the c# side or from the SQL Server side.

    I have the following stored procedure:

    CREATE PROCEDURE [dbo].[AddProject]
    	@email varchar(320),
    	@project varbinary(max),
    	@ProjectName varchar(32)
    AS
    BEGIN
    	SET NOCOUNT ON;
    	INSERT INTO Projects (Project, CreationDate, ProjectName) VALUES (@project,  GETDATE(), @ProjectName);
    	DECLARE @ID INT = SCOPE_IDENTITY();
        INSERT INTO ProjectsUsers VALUES (@email, @ID);
    	SELECT @ID;
    END

    I am trying to call the stored procedure using the function:

    public static int InsertNewProjectIntoDB(string Email, string ProjectName, EmployeeTree employeeTree)
            {
                const string SQL_SP = "dbo.AddProject";
                using (var conn = new SqlConnection(ConnectionString))
                using (var command = new SqlCommand(SQL_SP, conn)
                {
                    CommandType = CommandType.StoredProcedure
                })
                {
                    command.Parameters.AddWithValue("@email", Email);
                    command.Parameters.AddWithValue("@project", ObjectToByteArray(employeeTree));
                    command.Parameters.AddWithValue("@ProjectName", ProjectNameq);
                    conn.Open();
                    return (int)(command.ExecuteScalar());
                }
            }

    When I do it, I get the error: "Procedure or function 'AddProject' expects parameter '@email', which was not supplied."

    But as you can see from the code, I did supply the "email" parameter, yet I got the error.

    What am I doing wrong? 

    Saturday, November 16, 2019 6:55 PM

Answers

  • The code you posted looks ok, although I suggest you avoid AddWithValue.

    The error indicates the EMail string value is a .NET null value. If your intent is to insert a NULL value for email, use DBNull.Value instead.


    Dan Guzman, Data Platform MVP, http://www.dbdelta.com

    • Marked as answer by avivgood Sunday, November 17, 2019 3:07 PM
    Saturday, November 16, 2019 7:38 PM

All replies

  • Does it work in these experiments:

       …AddWithValue("@email", "some@email.com")

    or

       …AddWithValue("@email", ((object)Email) ?? DBNull.Value)   ?

    Saturday, November 16, 2019 7:30 PM
  • The code you posted looks ok, although I suggest you avoid AddWithValue.

    The error indicates the EMail string value is a .NET null value. If your intent is to insert a NULL value for email, use DBNull.Value instead.


    Dan Guzman, Data Platform MVP, http://www.dbdelta.com

    • Marked as answer by avivgood Sunday, November 17, 2019 3:07 PM
    Saturday, November 16, 2019 7:38 PM