Marius Vasile

Marius Vasile

  • 554
  • 1.9k
  • 148.9k

Handle null value in Reader

Oct 22 2024 6:17 PM

I have this expression

while (rdr.Read())
{
    decimal[] yValues =
    {
        decimal.Parse(rdr["Result1"].ToString()),
        decimal.Parse(rdr["Result2"].ToString()),
        decimal.Parse(rdr["Result3"].ToString()),
        decimal.Parse(rdr["Result4"].ToString()),
        decimal.Parse(rdr["Result5"].ToString()),
        decimal.Parse(rdr["Result6"].ToString()),
        decimal.Parse(rdr["Result7"].ToString()),
        decimal.Parse(rdr["Result8"].ToString()),
        decimal.Parse(rdr["Result9"].ToString()),
        decimal.Parse(rdr["Result10"].ToString()),
        decimal.Parse(rdr["Result11"].ToString()),
        decimal.Parse(rdr["Result12"].ToString())
};

which is working well if all Result are not null. If any of it is null I have error Input string was not in a correct format. How do I handle null values so there is no error?


Answers (10)