0

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.

Abhi
  • 141
  • 2
  • 4
  • 17

3 Answers3

1

One of the variables that you are passing into your parameters (either username, password, or Version) is likely null. Set a breakpoint, and then hover your mouse over each variable to see which one is throwing the exception. You need to take appropriate measures to check these values for null before using them in your query.

If you wish for the parameter to be "null" from SQL Server's perspective, you would need to use DBNull.Value - databases use DBNull, C# uses null, and they are not interchangeable (although you might expect that they would be.)

Dave Smash
  • 2,941
  • 1
  • 18
  • 38
  • Thank you @Elemental Pete, I did check to make sure that the parameters are not null. – Abhi Jan 09 '17 at 21:12
  • 1
    The parameters themselves can't be null because you are declaring them as new SqlParameter. The values assigned to the parameters might be null, though. The three things that you would need to check are username, password, and Version. – Dave Smash Jan 09 '17 at 21:18
  • Thanks, the value was not null, but it was more than the length of the parameter. Now, when I run it, it doesn't throw any error, but it doesn't return any value either. – Abhi Jan 09 '17 at 22:44
  • I m getting at this line `string strOra = ConfigurationManager.ConnectionStrings["ConnectionString_IPCOLO"].ToString(); OracleConnection con = new OracleConnection(strOra); //here` – Nad Sep 27 '19 at 09:58
  • @BN - There probably is no connection string in the list called "ConnectionString_IPCOLO" - this would cause that property to be null, and you are assigning null to the variable strOra. When you go to use it in the next line, it throws a null reference exception because it is null. Change to the second line to `if (!string.IsNullOrEmpty(strOra)) { // original code here } else { // error handling }`. – Dave Smash Sep 27 '19 at 18:59
1

Are you sure that your UserID column will return string and not an integer?

Also probably one of your parameter values is null.

Szörényi Ádám
  • 1,223
  • 13
  • 17
  • Thanks, the value was not null, but it was more than the length of the parameter. Now, when I run it, it doesn't throw any error, but it doesn't return any value either. – Abhi Jan 09 '17 at 22:44
1

I myself ran into a lot of troubles when trying to retreive the return of a SQL stored proc with Entity, theres is about 3 way to do so correctly, here's the links :

This methode worked for me :

Sql Stored proc and Entity framework 6

Many people rather like :

using stored procedure in entity framework

And here is the long but normal way, that most people use :

Getting data from stored procedure with Entity Framework

Surely you will be able to use one of these 3 ways has they all do the same thing : retreive the return of the stored proc.

Community
  • 1
  • 1
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62