alt
Advertisement
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow ODP.NET arrow ODP.NET - Handling Nulls when Working with OracleDataReader
Site Search


ODP.NET - Handling Nulls when Working with OracleDataReader

ODP.NET - Handling Nulls when Working with OracleDataReader

When we work with OracleDataReader (or for that matter, even with data rows in a data table), we may come across nulls. The following is the efficient way to deal in with such scenarios:

  1. 'create connection to db
  2. Dim cn As New OracleConnection("Data Source=xe; _
  3. User Id=scott;Password=tiger")
  4. Try
  5. 'create the command object
  6.  
  7. Dim cmd As New OracleCommand("SELECT comm FROM _
  8. emp WHERE empno=" & Me.txtEmpno.Text, cn)
  9. 'open the connection from command
  10. cmd.Connection.Open()
  11. 'create the data reader
  12. Dim rdr As OracleDataReader = _
  13. cmd.ExecuteReader(CommandBehavior.CloseConnection)
  14. 'check if it has any rows
  15. If rdr.HasRows Then
  16. 'read the first row
  17. rdr.Read()
  18. 'extract the details
  19. Dim result As Double = IIf(IsDBNull(rdr("comm")), _
  20. 0, rdr("comm"))
  21. MessageBox.Show("Commission: " & result)
  22. Else
  23. 'display message if no rows found
  24. MessageBox.Show("Not found")
  25. End If
  26. rdr.Dispose()
  27. Catch ex As Exception
  28. 'display if any error occurs
  29. MessageBox.Show("Error: " & ex.Message)
  30. 'close the connection if it is still open
  31. If cn.State = ConnectionState.Open Then
  32. cn.Close()
  33. End If
  34. End Try
 

You can observe that we are making use of the IIF function in Visual Basic.NET to make the inline comparison. We can also use the rdr.isDBNull method to achieve the same.


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape