Error: Object cannot be cast from DBNull to other types when running through for-loop VB.NET -


i have problem null value. need if check if 2 values null. if loop through first time, fine. when loop through second time, error on 'otabletemp.rows(i)("prospectielijstid").tostring() = ""'. have absolutly tried everything. i've tried dbnull.value, nothing,... full code:

    integer = 0 otabletemp.rows.count - 1 step 1          if (convert.toint32(otabletemp.rows(i)("opleidingsid")) = 0 , otabletemp.rows(i)("prospectielijstid").tostring() = "" , otabletemp.rows(i)("actieid") = specialeactieid)             cstring &= "select * aitest..specialeactie specialeactie.actieid = " + clglobalsdata.g_fieldn(specialeactieid)             exit         elseif (otabletemp.rows(i)("prospectielijstid").tostring() isnot "" , otabletemp.rows(i)("actieid") = specialeactieid)             cstring &= "    select klantprospectalgemeen.nummer, specialeactie.titel, specialeactie.inhoud, specialeactie.link " & _             "from (aitest..specialeactie inner join aitest..t_prospectielijst on t_prospectielijst.naamlijst= " & _             "specialeactie.prospectielijstid) inner join aitest..klantprospectalgemeen on t_prospectielijst.nummer=klantprospectalgemeen.nummer specialeactie.actieid = " + clglobalsdata.g_fieldn(specialeactieid)             exit         elseif (convert.toint32(otabletemp.rows(i)("opleidingsid")) <> 0 , otabletemp.rows(i)("actieid") = specialeactieid)             cstring &= "select klantprospectalgemeen.nummer, specialeactie.titel, specialeactie.inhoud, specialeactie.link " & _             "from (aitest..specialeactie inner join aitest..cursus on cursus.nummerinterncursus=specialeactie.opleidingsid " & _             "inner join aitest..cursusemailmarketing on cursus.nummerinterncursus = cursusemailmarketing.nummerinterncursus " & _             "inner join aitest..t_prospectielijst on t_prospectielijst.naamlijst=cursusemailmarketing.prospectielijstid) " & _             "inner join aitest..klantprospectalgemeen on t_prospectielijst.nummer= klantprospectalgemeen.nummer specialeactie.actieid = " + clglobalsdata.g_fieldn(specialeactieid)             exit         end if     next 

please me, i'm desperate..

first, i'm not sure causes error exactly, however, here general tips can find reason error , fix it:

i suggest use field extension method of datarow typed , supports nullable types. automatically converts dbnull.value appropriate nullable type.

you should use andalso instead of and. former not execute second part if first false whereas latter execute both can result in errors this.

consider this:

dim str string = nothing if str isnot nothing , str.length = 0    ' boooom end if 

this works:

if str isnot nothing andalso str.length = 0     ' no booom  end if 

here sql part show example more readable:

for each row datarow in otabletemp.rows     dim opleidingsid = row.field(of int32)("opleidingsid")     dim prospectielijstid = row.field(of string)("prospectielijstid")     dim actieid = row.field(of int32)("actieid")      if opleidingsid = 0 andalso string.isnullorwhitespace(prospectielijstid) andalso actieid = specialeactieid         cstring = "select * aitest..specialeactie ..."     end if 

note open sql-injection, use sql-parameters!


Comments