Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to enter data NULL (rather than numeric numbers, etc.) to the field of type integer. I use the following code:

VB
Dim Col2 As Nullable(Of Integer) = Nothing
OpenDatabase()
Command.Connection = Con
Command.CommandType = CommandType.Text
Command.CommandText = "INSERT INTO SIGN(TYPESIGN_ID) VALUES('" + CStr(Col2) + "')"
Command.ExecuteNonQuery()
Con.Close()


the result is "Nullable object must have a value". where my mistake?
Note : I'm using VB .Net 2010 & sql server 2005
Posted
Updated 12-Oct-19 16:52pm

don't write 'nothing' in first line
or
you can write insert query like below,
"INSERT INTO SIGN(TYPESIGN_ID) VALUES(" + "null" + ")"

Happy Coding!
:)
 
Share this answer
 
Comments
om3n 28-Aug-12 23:39pm    
thanks all,
I've tried :

Dim Col2 As Nullable(Of Integer)
OpenDatabase()
Command.Connection = Con
Command.CommandType = CommandType.Text
Command.CommandText = "INSERT INTO SIGN(TYPESIGN_ID) VALUES('" + Col2 + "')"
Command.ExecuteNonQuery()
Con.Close()
Still can't, I get error "Conversion from string"')" to type 'Double' is not valid."

if using :
"INSERT INTO SIGN(TYPESIGN_ID) VALUES(" + "null" + ")"
the result is successfully.

however, what if insert null values ​​using variables?
Aarti Meswania 29-Aug-12 0:27am    
welcome :)
hiii,


SQL
To retrieve the value of a variable of a nullable type, you should first test its HasValue property to confirm that it has a value. If you try to read the value when HasValue is False, Visual Basic throws an InvalidOperationException exception. The following example shows the recommended way to read the variable Col2

If Col2.HasValue Then
    Command.CommandText = "INSERT INTO SIGN(TYPESIGN_ID) VALUES('" + CStr(Col2) + "')"
End If
 
Share this answer
 
Comments
om3n 29-Aug-12 22:37pm    
thanks for reply Ganesh Nikam :)
my issues are resolved
If this is a nullable object, you don't need to use CStr.
Or else check if Col2 is not null before using CStr.
 
Share this answer
 
Comments
om3n 28-Aug-12 23:14pm    
I've tried as you say:
Command.CommandText = "INSERT INTO SIGN(TYPESIGN_ID) VALUES('" + Col2 + "')"
I get error "Conversion from string"')" to type 'Double' is not valid."

I'm also try :
Command.CommandText = "INSERT INTO SIGN(TYPESIGN_ID) VALUES('" + Col2.ToString + "')"
Also get error "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_SIGN_TYPE_SIGN". The conflict occurred in database "database", table "dbo.TYPE_SIGN", coloumn 'TYPESIGN_ID'. The statement has been terminated."

if using Col2.ToString the value is 0
Abhinav S 28-Aug-12 23:23pm    
If this is a foreign key, it can only have values from within the primary key. If you try to set it to null or 0, you are going to get errors. Use it only with the proper values.
om3n 28-Aug-12 23:55pm    
thanks Abhinav S,
understood, but I need enter NULL value not 0 either numeric or alphabetic,
I tried directly to sql server database, there is can accept null values

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900