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,
BlakeMcKennaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

unknown_routineCommented:
Use nullable types:

Dim MyString? As Boolean


Or

Dim MyString As Nullable(Of String)
0
Paul MacDonaldDirector, Information SystemsCommented:
Read this:
http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

Then make your variables nullable.
0
BlakeMcKennaAuthor Commented:
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.
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Paul MacDonaldDirector, Information SystemsCommented:
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.
0
BlakeMcKennaAuthor Commented:
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
0
Paul MacDonaldDirector, Information SystemsCommented:
Try
  Public NULLValue As Nullable(of T As String)
   ...


...though that may not be right either...
0
BlakeMcKennaAuthor Commented:
Still having issues with that. It doesn't like the syntax. See attached image.
Sreenshot.jpg
0
Fernando SotoRetiredCommented:
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
0
BlakeMcKennaAuthor Commented:
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...
0
Fernando SotoRetiredCommented:
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.
0
BlakeMcKennaAuthor Commented:
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,
0
Fernando SotoRetiredCommented:
So if i is the value you will be sending to the database then you can do the following

' Setting i which is Type nullable of integer
' Setting i to Nothing to send to the database
Dim i As Nullable(Of Integer) = Nothing

' Create a SQL parameter to send to SQL server.
Dim param As New SqlParameter("@Value", SqlDbType.Int)

' Before filling the SqlParameter with the value to send to SQL server
' Test to see if you need to send a null 
If i Is Nothing Then
    param.Value = DBNull.Value
Else
    param.Value = x
End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.