Search code examples
oledb

oledb stored procedure c# Procedure or function expects parameter which was not supplied


I am getting the error : Implicit conversion from data type nvarchar to varbinary is not allowed. Use the CONVERT function to run this query. my c# code snippet is:

 OleDbCommand spCmd = new OleDbCommand
                    ("[SomeServer].[dbo].GetAllProperties", conn)
                    {
                        CommandType = CommandType.StoredProcedure
                    };
                spCmd.Parameters.AddWithValue("@P1", path);
                spCmd.Parameters.AddWithValue("@P5", 4);
                int rowsAffected = spCmd.ExecuteNonQuery();

path is a string. corresponding sql sp is:

ALTER PROCEDURE [dbo].[GetAllProperties]
@P1 nvarchar (425),
@P2 varchar(32) = NULL,
@P3 as varbinary(85) = NULL, 
@P4 as nvarchar(260) = NULL,
@P5 int
AS
BEGIN

.... Please let me know how to invoke this sp from c#. i tried specifying all parameters in order as follows but then i get error: Implicit conversion from data type nvarchar to varbinary is not allowed. Use the CONVERT function to run this query

                spCmd.Parameters.AddWithValue("@P1", path);
                spCmd.Parameters.AddWithValue("@P2", DBNull.Value);
                spCmd.Parameters.AddWithValue("@P3", DBNull.Value);
                spCmd.Parameters.AddWithValue("@P4", DBNull.Value);
                spCmd.Parameters.AddWithValue("@P5", 4);
                int rowsAffected = spCmd.ExecuteNonQuery();

Solution

  • to pass null values to the optional parameters simply use null instead of DBType.Null. It works:

            spCmd.Parameters.AddWithValue("@P1", path);
            spCmd.Parameters.AddWithValue("@P2", null);
            spCmd.Parameters.AddWithValue("@P3", null);
            spCmd.Parameters.AddWithValue("@P4", null);
            spCmd.Parameters.AddWithValue("@P5", 24);