vb.net - Validation to get null values -
i trying make validation if there no value on label application run normally. explain better show the code using , explain it
private sub showtotalresult() sqlcon = new sqlconnection sqlcon.connectionstring = "...." try sqlcon.open() dim query string query = "select cast(sum(cast(filesize float)) / 1024 / 1024 decimal(10,2)) infofile" sqlcmd = new sqlcommand(query, sqlcon) sqldr = sqlcmd.executereader if lblresultadototal.text <> "" if sqldr.read() lblresultadototal.text = sqldr.getdecimal(0) exit sub end if end if sqlcon.close() catch ex sqlexception msgbox(ex.message) sqlcon.dispose() end try end sub
what piece of code giving me total in mb of got on database. make tests deleted info database , when i've tried run application gives me error on line lblresultadototal.text = sqldr.getdecimal(0)
because there no value on label. turn around do?
i've tried use if lblresultadototal.text <> "" lblresultadototal.text = sqldr.getvalue(0) end if
this query using values database
select cast(sum(cast(filesize float)) / 1024 / 1024 decimal(10,2)) infofile
this error data null. method or property cannot called on null values.
not work. have solution?
you cannot call getdecimal if field null because internally getdecimal method tries cast null value decimal. can read warning in remarks section of getdecimal page on msdn
it easy fix using sqldatareader.isdbnull
' call sqldatareader.read required position ' ' reader on first record returned query' if sqldr.read() if sqldr.isdbnull(0) lblresultadototal.text = "0,00" else lblresultadototal.text = sqldr.getdecimal(0) end if end if
however, bit perplexed casts inside query. raw value , try convert values obtained in vb.net code more easy it. (need test performances)
Comments
Post a Comment