none
An exception of type 'System.NullReferenceException' occurred in EntityFramework.dll but was not handled in user code : Executing a stored procedure RRS feed

  • Question

  • Hello,
    I have my code below:

      User user = new User();
      WFMModel db = new WFMModel();
      SqlParameter param1 = new SqlParameter("@username", username);
      SqlParameter param2 = new SqlParameter("@password", password);
      SqlParameter param3 = new SqlParameter("@Version", Version);
    
     var userData = db.Database.SqlQuery<User>("usp_Rep_App_DotNet_Validate_User_new @username,@password,@Version",
    param1, param2, param3);
    
    


    And I have my User class as below:

        public class User
        {
            public string UserID;
            public string FirstName;
            public string LastName;
            public string Message;
            public string Department;
        }

    But when the code executes, I get the error:
    An exception of type 'System.NullReferenceException' occurred in EntityFramework.dll but was not handled in user code.

    When I run the stored procedure, I get the data without any issues (no nulls as well).

    Any idea what's going on?
    Thanks in advance.

    • Moved by CoolDadTx Tuesday, January 10, 2017 3:58 PM EF related
    Monday, January 9, 2017 3:53 PM

Answers

  • Without the call stack and exception information we aren't going to be able to help you. A null ref exception means you have a null and aren't handling it. The only places in your code that you posted where this seems possible is 'db' and 'db.Database'. I would put a breakpoint on that line and then execute the code. When it gets hit look at the variables. db is presumably a field in your type so you'll have to ensure it gets initialized (constructor, new, etc). Database is a property off DbContext so it should in theory be created for you but EF doesn't create a connection to the DB until needed so at the point you make this call it might not have been created yet. I suspect that it is fine though.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by h_abhijith Tuesday, January 10, 2017 5:35 PM
    Tuesday, January 10, 2017 3:58 PM

All replies

  • Without the call stack and exception information we aren't going to be able to help you. A null ref exception means you have a null and aren't handling it. The only places in your code that you posted where this seems possible is 'db' and 'db.Database'. I would put a breakpoint on that line and then execute the code. When it gets hit look at the variables. db is presumably a field in your type so you'll have to ensure it gets initialized (constructor, new, etc). Database is a property off DbContext so it should in theory be created for you but EF doesn't create a connection to the DB until needed so at the point you make this call it might not have been created yet. I suspect that it is fine though.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by h_abhijith Tuesday, January 10, 2017 5:35 PM
    Tuesday, January 10, 2017 3:58 PM
  • Thanks Michael, One of the parameter values was longer than the length defined in the stored procedure.
    Tuesday, January 10, 2017 5:44 PM