0

I am trying to take data from a web form and place it in an ms-access database. The code which is in the aspx.vb page (to date) is listed below but when I run it the cmd.ExecuteNonQuery() throws up

"No value given for one or more required parameters."

Why is this? What should the code read?

  Protected Sub RegButton_Click(sender As Object, e As EventArgs) Handles RegButton.Click
    Dim con As New OleDb.OleDbConnection
    Dim cmd As New OleDb.OleDbCommand
    Dim Sql As String

    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Users.accdb;Persist Security Info=True"

    con.Open()

    Sql = "INSERT INTO Users.Personnel (FirstName, LastName, Address, Email, Username, UserPassword) VALUES (@first, @last, @addr, @email, @uname, @pwd) "
    cmd = New OleDb.OleDbCommand(Sql, con)
    cmd.ExecuteNonQuery()
    MsgBox("saved")

    cmd.Parameters.AddWithValue("@first", FirstBox.Text)
    cmd.Parameters.AddWithValue("@last", LastBox.Text)
    cmd.Parameters.AddWithValue("@addr", AddressBox.Text)
    cmd.Parameters.AddWithValue("@email", EmailBox.Text)
    cmd.Parameters.AddWithValue("@uname", UserBox.Text)
    cmd.Parameters.AddWithValue("@pwd", PwdBox.Text)

    con.Close()

End Sub

1 Answer 1

1

you can not execute query before passing parameters try this.

con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Users.accdb;Persist Security Info=True"

con.Open()

Sql = "INSERT INTO Users.Personnel (FirstName, LastName, Address, Email, Username, UserPassword) VALUES (@first, @last, @addr, @email, @uname, @pwd) "
cmd = New OleDb.OleDbCommand(Sql, con)


cmd.Parameters.AddWithValue("@first", FirstBox.Text)
cmd.Parameters.AddWithValue("@last", LastBox.Text)
cmd.Parameters.AddWithValue("@addr", AddressBox.Text)
cmd.Parameters.AddWithValue("@email", EmailBox.Text)
cmd.Parameters.AddWithValue("@uname", UserBox.Text)
cmd.Parameters.AddWithValue("@pwd", PwdBox.Text)
cmd.ExecuteNonQuery()
MsgBox("saved")
con.Close()

End Sub

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

1 Comment

Thanks Amit. A simple solution which makes me feel simple for not spotting it!!

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.