|
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:
'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() 'create the data reader Dim rdr As OracleDataReader = _ cmd.ExecuteReader(CommandBehavior.CloseConnection) 'check if it has any rows If rdr.HasRows Then 'read the first row rdr.Read() 'extract the details Dim result As Double = IIf(IsDBNull(rdr("comm")), _ 0, rdr("comm")) MessageBox.Show("Commission: " & result) Else 'display message if no rows found MessageBox.Show("Not found") End If rdr.Dispose() 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
%27create%20connection%20to%20db%20%0ADim%20cn%20As%20New%20OracleConnection%28%22Data%20Source%3Dxe%3B%20_%20%0A%20%20%20%20%20User%20Id%3Dscott%3BPassword%3Dtiger%22%29%20%0ATry%20%0A%20%20%20%20%20%27create%20the%20command%20object%20%0A%0A%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%20emp%20WHERE%20empno%3D%22%20%26amp%3B%20Me.txtEmpno.Text%2C%20cn%29%20%0A%20%20%20%20%20%27open%20the%20connection%20from%20command%20%0A%20%20%20%20%20cmd.Connection.Open%28%29%20%0A%20%20%20%20%20%27create%20the%20data%20reader%20%0A%20%20%20%20%20Dim%20rdr%20As%20OracleDataReader%20%3D%20_%20%0A%20%20%20%20%20cmd.ExecuteReader%28CommandBehavior.CloseConnection%29%20%0A%20%20%20%20%20%27check%20if%20it%20has%20any%20rows%20%0A%20%20%20%20%20If%20rdr.HasRows%20Then%20%0A%20%20%20%20%20%20%20%20%20%20%27read%20the%20first%20row%20%0A%20%20%20%20%20%20%20%20%20%20rdr.Read%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%27extract%20the%20details%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20result%20As%20Double%20%3D%20IIf%28IsDBNull%28rdr%28%22comm%22%29%29%2C%20_%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%200%2C%20rdr%28%22comm%22%29%29%20%0A%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%20Else%20%0A%20%20%20%20%20%20%20%20%20%20%27display%20message%20if%20no%20rows%20found%20%0A%20%20%20%20%20%20%20%20%20%20MessageBox.Show%28%22Not%20found%22%29%20%0A%20%20%20%20%20End%20If%20%0A%20%20%20%20%20rdr.Dispose%28%29%20%0ACatch%20ex%20As%20Exception%20%0A%20%20%20%20%20%27display%20if%20any%20error%20occurs%20%0A%20%20%20%20%20MessageBox.Show%28%22Error%3A%20%22%20%26amp%3B%20ex.Message%29%20%0A%20%20%20%20%20%27close%20the%20connection%20if%20it%20is%20still%20open%20%0A%20%20%20%20%20If%20cn.State%20%3D%20ConnectionState.Open%20Then%20%0A%20%20%20%20%20%20%20%20%20%20cn.Close%28%29%20%0A%20%20%20%20%20End%20If%20%0AEnd%20Try
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)
|