Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

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,
Avatar of unknown_routine
unknown_routine
Flag of United States of America image

Use nullable types:

Dim MyString? As Boolean


Or

Dim MyString As Nullable(Of String)
Avatar of Paul MacDonald
Read this:
http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

Then make your variables nullable.
Avatar of BlakeMcKenna

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.
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
Try
  Public NULLValue As Nullable(of T As String)
   ...


...though that may not be right either...
Still having issues with that. It doesn't like the syntax. See attached image.
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
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.
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,
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial