If I have code similar to the following:
while(myDataReader.Read())
{
myObject.intVal = Convert.ToInt32(myDataReader["mycolumn"] ?? 0);
}
It throws the error:
Object cannot be cast from DBNull to other types.
defining intVal
as a nullable int is not an option. Is there a way for me to do the above?
Null is similar to zero pointer in C++. So it is a reference which not pointing to any value. DBNull. Value is completely different and is a constant which is returned when a field value contains NULL.
The DBNull class represents a nonexistent value. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column.
Uses of Null Coalescing Operator: It is used to replace the ternary operator in conjunction with the PHP isset() function. It can be used to write shorter expressions. It reduces the complexity of the program. It does not throw any error even if the first operand does not exist.
Here's one more option:
while (myDataReader.Read())
{
myObject.intVal = (myDataReader["mycolumn"] as int? ?? 0);
}
Can you use an extension method? (written off the top of my head)
public static class DataReaderExtensions
{
public static T Read<T>(this SqlDataReader reader, string column, T defaultValue = default(T))
{
var value = reader[column];
return (T)((DBNull.Value.Equals(value))
? defaultValue
: Convert.ChangeType(value, typeof(T)));
}
}
You'd use it like:
while(myDataReader.Read())
{
int i = myDataReader.Read<int>("mycolumn", 0);
}
Can you simply use Int32.Tryparse
?
int number;
bool result = Int32.TryParse(myDataReader["mycolumn"].ToString(), out number);
According to the MSDN, number
will contain 0 if the conversion failed
How about something like:
object x = DBNull.Value;
int y = (x as Int32?).GetValueOrDefault(); //This will be 0
Or in your case:
int i = (myDataReader["mycolumn"] as Int32?).GetValueOrDefault();
Why not use something other than the null coalescing operator (DBNull.Value
!= null
):
int i = myDataReader["mycolumn"] == DBNull.Value ?
Convert.ToInt32(myDataReader["mycolumn"]) :
0;
You could always wrap it up in a neat extension method:
public static T Read<T>(this DataReader reader, string column, T defaultVal)
{
if(reader[column] == DBNull.Value) return defaultVal;
return Convert.ChangeType(reader[column], typeof(T));
}
Nope, only works for nulls.
How about an extension method on object that checks for DBNull, and returns a default value instead?
//may not compile or be syntactically correct! Just the general idea.
public static object DefaultIfDBNull( this object TheObject, object DefaultValue )
{
if( TheObject is DBNull )
return DefaultValue;
return TheObject;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With