I am trying to fetch record into listarray as follows:
List<Car> lst = new List<Car>();
string str = "select * from Inventory";
using(SqlCommand cmd = new SqlCommand(str,this.sqlcon))
{
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
lst.Add(new Car
{
CarId = (int)rdr["CarId"],
Make = (string)(rdr["Make"] ?? ""),
Color= (string)(rdr["Color"] ?? ""),
PetName = (string)(rdr["PetName"] ?? "")
});
}
rdr.Close();
}
Make,color and petname may have null values and thus I used the ??
operator. I get the following error
Unable to cast object of type system.dbnull' to 'system.string'.
What is the correct way of checking for null in this scenario?