Segun Olanipekun
Segun Olanipekun

Reputation: 3

ConnectionString property has not been initialized

I have to improve my coding in order to connect from VB to database, still it wouldn't let it keeps suggesting "ConnectionString property has not been initialized".

MessageBox.Show("Order sent")

    provider = "Provider=Microsoft.Jet.OLEDB.12.0; Data Source="
    dataFile = "J:\Database511_be.accdb"
    myConnection.ConnectionString = connString
    myConnection.Open()>>>>>THIS IS THE SECTION THAT BUGS ME, IT WOULDN'T  CONNECT TO THE DATABASE
    Dim str As String


    str = "Insert INTO CUSTOMER_DATABASE([Items],[Pizza Size],[Quantity],[Table Number], [Total]) Values ( ?,?,?,?)"
    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)

    cmd.Parameters.Add(New OleDbParameter("Items", CType(TextBox3.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Pizza Size", CType(TextBox4.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Quantity", CType(TextBox5.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Table Number", CType(ListBox4.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Total", CType(TextBox8.Text, String)))

    Try

        cmd.ExecuteNonQuery()
        cmd.Dispose()
        myConnection.Close()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox8.Clear()

    Catch ex As System.Exception


    End Try

Upvotes: 0

Views: 89

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123409

You are defining your connection string but you are not associating it with the OleDbConnection object. You want to do this:

Dim connection As New OleDbConnection(connString)

or, even better

Using connection As New OleDbConnection(connString)
    connection.Open()
    ' ... do stuff with the open connection
End Using

Upvotes: 1

Related Questions