Click to See Complete Forum and Search --> : OleDb Error


Broodmdh
August 26th, 2005, 01:19 PM
I am trying to incorporate an Access db into my site (as a learning exercise), and I am having a problem with my code. I accept input from the user, check it against the database, and if the input isn't already present in the db, I add it. I get the following error when I attempt to assign a new sql string to my OleDbCommand:

"System.InvalidOperationException: The OleDbCommand is currently busy Open, Fetching. at System.Data.OleDb.OleDbCommand.OnSchemaChanging() at System.Data.OleDb.OleDbCommand.set_CommandText(String value) at site2.Register.cmdSubmit_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\site2\register.aspx.vb:line 89"

My code is as follows:


Dim conn As New OleDbConnection()
Dim cmd As New OleDbCommand()
Dim dbQuery As String
Dim dbUName, dbUPass, dbUFName, dbULName, dbUEmail, dbUCountry As String
Dim connString As String
Dim dbDir As String = Server.MapPath("bin") & "\db1.mdb"

Try
dbUName = txtUname.Text
dbUPass = txtPass1.Text
dbUFName = txtFname.Text
dbULName = txtLname.Text
dbUEmail = txtEmail.Text

If txtCountry.Text Is Nothing Then
dbUCountry = ""
Else
dbUCountry = txtCountry.Text
End If

connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbDir & ";User Id=admin;Password=;"
conn.ConnectionString = connString
conn.Open()

cmd = New OleDbCommand()
cmd.Connection = conn

dbQuery = "SELECT * FROM Users WHERE txtUserName = '" & dbUName & "'"
cmd.CommandText = dbQuery

Dim dbReader As OleDbDataReader = cmd.ExecuteReader

If dbReader.Read = True Then
lblMessage.ForeColor = Color.Red
lblMessage.Text = "Username already exists!"
End If

dbQuery = "INSERT INTO Users (txtUserName,txtPassword,txtFirstName,txtLastName,txtEmail,txtCountry) VALUES (" & dbUName & "," & dbUPass & "," & dbUFName & "," & dbULName & "," & dbUEmail & "," & dbUCountry & ")"

cmd.CommandText = dbQuery
cmd.ExecuteNonQuery()

cmd.Dispose()
conn.Dispose()
dbReader.Close()

Catch err As Exception
lblMessage.Text = err.ToString
End Try


Any suggestions?

rader.net
August 28th, 2005, 07:19 AM
when you execute "cmd.ExecuteNonQuery()" you must close the datareader at first .while a command is executing you cant execute another command.
use dbReader.Close() to close the reader manually
or
use cmd.ExecuteReader(CommandBehavior.CloseConnection) to have a try.