Object reference not set to an instance of an object.

  • Thread starter Thread starter TG
  • Start date Start date
T

TG

Can someone spot my error in this code? I get the error message
"Object reference not set to an instance of an object." when I attempt
to read from the OleDbDataReader.

Thanks

BTW, I posted this yesterday, but I don't see it in the NG. My
apologies if this turns out to be a second posting.
******************************
Private Sub Test2()
Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;"
& _
"Data Source=c:\;" & _
"Extended Properties='text;HDR=Yes;FMT=Delimited'"
Dim Conn As New OleDbConnection(ConnString)
Dim Qry As String = "SELECT * from Global-List.txt"
Dim Cmd As New OleDbCommand(Qry, Conn)
Dim Dr As OleDbDataReader

Conn.Open()

Try
Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

While Dr.Read 'Object reference not set to an instance of an
object.
Console.WriteLine(Dr.GetString(0))
End While

Dr.Close()
End Sub
 
(e-mail address removed) (TG) wrote in
Can someone spot my error in this code? I get the error message
"Object reference not set to an instance of an object." when I
attempt to read from the OleDbDataReader.

<code snipped>

TG,

Check the console output to make sure there isn't an exception being
thrown in the "try" block.

It seems to me that "Dr =
Cmd.ExecuteReader(CommandBehavior.CloseConnection)" is throwing an
exception, which is caught in the "catch" block. This means the
variable "Dr" will be null.

If this is the case, then the connection string is probably causing
the exception. The DataSource is set to c:\. It should be set to
the database's filename (e.g. c:\mydata\mydatabase.mdb).

Hope this helps.

Chris.
 
Back
Top