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.
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:
Imports Oracle.DataAccess.Client Public Class Form12 Private Sub btnGetCommission_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetCommission.Click 'create connection to db Dim cn As New OracleConnection("Data Source=xe; _ User Id=scott;Password=tiger") Try 'create the command object Dim cmd As New OracleCommand("SELECT comm FROM _ emp WHERE empno=" & Me.txtEmpno.Text, cn) 'open the connection from command cmd.Connection.Open() 'execute the command and get the single value 'result Dim result As Double = cmd.ExecuteScalar cmd.Connection.Close() cmd.Dispose() 'display the output MessageBox.Show("Commission: " & result) Catch ex As Exception 'display if any error occurs MessageBox.Show("Error: " & ex.Message) 'close the connection if it is still open If cn.State = ConnectionState.Open Then cn.Close() End If End Try End Sub
Imports%20Oracle.DataAccess.Client%20%0A%0APublic%20Class%20Form12%20%0A%0A%20%20%20%20%20Private%20Sub%20btnGetCommission_Click%28ByVal%20sender%20As%20%0A%20%20%20%20%20System.Object%2C%20ByVal%20e%20As%20System.EventArgs%29%20Handles%20%0A%20%20%20%20%20btnGetCommission.Click%20%0A%20%20%20%20%20%20%20%20%20%20%27create%20connection%20to%20db%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20cn%20As%20New%20OracleConnection%28%22Data%20Source%3Dxe%3B%20_%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20User%20Id%3Dscott%3BPassword%3Dtiger%22%29%20%0A%20%20%20%20%20%20%20%20%20%20Try%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27create%20the%20command%20object%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Dim%20cmd%20As%20New%20OracleCommand%28%22SELECT%20comm%20FROM%20_%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20emp%20WHERE%20empno%3D%22%20%26amp%3B%20Me.txtEmpno.Text%2C%20cn%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27open%20the%20connection%20from%20command%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cmd.Connection.Open%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27execute%20the%20command%20and%20get%20the%20single%20value%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27result%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Dim%20result%20As%20Double%20%3D%20cmd.ExecuteScalar%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cmd.Connection.Close%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cmd.Dispose%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27display%20the%20output%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20MessageBox.Show%28%22Commission%3A%20%22%20%26amp%3B%20result%29%20%0A%20%20%20%20%20%20%20%20%20%20Catch%20ex%20As%20Exception%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27display%20if%20any%20error%20occurs%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20MessageBox.Show%28%22Error%3A%20%22%20%26amp%3B%20ex.Message%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27close%20the%20connection%20if%20it%20is%20still%20open%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20If%20cn.State%20%3D%20ConnectionState.Open%20Then%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cn.Close%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20End%20If%20%0A%20%20%20%20%20%20%20%20%20%20End%20Try%20%0AEnd%20Sub
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:
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.
Trackback(0)
|