Madan Raj
Madan Raj

Reputation: 37

The ConnectionString property has not been initialized

I am working on a project which is prescribed by Bangalore University. I am using vb.net 2008 as front end and Ms Access 2007 as back end. When I try to run the program I get a error as The ConnectionString property has not been initialized.

Here is the code that I have used

private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
    Dim con As New OleDb.OleDbConnection
    Dim cmd As New OleDb.OleDbCommand
    Dim dbProvider As String
    Dim dbSource As String

    dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "DataSource=C:\My Project\project.mdb"
    con.ConnectionString = dbProvider & dbSource
    con = New OleDb.OleDbConnection

    con.Open()
    cmd.Connection = con
    cmd.CommandText = "Insert Into Application(appno, mname, age, gender, height, weight, address, purpose, bmi, mailid, phoneno, paymnet)" & _
                      "Values('" & Me.appno.Text & "', '" & Me.mname.Text & "', '" & Me.age.Text & "', '" & Me.gender.Text & "', '" & _
                      Me.mheight.Text & "', '" & Me.weight.Text & "', '" & Me.address.Text & "', '" & Me.purpose.Text & "', '" & _
                      Me.bmi.Text & "', '" & Me.mailid.Text & "', '" & Me.phoneno.Text & "', '" & Me.payment.Text & "')"
    cmd.ExecuteNonQuery()
    con.Close()
End Sub

Upvotes: 1

Views: 2262

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 181077

You're creating two connections, you're setting the first one up, so the latter one is probably a copy and paste error;

Dim con As New OleDb.OleDbConnection

...

dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
dbSource = "DataSource=C:\My Project\project.mdb"
con.ConnectionString = dbProvider & dbSource
con = New OleDb.OleDbConnection                  <-- creates a new connection
                                                     without a connection string.

Upvotes: 1

Related Questions