Stored procedure expects a parameter I am already passing in

后端 未结 3 1431
南方客
南方客 2021-01-19 03:28

I am trying to execute a stored procedure with this declaration:

ALTER PROCEDURE [dbo].[getByName]
    @firstName varchar,
    @lastName varchar
AS
...


        
相关标签:
3条回答
  • 2021-01-19 03:38

    I have seen this issue occur many, many times in two common scenarios:

    1. The value being assigned to the parameter is null (or Nothing in VB.Net). This is the .Net null, not the DB null (DBNull.Value) and this happens most commonly with strings.

    2. The parameter being created is associated with the wrong command object. This commonly occurs when you have multiple command objects in the same class with similar names.

    Please double-check the code to ensure that the string variable is not set to null and that the parameter is being added to the correct command.

    Update

    Based on the full updated code that was posted, the likely issue is 1 above.

    To circumvent this problem in your code, add the following at the top of the method:

    if (first == null) {
      first = "";
    }
    if (last == null) {
      last = "";
    }
    
    0 讨论(0)
  • 2021-01-19 03:41

    Try this it will work:

    SqlParameter[] sqlParams = new SqlParameter[] { 
                new SqlParameter("@UserName",(object)userName ?? DBNull.Value),
                new SqlParameter("@Password",(object)password ?? DBNull.Value)
    };
    

    If parameter is NULL than replace it with DBNull Type using ?? Operator

    0 讨论(0)
  • 2021-01-19 03:52

    Please add CommandaType to SQLCommand.

    e.g: scmd.CommandType = CommandType.StoredProcedure;

    0 讨论(0)
提交回复
热议问题