user3049808
user3049808

Reputation: 25

ADO.NET: The ConnectionString property has not been initialized

I dont know how i Can fixed the wrong in my program...the problem is con.open()

Private Sub btnadd1_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnadd1.Click

Dim cmd As New OleDb.OleDbCommand
Dim con As New OleDb.OleDbConnection
Dim Printlist1 As New DataTable

If Not con.State = ConnectionState.Open Then 
   con.Open()
   cmd.Connection = con
End If

    If Me.text1.Tag & "" = "" Then
        cmd.CommandText = "INSERT INTO Datagrid1(StickerCode, Description, Company, Department, Location, User, SerialNumber, DatePurchased, Tagable, Quantity, Brand, Model ) " & _
                        " VALUES(" & Me.text1.Text & ",'" & Me.text2.Text & "','" & _
                            Me.text3.Text & "','" & Me.text4.Text & "','" & Me.text5.Text & "','" & _
                            Me.text6.Text & "','" & Me.text7.Text & "','" & Me.text8.Text & "','" & _
                            Me.text9.Text & "','" & Me.text10.Text & "','" & Me.text11.Text & "','" & _
                            Me.text12.Text & "')"

        cmd = New OleDbCommand(cmd.CommandText, con)
        cmd.ExecuteNonQuery()
    Else


        cmd.CommandText = "UPDATE Form4 " & _
                    " SET StickerCode='" & Me.text1.Text & _
                    ", Description='" & Me.text2.Text & "'" & _
                    ", Company='" & Me.text3.Text & "'" & _
                    ", Department='" & Me.text4.Text & "'" & _
                    ", Location='" & Me.text5.Text & "'" & _
                    ", User='" & Me.text6.Text & "'" & _
                    ", SerialNumber='" & Me.text7.Text & "'" & _
                    ", DatePurchased='" & Me.text8.Text & "'" & _
                    ", Tagable='" & Me.text9.Text & "'" & _
                    ", Quantity='" & Me.text10.Text & "'" & _
                    ", Brand='" & Me.text11.Text & "'" & _
                    ", Model='" & Me.text12.Text & "'" & _
                    " WHERE text1=" & Me.text1.Tag


        cmd.ExecuteNonQuery()

    End If
    RefreshData()
    Me.btnclear1.PerformClick()
    con.Close()

End Sub

Upvotes: 0

Views: 349

Answers (2)

Steven Doggart
Steven Doggart

Reputation: 43743

You are not giving the OleDbConnection object a connection string. The connection string tells the OleDbConnection object to which database you want it to connect. You can give it the connection string in the constructor, like this:

Dim con As New OleDb.OleDbConnection(connectionString)

Or, you can set it afterwards, via the ConnectionString property, like this:

Dim con As New OleDb.OleDbConnection()
con.ConnectionString = connectionString

The format for the actual connection string varies depending on the database engine and version. There are plenty of resources online, though, which will show you the correct format to use and what options it can include. Just as an example, it might look something like this:

Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb"

UPDATE

Sorry if I've confused you by discussing the steps out of order. Basically, all you need to do is to give the connection string to the OleDbConnection object. You need to do so before you call its Open method. For instance, you can change the following line:

Dim con As New OleDb.OleDbConnection

To this:

Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb")

Obviously, however, you need to change the connection string to whatever it ought to be for your particular situation. The one above is merely an example of what a valid connection string looks like.

Or, if you don't want to give the connection string to the OleDbConnection object right away, you can wait to give it to it later, as long as you set it before calling Open. For instance, you could change the following lines:

Dim cmd As New OleDb.OleDbCommand
Dim con As New OleDb.OleDbConnection
Dim Printlist1 As New DataTable
If Not con.State = ConnectionState.Open Then 
    con.Open()
    ' ...

To this:

Dim cmd As New OleDb.OleDbCommand
Dim con As New OleDb.OleDbConnection
Dim Printlist1 As New DataTable
If Not con.State = ConnectionState.Open Then 
    con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb"
    con.Open()
    ' ...

The original example, which seems to have confused you, first stored the connection string in a variable, and then passed the value from that variable to the connection object, like this:

Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb"
Dim con As New OleDb.OleDbConnection(connectionString)

Upvotes: 1

AGB
AGB

Reputation: 2448

You haven't passed a connection string to your connection object so it has no idea which database to open or where to find it.

Try this:

Dim con As New OleDb.OleDbConnection("My connection string")

Upvotes: 1

Related Questions