1

Using VB.NET with ASP.NET and and ms-access 2003 data, I'm trying to input data from a web form to the a table in db.mdb called 'USER'.

I tried this code:

 Protected Sub btnCreateAccount_Click(sender As Object, e As System.EventArgs) Handles btnCreateAccount.Click

    Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\WebSite3\db.mdb;User Id=admin;Password=;")

    Dim cmd As OleDbCommand = New OleDbCommand("INSERT INTO USER (Name, Surname, Username, Country, TelNo, Password, Address) VALUES (?, ?, ?, ?, ?, ?, ?)", conn)

    If txtPass.Text = txtCPass.Text Then

        cmd.Parameters.Add("@Name", OleDbType.VarChar, 255).Value = txtName.Text
        cmd.Parameters.Add("@Surame", OleDbType.VarChar, 255).Value = txtSurname.Text
        cmd.Parameters.Add("@Address", OleDbType.VarChar, 255).Value = txtAddress.Text
        cmd.Parameters.Add("@Country", OleDbType.VarChar, 255).Value = txtCountry.Text
        cmd.Parameters.Add("@Username", OleDbType.VarChar, 255).Value = txtUsername.Text
        cmd.Parameters.Add("@Password", OleDbType.VarChar, 255).Value = txtPass.Text
        cmd.Parameters.Add("@TelNo", OleDbType.Integer).Value = txtTelNo.Text

        Try

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()

        Catch ex As OdbcException
            Throw ex
        Finally
            conn.Close()
            lblAccount.Visible = True


        End Try

    End If

End Sub

But it's returning me an error message:

enter image description here

Any suggestions to why the problem might be cause?

1
  • Strongly suggest you get rid of the Catch clause. It messes up your stack trace to make it look like the Throw Ex line is the source of the exception. Commented Mar 24, 2012 at 15:01

1 Answer 1

3

User and Password are reserved keywords. Change the names, or use square brackets around the names in the query:

    Dim cmd As OleDbCommand = New OleDbCommand("INSERT INTO [USER] (Name, Surname, Username, Country, TelNo, [Password], Address) VALUES (?, ?, ?, ?, ?, ?, ?)", conn)

Then, for then next error that you will encounter: As the parameters are not named in the query, the parameter objects in the Parameters collection has to be added in the same order as they are used in the query.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks !.. also just one more thing I'm getting an error on {"Data type mismatch in criteria expression."} on the cmd.ExecuteNonQuery() What might be causing this ?
@Brian: Have you checked that the parameters is in the correct order? Have you checked that the parameter types correspond to the database types? Have you verified that the phone number only contains digits, so that it's possible to convert to an integer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.