. Advertisement .
..3..
. Advertisement .
..4..
I am tired of fixing the problem: object cannot be cast from dbnull to other types in the csharp; even if I get the reference from another forum, it still returns an error:
*** Start Block ***
Object cannot be cast from DBNull to other types
To identify the problem, I will show you the detail here:
public Boolean Create(DataTO DataTO)
{
IDbTrans transaction = null;
IDbCmd IDbCmd;
string EncryptedPassword = Encrypt(DataTO.txtPwd);
Base dataAccCom = null;
try
{
dataAccCom = Factory.Create();
dataAccCom.OpenConnection();
transaction = dataAccCom.BeginTransaction();
IDbCmd = dataAccCom.CreateCommand("sp_Register", true);
dataAccCom.AddParameter(IDbCmd, "op_Id", DbType.Int64, 0, ParameterDirection.Output);
dataAccCom.AddParameter(IDbCmd, "p_dlstTitle", DbType.String, ReplaceNull(DataTO.dlstTitle));
dataAccCom.AddParameter(IDbCmd, "p_txtFirstName", DbType.String, ReplaceNull(DataTO.txtFirstName));
dataAccCom.AddParameter(IDbCmd, "p_txtMiddleName", DbType.String, ReplaceNull(DataTO.txtMiddleName));
dataAccCom.AddParameter(IDbCmd, "p_txtLastName", DbType.String, ReplaceNull(DataTO.txtLastName));
dataAccCom.AddParameter(IDbCmd, "p_txtDob", DbType.DateTime, DataTO.txtDob);
dataAccCom.AddParameter(IDbCmd, "p_txtDesig", DbType.String, ReplaceNull(DataTO.txtDesig));
dataAccCom.AddParameter(IDbCmd, "p_txtOFlatNo", DbType.String, ReplaceNull(DataTO.txtOFlatNo));
dataAccCom.AddParameter(IDbCmd, "p_txtOBuild", DbType.String, ReplaceNull(DataTO.txtOBuild));
dataAccCom.AddParameter(IDbCmd, "p_txtOPost", DbType.String, ReplaceNull(DataTO.txtOPost));
dataAccCom.AddParameter(IDbCmd, "p_txtOArea", DbType.String, ReplaceNull(DataTO.txtOArea));
dataAccCom.AddParameter(IDbCmd, "p_txtOCity", DbType.String, ReplaceNull(DataTO.txtOCity));
dataAccCom.AddParameter(IDbCmd, "p_txtRBuild", DbType.String, ReplaceNull(DataTO.txtRBuild));
dataAccCom.AddParameter(IDbCmd, "p_txtRPost", DbType.String, ReplaceNull(DataTO.txtRPost));
dataAccCom.AddParameter(IDbCmd, "p_txtUserID", DbType.String,ReplaceNull(DataTO.txtUserID));
dataAccCom.AddParameter(IDbCmd, "p_txtPwd", DbType.String, ReplaceNull(EncryptedPassword));
dataAccCom.ExecuteNonQuery(IDbCmd);
DataTO.Id = Convert.ToInt64(dataAccCom.GetParameterValue(IDbCmd, "op_Id"));
transaction.Commit();
return true;
}
catch (System.Exception ex)
{
if (transaction != null)
{
transaction.Rollback();
}
throw ex;
}
finally
{
transaction = null;
if (dataAccCom != null)
{
dataAccCom.CloseConnection();
}
dataAccCom = null;
IDbCmd = null;
}
}
public string ReplaceNull(string value)
{
if (value == null)
{
return "";
}
else
{
return value;
}
}
public DateTime ReplaceNull(DateTime value)
{
if (value == null)
{
return DateTime.Now;
}
else
{
return value;
}
}
public double ReplaceNull(double value)
{
if (value == null)
{
return 0.0;
}
else
{
return value;
}
}
How do I do that? Could you support me in improving this problem?
The cause:
After researching your problem for hours, I found that the parameter of your output is returning a value of DBNull. This is the reason why you have got the: ”object cannot be cast from dbnull to other types” error.
Solution:
The easiest solution for this error is adding a check for DBNull value as the following:
Another way that is equally effective is using the
Convert.IsDBNull
method:It is possible that the line has been retracted.
This is the root cause of the problem. It is possible that the
op_Id
value has been set to null by a stored procedure.Use the
Convert.IsDBNull
method to protect yourself. Take this example: