-1

What does this exception can mean? In the "return" line I get an error

Object reference not set to an instance of an object

name and pass are properly filled, not null.

    public int GetUBID(string name, string pass)
    {
        var PassHash = GetPasswordHash(pass);

        using (var con = new SqlConnection(Config.ConnectionString))
        {
            return (int)con.ExecuteScalar("spUser_getUBID",
                new { name = name, pass = PassHash },
                null, null,
                commandType: CommandType.StoredProcedure); //<< Exception

        }
    }
Титан
  • 41
  • 1
  • 9

1 Answers1

1

I bet if you check the value of returnObject, it will be NULL:

using (var con = new SqlConnection(Config.ConnectionString))
{
     object returnedObject = con.ExecuteScalar("spUser_getUBID",
                                               new { name = name, pass = PassHash },
                                               null, null,
                                              commandType: CommandType.StoredProcedure);
    // what value do you get here, in "returnedObject" ?? 
    if (returnedObject != null)
    {
        return (int)returnedObject;
    }
}

ALWAYS check your returned values for null - don't just blindly assume they're ok - many times they're not OK ! Just typecasting using (int) without a NULL check is bound to cause exceptions .....

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459