hello press save, i got and error and said ExecuteNonQuery: Connection property has not been initialized.
Here's the code:

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim com As New OleDbCommand
        com.Connection = con
        com.CommandText = "insert into Sales values('" & txtSalesId.Text & "','" & txtCustomerId.Text & "','" & _
                            txtWatchId.Text & "','" & txtDate.Text & "'," & txtQuantity.Text & "," & txtAmount.Text & ")"
        com.ExecuteNonQuery()
        MsgBox("Record Inserted")
        Call populateCustomer()
        Call populateWatch()
    End Sub

Please any1 correct my statement
thanks

Recommended Answers

All 4 Replies

>Connection property has not been initialized.
Connection string is empty.

Dim com As New OleDbCommand
 Dim con as New OleDBConnection
 con.ConnectionString="Put_connection_string_here"
 com.Connection = con
 com.CommandText = "insert into Sales values('" & txtSalesId.Text & "','" & txtCustomerId.Text & "','" & _
                            txtWatchId.Text & "','" & txtDate.Text & "'," & txtQuantity.Text & "," & txtAmount.Text & ")"
  con.Open()
        com.ExecuteNonQuery()
    con.Close()
    ...
        MsgBox("Record Inserted")
        Call populateCustomer()
        Call populateWatch()
   ...

>Connection property has not been initialized.
Connection string is empty.

Dim com As New OleDbCommand
 Dim con as New OleDBConnection
 con.ConnectionString="Put_connection_string_here"
 com.Connection = con
 com.CommandText = "insert into Sales values('" & txtSalesId.Text & "','" & txtCustomerId.Text & "','" & _
                            txtWatchId.Text & "','" & txtDate.Text & "'," & txtQuantity.Text & "," & txtAmount.Text & ")"
        com.ExecuteNonQuery()
    con.Close()
    ...
        MsgBox("Record Inserted")
        Call populateCustomer()
        Call populateWatch()
   ...

Don't forget to open the connection before executing the query ;)

con.Open()
commented: Thanks Simon. +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.