Cannot insert null into database vb.net -
i have been trying insert null database every time getting failed. tried several codes nothing working. when tried code:
if string.isnullorempty(textbox2.text) textbox2.text = dbnull.value else textbox2.text = textbox2.text end if
it gives me error :
value of type 'system.dbnull' cannot converted 'string'.
and when tried this:
if not string.isnullorempty(textbox2.text) = true textbox2 = system.dbnull.value else textbox2.text = textbox2.text end if
it gives me error:
value of type 'system.dbnull' cannot converted 'system.windows.forms.textbox'.
before these code have added validations in order make text box compulsory filled user.
this place getting error:
dsnewrow.item("emp_name") = textbox1.text dsnewrow.item("emp_age") = textbox2.text 'this place getting error' dsnewrow.item("emp_gend") = combobox1.text dsnewrow.item("emp_dob") = datetimepicker1.text dsnewrow.item("emp_mstatus") = textbox4.text dsnewrow.item("emp_qual") = richtextbox1.text dsnewrow.item("emp_exp") = richtextbox2.text
it shows:
input string not in correct format.couldn't store <> in emp_age column. expected type int32.
the dbnull.value
assigned datarow column using syntax (assuming, of course, emp_age
column nullable)
dsnewrow.item("emp_age") = if(string.isnullorwhitespace(textbox2.text), dbnull.value, textbox2.text)
here, use if conditional operator set value of column dbnull.value or textbox2.text
however, there problem not visible. emp_age
column requires integer value , shouldn't write in string. better check input value sure have valid integer value
dim age integer if int32.tryparse(textbox2.text, age) dsnewrow.item("emp_age") = age else dsnewrow.item("emp_age") = dbnull.value end if
Comments
Post a Comment