exception on : ra = myCommand.ExecuteNonQuery()

B

barret bonden

(VB Exoress 20005)

I get an exception on this line : ra = myCommand.ExecuteNonQuery()

{"ExecuteNonQuery requires an open and available Connection. The
connection's current state is closed."}

in this code:

Dim myConnection As OleDbConnection

Dim myCommand As OleDbCommand

Dim ra As Integer

myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\t.mdb")

myCommand = New OleDbCommand("delete from main where last = 'freud'",
myConnection)

ra = myCommand.ExecuteNonQuery()

MessageBox.Show("Records Deleted" & ra)

myConnection.Close()





whereas this code works fine :

myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\t.mdb")

myCommand = New OleDbCommand("select * from main", myConnection)

Dim myReader As OleDb.OleDbDataReader

myConnection.Open()

myReader = myCommand.ExecuteReader

Do Until myReader.Read = False

MessageBox.Show(myReader("last"))

Loop

myReader.Close()

myConnection.Close()
 
M

Marina

The message reallhy says it all.

ExecuteNonQuery requires an open connection.
You never open your connection.

So, the solution is to open the connection before running the query.
 
B

barret bonden

Many thanks - that was it -

I can read Kant and Sartre and PHP - somehow the syntax of ADO net blinds
me - too many abstractions from the data itself - I'll keep plugging away.
 
C

Cor Ligthert [MVP]

Barret,
I can read Kant and Sartre and PHP - somehow the syntax of ADO net blinds
me - too many abstractions from the data itself - I'll keep plugging away.
Probably will that go over in a while, beside the strange keyword "Dim" is
VB very describting.

To review your code a little bit, to make it more readable (Because you
don't have the need from much conventions from older compilers or better
programmers who are full with all kind of old rules because they did it
forever that way).

I show you some little changes in your VB Net Code

Dim myConnection as New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\t.mdb")
Dim myCommand as New OleDbCommand("delete from main where last = 'freud'",
myConnection)
myConnection.Open
dim ra As Integer = myCommand.ExecuteNonQuery()
MessageBox.Show("Records Deleted: " & ra.ToString)
myConnection.Close()

Just to give an idea

(There are more reasons to do it this way, however I don't want to make it
complicated in a message)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top