Object Cannot Be Cast from Dbnull to Other Types

Object cannot be cast from DBNull to other types

I'm thinking that your output parameter is coming back with a DBNull value. Add a check for that like this

var outputParam = dataAccCom.GetParameterValue(IDbCmd, "op_Id");
if(!(outputParam is DBNull))
DataTO.Id = Convert.ToInt64(outputParam);

C#: Object cannot be cast from DbNull to other types

check for DbNull before calling Convert.ToInt32: as you have seen, this will raise an exception if the value is DbNull.
something like:

object x = *value from db*
int y;
if (x != DbNull.Value)
y= Convert.ToInt32(x);
else
//handle null somehow

Object cannot be cast from DBNull to other types exception in C#

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

Object cannot be cast from DBNull to other types. error

txtdate.Value = objPridr2["MST_DATE"] is DBNull ? 0 : Convert.ToDateTime(objPridr2["MST_DATE"]).ToString("dd/MM/yyyy").Trim();

executescalar: Object cannot be cast from DBNull to other types

An easy way to get around this issue is to check if the returned value is DBNull:

Dim cv As Integer
Dim queryResult = cmd2.ExecuteScalar()
If IsDBNull(queryResult) Then
' No matching records. Do something about it.
Else
cv = DirectCast(queryResult, Integer)
End If

Object cannot be cast from DBNull to other types.' in stored procedure

You should check DBNull then assign value accordingly, like this

dependency.ReleaseId = dataReader["ReleaseId"] != System.DBNull.Value ? Convert.ToInt32(dataReader["ReleaseId"]) : 0;

C # Object cannot be cast from DBNull to other types

You need to check whether your column has null value or not before casting it to another type. As you are checking it with null, but dr["GZA"] won't be null, So you need to check it with DBNull.Value

Try this

foreach (DataRow dr in dt.Rows)
{
int GZA = (dr["GZA"] == DBNull.Value) ? 0 : Convert.ToInt32(dr["GZA"]);
if (dr["SALARY"] != DBNull.Value)
{
dr["Actual_salary"] = Convert.ToInt32(dr["SALARY"]) - ((Convert.ToInt32(dr["SALARY"]) / 30) * GZA);
}
}

I have a error Object cannot be cast from DBNull to other types?

you need to check if the reader is of type DBNULL

Call IsDBNull() on the reader to check the column before attempting to convert it:

using (reader = server.ExecuteReader(CommandType.Text, TopIDQuery, paramet))
{
while (reader.Read())
{
var column = reader.GetOrdinal("TopID");

if (!reader.IsDBNull(column))
topID = Convert.ToInt32(reader[column]);
}
}
}

Or, compare against DBNull.Value:

var value = reader["TopID"];

if (value != DBNull.Value)
{
topID = Convert.ToInt32(value);
}

Object cannot be cast from DbNull to other types C# Exception

DbNull in a result set means your database query is returning a NULL value in some column. A NULL value, by definition, cannot be converted to a string or number or anything.

In your case you only have one column in your result set, and it's a number. So zero can probably substitute for NULL without causing damage to your data.

Try adjusting your queries so they never return NULL. Here's how:

 get_P_Amount = "SELECT IFNULL(`purchasing`,0) AS 'purchasing' ... 

and

get_P_Amount = "SELECT IFNULL(SUM(t_price), 0) as 'purchasing' FROM `db_vegetab ...

This is a guess, however. You didn't tell us in your question which line of code in your program threw the exeception.

Pro tip: Never use catch (Exception) { }. It causes your program silently to ignore stuff that goes wrong. You want to know when your program throws exceptions, especially when it processes data about other peoples' money. If you must use it, do something like this:

 catch (Exception) {
/* empty, intentionally, because we ignore the zumbinated framiss */
}

This will help keep the next programmer to work on your code from cursing your name.



Related Topics



Leave a reply



Submit