Null or nonexistent data gets complicated when the application expects a data value and gets nothing or unexpected values; that's when you must perform coding that properly handles these situations to avoid application failure. This requires a close examination of the data wherever and whenever it's needed.
Null and void
A null value is a value that doesn't refer to any object. It's the default value of reference-type variables (e.g., class, interface, delegate).
These items are called reference types because they're objects that store references to the actual data. String, object, and struct are reference type examples. The null keyword is a literal that represents a null reference with the C# .NET environment. You may use the null keyword to check or assign the value of an object. For example, the following C# code determines if a string value is null:
string sTest = "Test";
if (sTest == null) {
Console.WriteLine("sTest is Null")
}
This code works without any problems with C#, but there's no VB.NET equivalent of the null keyword. Instead, VB.NET uses the Nothing keyword. The following code demonstrates its use:
Dim sTest As String
If (sTest Is Nothing) Then
Console.WriteLine("sTest is Null")
End If
Another area of confusion stems from the fact that VB.NET doesn't treat null and Nothing as equals; consequently, a VB.NET programmer may have to check for both values.
The variation in support may cause confusion, but a developer rarely develops in both languages simultaneously. On the other hand, Microsoft provides a uniform method for working with null values: The base System.Convert namespace includes the DBNull object.
DBNull
The use of the DBNull class indicates the absence of a known value. While the Microsoft documentation says it's typically in a database application, you may also use it with any type of data.
In database applications, a null object is a valid value for a field. This class differentiates between a null value (a null object) and an uninitialised value (the DBNull.Value instance). For example, a table can have records with uninitialised fields. By default, these uninitialised fields have the DBNull value.
You can utilise the DBNull object by way of its Value property. This represents a null value, which is never equal to anything. The following C# snippet revises the previous example to utilise the DBNull class:
if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine("sTest is null.");
Notice how this code utilises the string class's equality method. The VB.NET sample may utilise the same code, like this:
If (sTest.Equals(System.DBNull.Value) Then
Console.WriteLine("sTest is Null")
End If
You may also use the DBNull.Value constant on the right-hand of an assignment operation, thus storing a null value within a reference type. In addition, the .NET Framework provides the IsDBNull function (in the System.Convert namespace), which is a shorthand approach for determining if a reference type contains a null value. The following VB.NET uses IsDBNull to check an object's value:
Dim oTest As Object = DBNull.Value
If ((IsDBNull(oTest))) Then
Console.WriteLine("oTest is Null")
End If
Even if you check for null values at every conceivable location within your code, they still may creep into the application. Fortunately, the try/catch exception block provides another line of defense; it allows you to catch any unhandled exceptions that null values cause. The System namespace provides the NullReferenceException for these situations. See how the following C# code uses NullReferenceException.
try {
if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine("sTest is null.");
}
} catch (System.NullReferenceException e) {
Console.WriteLine("Error: " + e.ToString());
}
Handle your data with care
Applications don't always receive the type of data they expect; for instance, an app may be looking for integer values and receive string or null values. Therefore, since data is the backbone of an application, you must be extremely careful when working with it.
Do you need help with .Net? 







1
Jyothi Narasimhan - 23/03/05
I would like to know if comparing null with variables like mentioned below is correct or not. If not why?
object objTemp;
if(null == objTemp) is correct or is if(objTemp == null) is correct???
» Report offensive content
2
Karthigaiya raja kumar - 29/04/05
The Article was very useful to handle reference type of objects when they are null. If possible please do let me know is there is any thing like 'null' to ****ign to value types like struct, when it is expecting any value and it did not get that.
Thanx
» Report offensive content
3
Matthew Wills - 18/11/05
>> This code works without any problems with C#, but there's no VB.NET equivalent of the null keyword. Instead, VB.NET uses the Nothing keyword.
Or, in other words "The VB.NET equivalent of the null keyword is the Nothing keyword." The current way you state is confusing at best.
>> Another area of confusion stems from the fact that VB.NET doesn't treat null and Nothing as equals; consequently, a VB.NET programmer may have to check for both values.
What are you talking about? VB.NET uses the word Nothing instead of null. This isn't a hard concept to grasp. You don't need to 'check for both values'. They are THE SAME VALUE.
» Report offensive content
4
Karthigaiyaraja kumar - 19/05/06
Thanks to Microsoft.. They provided the solution for the problem I had some one year ago(anywayz I tweaked my code to handle the situation)in the .net2.0 thru nullable types...
» Report offensive content
5
michros - 17/08/06
more info in adding null values into sql databse in asp.net:
http://www.dotnetspider.com/namespace/ShowClass.aspx?ClassId=55
» Report offensive content
6
kavita - 19/09/06
i need help
» Report offensive content
7
deven - 12/10/06
Set(ByVal Value As Byte())
If (Not _tempfile Is Nothing) Then
File.Delete(_tempfile)
End If
_tempfile = Path.GetTempFileName()
Dim stream As New FileStream(_tempfile, FileMode.Create, FileAccess.Write)
stream.Write(Value, 0, Value.Length)
stream.Close()
End Set
» Report offensive content
8
Sivakumar - 25/03/08
I am designing a new table with a few columns that may or may not have
a value on each row that is inserted.
What issues determine whether to allow a NULL value to be inserted for
that column or define a default value to be used?
For Eg: For the particular text box if client doesn't used means i need some message( N/A-Not Applicable) Like that I need ?
» Report offensive content
9
Guillermo Lopez - 30/08/08
when I open a web page I have a window tha said "windows can not fine "(null)" what is ti and how canfix it.
» Report offensive content