I have a simple code which calls a stored procedure:
bool connOpened = OpenConnectiontoDB();
SqlCommand cmd = new SqlCommand("spRegisterUser", cn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter userName = new SqlParameter("@UserName", username);
SqlParameter pass = new SqlParameter("@Password", password);
SqlParameter user_email = new SqlParameter("@Email", email);
cmd.Parameters.Add(userName);
cmd.Parameters.Add(pass);
cmd.Parameters.Add(user_email);
int returnCode=0;
try
{
returnCode = (int)cmd.ExecuteScalar();
}
catch (Exception e)
{
int i = 0;
}
return returnCode;
An exception is thrown at :
returnCode = (int)cmd.ExecuteScalar();
Message: "Object reference not set to an instance of an object."
I have no idea why, I checked:
cn.status
it shows as open, I can run the stored procedure directly in the DB. And cmd
appears to be populated so what is this error?
why you say the query returns null? It does not. It runs a stored procedure which returns an integer. And I tested the sp I only get -1 or 1.
The proposed duplicate answer does not answer this question. It only shows how to handle the error when object is null. My question was what is the cause of null.