Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass null value to integer variable in .vb.net

Tags:

vb.net

i have integer variable like this:

Dim valetid as integer
 If cvalettype.Checked Then
            valetid = RecordID("vtid", "VType_tbl", "Vtype", cmbvalettype.Text)
        Else
            valetid = 0
        End If 

if condition coming to else case the valetid taking 0 value.that is saving in database as 0 only. if condition coming to else case i want to save my valetid in mydatabase as Null(now in my database saving valetid as 0).in my database Valetid datatype i declared as int.how i can do this?

like image 225
user2674855 Avatar asked Jun 20 '26 11:06

user2674855


1 Answers

First, your integer variable has to be nullable in both the VB code and the database. Assuming that the database allows nulls for this field, you can do something like this:

Dim valetid as Integer?
If cvalettype.Checked Then
    valetid = RecordID("vtid", "VType_tbl", "Vtype", cmbvalettype.Text)
Else
    valetid = Nothing
End If

' Do stuff with your valetid variable here

Or, as Neolisk prefers it:

Dim valetid as Nullable(Of Integer)
If cvalettype.Checked Then
    valetid = RecordID("vtid", "VType_tbl", "Vtype", cmbvalettype.Text)
Else
    valetid = Nothing
End If

' Do stuff with your valetid variable here
like image 178
Douglas Barbin Avatar answered Jun 22 '26 23:06

Douglas Barbin