BlakeMcKenna
asked on
How to assign a NULL value to string and numeric variables???
I need to be able to assign NULL to string and numeric variables in my VB.Net app. Based on the research that I have done, I'm not sure that it can be done. Hopefully I'm wrong. I need to be able to look in a SQL Server DB and columns and see NULL if there is no data in them. I don't want them blank for VARCHAR columns or 0 for NUMERIC columns.
Thanks,
Thanks,
ASKER
My apologies...I actually mistyped what I was wanting to do. I need to assign a NULL VALUE to a control, i.e. TextBox or ComboBox if the field remains empty.
They start out that way, effectively. If you need to store a null value and the control simply returns a blank, check for a blank first and store a null if it is blank. You might need an intermediate variable to hold the control's value before you put it in the database.
ASKER
I see what you mean. However, I did try to create a variable in a Module and make it Public. I got an error in doing so. Please see the attached Image.
Thanks,
Sreenshot.jpg
Thanks,
Sreenshot.jpg
Try
Public NULLValue As Nullable(of T As String)
...
...though that may not be right either...
Public NULLValue As Nullable(of T As String)
...
...though that may not be right either...
ASKER
Still having issues with that. It doesn't like the syntax. See attached image.
Sreenshot.jpg
Sreenshot.jpg
Hi BlakeMcKenna;
The Nullable data types are for value types and NOT for Reference Types as String is a Reference Type.
Dim NullValue As String
NullValue = "Has a value"
NullValue = Nothing ''' The variable NullValue now is null / Nothing
The Nullable data types are for value types and NOT for Reference Types as String is a Reference Type.
Dim NullValue As String
NullValue = "Has a value"
NullValue = Nothing ''' The variable NullValue now is null / Nothing
ASKER
My scenario is that I have tried to assign "Nothing" to Class Properties Fields that are defined as Decimal and Integer. The assignment of "Nothing" worked, however, it did not store in the database as NULL rather 0. That is my dilemma. I need to store a NULL value in database columns that are defined as some kind of numeric. I just don't know how to do that programmatically...
When you have a null/Nothing in your code and want to write that null value to a database try using DBNull.Value in its place.
ASKER
I tried this:
Dim i As Integer = 0
i = DBNull.Value
It did not like this. If this isn't what you mean, can you plz show me the syntax you are referring to?
Thanks,
Dim i As Integer = 0
i = DBNull.Value
It did not like this. If this isn't what you mean, can you plz show me the syntax you are referring to?
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dim MyString? As Boolean
Or
Dim MyString As Nullable(Of String)