0

I have a few items returning DBnull pulling from the database which throws an exception. What is the best way to handle the exception? More specifically for Daily and EndDate. Daily is a bit datatype in sql and EndDate is DateTime. Here is how I have the code currently implemented.

while (reader.Read())
                {
                    sessionInfo.ID = sessionId;
                    sessionInfo.Name = Convert.ToString(reader["Name"]);
                    sessionInfo.GroupID = Convert.ToString(reader["GroupId"]);
                    sessionInfo.Disabled = Convert.ToBoolean(reader["IsDisabled"]);
                    sessionInfo.IsTestSession = Convert.ToBoolean(reader["IsTestSession"]);
                    sessionInfo.NextTrigger = reader["NextExecutionTime"] == DBNull.Value ? null : (DateTime?)reader["NextExecutionTime"];
                    sessionInfo.Type = Convert.ToString(reader["SType"]);
                    sessionInfo.POKeepDays = Convert.ToInt32(reader["DaysToKeep"]);
                    sessionInfo.When = new SchedulerInfo()
                    {
                        ID = sessionId,
                        SessionID = sessionId,
                        //TODO Recurrence = 
                        //TODO MultiTriggerTimes
                        //TODO MultiTriggerColl                          
                        MinuteFreq = Convert.ToString(reader["MinuteFreq"]),
                        HourFreq = Convert.ToString(reader["HourFreq"]),
                        Daily = reader.GetBoolean(reader.GetOrdinal("Daily")),
                        WeekFreq = Convert.ToString(reader["WeekFreq"]),
                        StartDate = Convert.ToDateTime(reader["StartDate"]),
                        EndDate = Convert.ToDateTime(reader["EndDate"]),
                        NoOrderDays = Convert.ToString(reader["NoOrderDays"]),
                        OrderDays = Convert.ToString(reader["OrderDays"]),
                        SelectedHolidays = Convert.ToString(reader["NoOrderHoliday"]),
                        SelectedOrderHolidays = Convert.ToString(reader["HolidayName"]),

I am hoping to learn how to best handle the exception being thrown for a BdNull value

1 Answers1

0
  • Avoid using Convert.ToFoo, instead use the GetFoo methods on DataReader directly.
  • If you use DataReader.IsDBNull() first then you won't get a DBNull-related exception in the first place.

Like so:

while (reader.Read())
{
    sessionInfo.ID = sessionId;
    sessionInfo.Name = reader.IsDBNull( "Name" ) ? null : reader.GetString( "Name" );
    sessionInfo.GroupId = reader.IsDBNull( "GroupId" ) ? null : reader.GetString( "GroupId" );
    sessionInfo.Disabled = reader.IsDBNull( "Disabled" ) ? false : reader.GetBoolean( "Disabled" );
    // etc...
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Worked great for EndDate, now I am having issues with Daily. Daily = reader.IsDBNull( "Daily" ) ? false : reader.GetBoolean( "Daily" ), It is looking for an INT and not a string value in the argument. –  Jul 16 '19 at 21:42
  • @JadenAdams Pass the result of `GetOrdinal` into both `IsDBNull` and `GetBoolean`. You might want to store it in an intermediate `int` variable before the first call to `reader.Read()`. – Dai Jul 16 '19 at 21:54