3

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?

5
  • 3
    Please add the code that sends the value to the database. And when you edit that in, please highlight it and hit the {} button (as I've done for your existing code) so that it gets marked up as code with syntax highlighting. Commented Aug 15, 2013 at 6:34
  • 2
    Nullable(Of Integer) might be an option... Commented Aug 15, 2013 at 6:36
  • if i declare Nullable(Of Integer) .how i can pass value Commented Aug 15, 2013 at 6:37
  • See My post : stackoverflow.com/a/18247643/2218635 Commented Aug 15, 2013 at 6:51
  • "Possible Duplicate" : stackoverflow.com/questions/4937848/… Commented Aug 15, 2013 at 7:05

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

4 Comments

+1, although I prefer more clear syntax Nullable(Of Integer) instead of this confusing ?.
It's just syntactic sugar. Nullable(Of Integer) or Nullable<int> are the same things, just a lot more typing.
No offence - merely trying to promote code clarity here. My preference is to leave code as clear as possible for someone who just started learning VB.NET, came from the old VB6 world or even another language/framework.
Updated to include alternate syntax.
0

I would;

    If cvalettype.Checked Then
        valetid = RecordID("vtid", "VType_tbl", "Vtype", cmbvalettype.Text)
    Else
        valetid = nothing
    End If 

And then later you can test;

    if valetid isnot nothing then
         'write valetid to database
    else
         'write dbnull to database
    end if

7 Comments

no.sir i tryed this already..this is inserting value 0 in mydatabase
you didnt specify how you write back to the database, you can still use isnot nothing, and if is nothing; write dbnull to the database...
int is value type so how can you set null ?
how to write dbnull to datbase
instead of int what datatype i have to give in my database
|
-1

Is your integer declared as nullable integer? If so, you could simply do this:

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

3 Comments

i given valetid as Nullable(Of Integer) that time showing error:Property HasValue is Readonly
Ah sorry, you can assign it like this: valetid = nothing. If you don't know nullables, read this article: msdn.microsoft.com/en-us/library/ms235245.aspx
i assigned valetid = nothing but stiil am getting 0 only in my databse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.