I develop a dnn module and use public class classNameInfo: IHydratable (c#)
Can someone explain why this code always return 0 from the db:
System.Nullable<int> _ProductID;
_ProductID = (oReader["ProductID"] == System.DBNull.Value ? null : (int?)oReader["ProductID"]);
while this code returns the correct value?
int? y = (oReader["ProductID"] == System.DBNull.Value ? null : (int?)oReader["ProductID"]);
if (y != null)
{
_ProductID = (int)oReader["ProductID"];
}
else
{
_ProductID = 0;
}
If you try to convert
anythingtoint?, it will only work if the type of saidanythingis exactly int. Every other direct conversion will fail. Since the database types are never “exactlyint“, that conversion can’t work.To make the code shorter, you can use the following statement: