Exforsys

Home arrow Reviews arrow ODP.NET

ODP.NET - Handling Nulls when Executing with ExecuteScalar

Author: Packt Publishing     Published on: 5th Apr 2008    |   Last Updated on: 9th Apr 2008

ODP.NET - Handling Nulls when Executing with ExecuteScalar

The most important issue to remember is that ExecuteScalar simply returns an object type of data. The object refers to any data type within .NET. If the data type of your variable matches with the type of object returned by ExecuteScalar, an implicit (automatic) conversion takes place.

Ads

There would not be a problem as long as the data types match. However, it would be a problem if the result is NULL. Let us have an example that accepts an employee number from the user and gives his or her commission:

Sample Code
  1. Imports Oracle.DataAccess.Client
  2.  
  3. Public Class Form12
  4.  
  5.      Private Sub btnGetCommission_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetCommission.Click
  6.         'create connection to db
  7.             Dim cn As New OracleConnection("Data Source=xe; _
  8.                         User Id=scott;Password=tiger")
  9.                 Try
  10.                 'create the command object
  11.                 Dim cmd As New OracleCommand("SELECT comm FROM _
  12.                             emp WHERE empno=" & Me.txtEmpno.Text, cn)
  13.                     'open the connection from command
  14.                 cmd.Connection.Open()
  15.                     'execute the command and get the single value
  16.                 'result
  17.                     Dim result As Double = cmd.ExecuteScalar
  18.                 cmd.Connection.Close()
  19.                     cmd.Dispose()
  20.                     'display the output
  21.                     MessageBox.Show("Commission: " & result)
  22.                     Catch ex As Exception
  23.                     'display if any error occurs
  24.                     MessageBox.Show("Error: " & ex.Message)
  25.                     'close the connection if it is still open
  26.                     If cn.State = ConnectionState.Open Then
  27.                         cn.Close()
  28.                     End If
  29.             End Try
  30.     End Sub
Copyright exforsys.com



In the highlighted statement above, we are expecting a numeric (or double) value as the result. If the ExecuteScalar returns a double value, it would never be a problem. What if it returns a NULL? The following is the error you would receive:

Ads

To deal with the above error, we may have to include our own condition to test against nulls in the output. Just replace the highlighted code above with the following two statements and it should work fine now:

Dim result As Object = cmd.ExecuteScalar
If IsDBNull(result) Then result = 0


You can observe from the above two lines that we are receiving the value in the form of an object and assigning a value zero if it is null.



 
This tutorial is part of a ODP.NET tutorial series. Read it from the beginning and learn yourself.

ODP.NET

 

Comments