Let's say I have a custom class like this one:
public class Customer
{
public int CustomerID { get; set; }
public string CompanyName { get; set; }
public string BusinessAddress { get; set; }
public string Phone { get; set; }
public int ParentID { get; set; }
}
I create custom objects from the database using a datareader. Ex:
while (dr.Read())
{
listCustomers.Add(new Customer(
Convert.ToInt32(dr["CustomerID"]),
Convert.ToString(dr["CompanyName"]),
Convert.ToString(dr["BusinessAddress"]),
Convert.ToString(dr["Phone"]),
Convert.ToInt32(dr["ParentID"]),
)
ParentID can be null in the database (and I can't change it). When it's null, the conversion obviously fails.
How should I handle null values retrieved from the DB to populate my business objects? Would it be good pratice to use Nullable Types in my custom class? Any other tips?