Home All Groups Group Topic Archive Search About

Testing for OleDbDataReader is nothing before closing it

Author
4 Jul 2006 11:34 AM
Jerry Spence1
I have been doing the following is VB.Net 2003:

Dim dr1 As OleDbDataReader


.....Code here



If Not dr1 Is Nothing Then

      If Not dr1.IsClosed Then
          dr1.Close()
      end if

End Try


However in VB2005 I get the (correct) error message

dr1 is used before it has been assigned a value. A Null reference exception
could result at run time.

What syntax should I be using now?

Thanks

-Jerry

Author
4 Jul 2006 2:28 PM
M. Posseth
You have 2 options


1. set this naging warning off

2. always assign default values

in the case of a  object without a constructor assign Nothing
Dim dr As OleDb.OleDbDataReader = Nothing
otherwise just construct the object ( foo  As New .......)

regards

Michel Posseth [MCP]    


Show quoteHide quote
"Jerry Spence1" wrote:

> I have been doing the following is VB.Net 2003:
>
> Dim dr1 As OleDbDataReader
>
>
> .....Code here
>
>
>
> If Not dr1 Is Nothing Then
>
>       If Not dr1.IsClosed Then
>           dr1.Close()
>       end if
>
> End Try
>
>
> However in VB2005 I get the (correct) error message
>
> dr1 is used before it has been assigned a value. A Null reference exception
> could result at run time.
>
> What syntax should I be using now?
>
> Thanks
>
> -Jerry
>
>
>