hello everyone. can anyone tell me why this chunk of code:
if (Convert.IsDBNull(oCommand.Parameters["@userid"].Value))
{
p_nErrorCode = 10;
p_sErrorMessage = "Login failed, no matching credentials found";
goto Failed;
}
sReturn = Convert.ToString(oCommand.Parameters["@userid"].Value);
generates the following runtime error:
System.InvalidCastException: Object cannot be cast from DBNull to other
types
this seems impossible. no ErrorCode or ErrorMessage is recorded, which
would suggest that the if condition is false, and the value is not
DBNull. further evidence being that if i comment out the last line
(sReturn = Convert...) then the code generates no runtime exception.
that further suggests the if condition is false, and the value is not
DBNull. so how on earth can the last line generate the DBNull cast
exception? either it can't be NOT DBNull and then suddenly DBNull.
clearly i'm missing something. do you guys see it? do you need to see
more code?
thanks for any help,
jason
(in case anyone is wondering, this is all Failed does:)
Failed:
if (oCommand != null)
oCommand.Dispose();
if (oConn != null)
{
oConn.Close();
oConn.Dispose();
}
(and that's the end of the function. no more tampering with the
ErrorCode or ErrorMessage, or anything that would generate a DBNull
casting exception)