Object reference not set to an instance of an object

Lenin82

Member
Joined
Jun 20, 2003
Messages
6
Hi Im Pretty new to Programming and all. Im having a problem with some code.

Code:
 Inherits System.Windows.Forms.Form
    Dim objConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\northwind.mdb")
    Dim objInformationDA As New OleDb.OleDbDataAdapter() "SELECT * FROM Employees WHERE City LIKE %on%", objConnection)
    Dim objInformationCB As New OleDb.OleDbCommandBuilder(objInformationDA)
    Dim objDataSet As New DataSet()
    
    Public Sub SearchCity()
        objInformationDA.SelectCommand.CommandText = "Select * From Employees Where City Like %" & txtCity.Text & "%"
        objInformationDA.SelectCommand.Connection.ConnectionString = _
           "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\northwind.mdb"

    End Sub


An unhandled exception of type System.NullReferenceException occurred in AddressFinder1.exe

Additional information: Object reference not set to an instance of an object.

I think Ive declared all needed objects but I keep getting that error when I run SearchCity() .I believe its cause I"m trying to use the SelectCommand from the DataAdapter. I know I could probably initialize the DataAdapter with all the info right off the bat but I need to run custom commands on the Data that I will be retriving as you can see. I get that error when I objInformationDA.SelectCommand.CommandText is run and also when the other line of code gets called. Just wondering what Im doing wrong. Any help/advise will be greatly appreciated.
 
Try something like
Dim cmd As New OleDbCommand("SELECT * FROM table", objConnection)
objInformationDA.SelectCommand = cmd
 
Back
Top