Why i get this error?
Object cannot be cast from DBNull to other types.
when i inserted null value for a numeric datatype in sql server 2005
Here is my code,
if (MeasurementTr.Visible == true)
{
materialIn.measurementId = Convert.ToInt64(DlMeasurement.SelectedValue.ToString());
}
else
{
materialIn.measurementId = Convert.ToInt64(DBNull.Value);
}
EDIT :
Is this right??
materialIn.measurementId = (Convert.ToInt64(DlMeasurement.SelectedValue.ToString()) != 0) ? Convert.ToInt64(DlMeasurement.SelectedValue.ToString()) : 0;
elseblock that's the problem. I'm sure that theifblock is fine (ToStringcan never returnDBNull.Value). As I said - use theSetNullorSetmeasurementIdNullmethod on theDataRow, if that's what it is, or just set it tonullordefault(Int64)if it's non-nullable.