locked
System.InvalidOperationException: The parameter at index 'n' in the parameters array is null RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    I just started using EF 6.0.

    In my store procedure I need to pass some ObjectParameter. (Basically they work as out put parameters). But when I try ti execute my code it show error.

    System.InvalidOperationException: The parameter at index 3 in the parameters array is null.

    Below is my controller code where I am executing the store procedure.

    CPMVCEntities db = new CPMVCEntities();
    int? result = db.AdminAuthenticate_USP(login.pvcUsername, login.pvcPassword,"",null,null,null,null,null,null,null).FirstOrDefault();

    and below is the context.cs file code which EF creates automatically.

    public virtual ObjectResult<Nullable<int>> AdminAuthenticate_USP(string pvcUsername, string pvcPassword, string pvcIpAddress, ObjectParameter pbintId, ObjectParameter pchAdminType, ObjectParameter pvcFirstName, ObjectParameter pvcLastName, ObjectParameter pvcEmail, ObjectParameter pbtStatus, ObjectParameter dtimeLastLogin)
            {
                var pvcUsernameParameter = pvcUsername != null ?
                    new ObjectParameter("pvcUsername", pvcUsername) :
                    new ObjectParameter("pvcUsername", typeof(string));
        
                var pvcPasswordParameter = pvcPassword != null ?
                    new ObjectParameter("pvcPassword", pvcPassword) :
                    new ObjectParameter("pvcPassword", typeof(string));
        
                var pvcIpAddressParameter = pvcIpAddress != null ?
                    new ObjectParameter("pvcIpAddress", pvcIpAddress) :
                    new ObjectParameter("pvcIpAddress", typeof(string));
        
                return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Nullable<int>>("AdminAuthenticate_USP", pvcUsernameParameter, pvcPasswordParameter, pvcIpAddressParameter, pbintId, pchAdminType, pvcFirstName, pvcLastName, pvcEmail, pbtStatus, dtimeLastLogin);
            }

    On this line (bold one) a get the error

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Nullable<int>>("AdminAuthenticate_USP", pvcUsernameParameter, pvcPasswordParameter, pvcIpAddressParameter, pbintId, pchAdminType, pvcFirstName, pvcLastName, pvcEmail, pbtStatus, dtimeLastLogin);

    Please suggest.

    Tuesday, April 23, 2019 4:55 AM

Answers

  • User1120430333 posted

    You can use optional parameters on a method signature.  But on the other hand, you can't pass in a null value for a parameter object, becuase all that is going to do  is  blow up the program at the code that addresses any parameter object that is a null valued object, which you have nulled-out most of the parameters you are passing into the method, and then you are trying to address the null valued parameters in the code.  

    https://www.c-sharpcorner.com/UploadFile/manas1/named-and-optional-parameter-in-C-Sharp/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 23, 2019 8:34 AM

All replies

  • User-1174608757 posted

    Hi demoninside9,

    The parameter at index 3 in the parameters array is null.

    According to your description, the exception shows the parameter value is null and I think it represents that parameter value for pbintId is null. So there may be losing some settings in your store procedure.

    So, I hope you could share your table construction and store procedure in your database.It will help us to reproduce your problem.

    Best Regards

    Wei

    Tuesday, April 23, 2019 8:30 AM
  • User1120430333 posted

    You can use optional parameters on a method signature.  But on the other hand, you can't pass in a null value for a parameter object, becuase all that is going to do  is  blow up the program at the code that addresses any parameter object that is a null valued object, which you have nulled-out most of the parameters you are passing into the method, and then you are trying to address the null valued parameters in the code.  

    https://www.c-sharpcorner.com/UploadFile/manas1/named-and-optional-parameter-in-C-Sharp/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 23, 2019 8:34 AM