SideBar    Using T-SQL to Avoid Unwanted NULLs
DOWNLOAD THE CODE:
Download the Code 37456.zip

The first technique I use to test for NULL uses the Visual Basic .NET TypeOf function to examine the object data type:

If TypeOf dv Is DBNull Then
   txtDateShipped.Text =
     "<Unknown>"
Else
   txtDateShipped.Text =
     drReader(0).ToString
End If

The next technique compares the data value in dv by using the expression Is DBNull.Value, which returns True if the object referenced is NULL:

If dv Is DBNull.Value Then
   txtDateShipped.Text =
     "<Unknown>"
Else
   txtDateShipped.Text =
     drReader(0).ToString
End If

You can also use the IsDBNull expression to test for NULL against a specific SqlDataReader item; IsDBNull returns True if the column is set to NULL:

If IsDBNull(drReader(0)) Then
   txtDateShipped.Text =
     "<Unknown>"
Else
   txtDateShipped.Text =
     drReader(0).ToString
End If

And the final technique, which Listing 1 shows, uses the IsDBNull method of the specified SqlDataReader or Rows object, which returns True if the specified column contains a NULL.

Passing a NULL Value Back to the Server
In some cases, you might have to pass a NULL back to the server either as the contents of an updateable DataTable's DataRow or as a parameter in a query invocation. Visual Basic .NET and other languages make this operation easy—but different from what you might be used to. The code in Listing 2 executes the AcceptNullParms sample stored procedure. The stored procedure accepts two input parameters. The first parameter defaults to NULL, so if no parameter is passed, the default value of NULL is applied. The second parameter is required but can be set to NULL in your ADO.NET code (as Listing 2 shows) when you set up the Parameters collection.

The code to execute this stored procedure in Visual Basic .NET isn't complex. Notice that when I set up the Parameters collection, I don't specify a value for the first parameter, so ADO.NET passes default as the first parameter, as the SQL Server Profiler trace below shows. When I ran Profiler on the ADO.NET code that used the DataAdapter Fill method to execute the query, Profiler reported that the following query resulted:

exec AcceptNullParms @DateShipped
  = default, @QtyOrdered = NULL

I set the second Parameter.Value property to DBNull.Value to pass NULL as the data value, as the code in Listing 3 shows.

When you need to set a data column value to NULL in a DataRow (and the column permits you to set the value to NULL), simply set the object to DBNull.Value:

ds.Tables(0).Rows(0)("ISBN") =
 DBNull.Value

As more developers wade into ADO.NET, the number of questions about nothing—that is, about handling NULLs—is increasing proportionately. Fortunately, ADO.NET and the .NET languages provide far better support for nothing (er, NULL) than ADO and VB 6.0 do.

End of Article

Prev. page     1 2 [3]     next page -->



You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

Does any one know how to count the ODBC/ADO connections to a database?

I don't seem to be able to find any info on msdn or other lit about counting connections.

I have a problem with connection pooling and an error 8004005 where the error is ramdon and does not leave any trace except to tell me it's an "unspecified error" comment.

the software is using ADO, asp and an oracle database using odbc.

I can be reached at carlos@dcu-inc.com, thanks...

carlos

The VB6 example If Not rs(0) = Null Then Text1.Text = rs(0) Else Text1.Text = "<Unknown>" End If will not work unless there is some special setting I'm unaware of. The Null will progagate through the expression until it can no longer propagate at which point VB will treat it as false. The code

If Not rs(0)=Null then MsgBox "Not Null" EndIf If rs(0)=Null then MsgBox "Null" EndIf Although it seems one of these should be true, neither MsgBox will display because the Null will propagate through both of these and both will evaluate to false.

Craig Bennett

 
 

ADS BY GOOGLE