c# - Reading Null DateTime column from a Table in SqlServer -


i having issue reading calls call table. 1 of columns datetime column, , column have allowed null, if call has not been closed. when execute c# code fetch calls calls table, error data null, cannot read when no data present. thought if had if statement checks if column null, solve issue... doesn't work.

here code experiencing problem:

public void getallcalls(connection connectionobject)     {         try         {             if (memoryobject.getcallcount() != 0)             {                 memoryobject.deleteallcalls();             }              sqlcommand getcalls = new sqlcommand("sp_getallcalls", connectionobject.getdatabaseconnection());             getcalls.commandtype = commandtype.storedprocedure;             datareader = getcalls.executereader();             if (datareader.hasrows)             {                 while (datareader.read())                 {                     if (datareader.getdatetime(8).equals(dbnull.value))                     {                         call newcall = new call(datareader.getstring(0), datareader.getstring(1), datareader.getstring(2),                         datareader.getstring(3), datareader.getstring(4), datareader.getstring(5),                         datareader.getstring(6), datareader.getdatetime(7), convert.todatetime(null),                         datareader.getstring(9), datareader.getstring(10), datareader.getstring(11),                         datareader.getstring(12));                         memoryobject.addcall(newcall);                     }                     else                     {                         call newcall = new call(datareader.getstring(0), datareader.getstring(1), datareader.getstring(2),                         datareader.getstring(3), datareader.getstring(4), datareader.getstring(5),                         datareader.getstring(6), datareader.getdatetime(7), datareader.getdatetime(8),                         datareader.getstring(9), datareader.getstring(10), datareader.getstring(11),                         datareader.getstring(12));                         memoryobject.addcall(newcall);                     }                  }                 datareader.close();             }             else             {                 datareader.close();                 functionsobject.displaymessage("no calls found", "no calls found on system");             }         }         catch (exception error)         {             lognewerror(error, "getallcalls", connectionobject);         }      } 

the correct way of checking using sqldatareader.isdbnull method

if (datareader.isdbnull(8)) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -