I am using MySql database, if I execute the solution I am getting the exception in catch block, below is my code
MySqlManager dac = new MySqlManager();
long value = 0;
DbCommand dbCommand = dac.GetStoredProcCommand("Spforgotpassword");
dac.AddOutParameter(dbCommand, "p_op_EmailId", DbType.Int32, -1);
dac.AddInParameter(dbCommand, "p_p_EmailId", DbType.String, data.Email_Id);
dac.AddInParameter(dbCommand, "p_p_Password", DbType.String, Generatehash256(data.Password));
dac.AddInParameter(dbCommand, "p_P_EncryptedPassword", DbType.String, Encryption.Encrypt(data.Password));
dac.AddInParameter(dbCommand, "p_P_Mode", DbType.String, 102);
dac.AddInParameter(dbCommand, "p_IPAddress", DbType.String, data.IPAddress);
IDataReader reader = dac.ExecuteReader(dbCommand);
if (reader.Read())
{
value = reader["result"] == DBNull.Value ? default(long) : Convert.ToInt32(reader["result"]);
}
dac.CloseConnection(dbCommand, reader);
if (Convert.ToBoolean(ConfigurationSettings.AppSettings["EnableEmail"]))
{
if (Convert.ToInt32(dac.GetParameterValue(dbCommand, "p_op_EmailId")) != -1)
{
Mail.WebMail oWebmail = new Mail.WebMail(dac.GetParameterValue(dbCommand, "p_op_EmailId").ToString());
}
}
return value;
Once I execute this line I am getting the exception as --- Object cannot be cast from DBNull to other types
dac.GetParameterValue(dbCommand, "p_op_EmailId")
the p_op_EmailId is the output parameter.
It looks like the stored procedure sets the value of p_op_EmailId
to null
. So you cannot convert it to Int32
directly.
Instead you can cast it to a nullable int, and coalesce it to -1
before comparing:
(dac.GetParameterValue(dbCommand, "p_op_EmailId") as int? ?? -1) != -1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.