1

I have 3 text boxes I am using as a contact form. I am trying to use VB to take this data and add it into my database. I have run debugging and it says the error is in the INSERT INTO string. ![Screen Grab of Debugging] [1]: https://i.sstatic.net/ufYPs.png

Any ideas?

Imports System

Imports System.Data Imports System.Data.OleDb Partial Class Contact Inherits System.Web.UI.Page

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Name As String = txtName.Text
    Dim Email As String = tbxEmail.Text
    Dim Comment As String = tbxComment.Text

    Dim objConnection As OleDbConnection = Nothing
    Dim objcmd As OleDbCommand = Nothing

    Dim strSql As String
    Dim dbConn As OleDbConnection = Nothing

    dbConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = Z:\Documents\Databases\user.accdb")
    dbConn.Open()

    strSql = "INSERT INTO user (username, email, comments) VALUES (?,?,?)"
    objcmd = New OleDbCommand(strSql, dbConn)
    objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@username", Name))
    objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@email", Email))
    objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@comments", Comment))
    objcmd.ExecuteNonQuery()


    dbConn.Close()
    Response.Write("Submitted Successfully")

End Sub

1 Answer 1

1

USER is a reserved keyword in MS-Access, To use it as a table name your need to enclose it in square brackets

strSql = "INSERT INTO [user] (username, email, comments) VALUES (?,?,?)"

As a side note, try to use the Using Statement when you work with disposable objects.

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Name As String = txtName.Text
    Dim Email As String = tbxEmail.Text
    Dim Comment As String = tbxComment.Text

    Using dbConn = New OleDbConnection(".......")
        dbConn.Open()
        Dim strSql = "INSERT INTO [user] (username, email, comments) VALUES (?,?,?)"
        Using objcmd = New OleDbCommand(strSql, dbConn)
            objcmd.Parameters.AddWithValue("@username", Name)
            objcmd.Parameters.AddWithValue("@email", Email)
            objcmd.Parameters.AddWithValue("@comments", Comment)
            objcmd.ExecuteNonQuery()
        End Using
    End Using
    Response.Write("Submitted Successfully")
End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reply, I have tried 2 things. If I add the Using statement I end up with build errors, stating both dbconn and objcmd hide a variable in and enclosing block.
I have tried running without the 'Using statement', just adding square brackets to user [user]. While debugging the following error is thrown Unhandled exception at line 940, column 13 in localhost:1169/… 0x800a139e - JavaScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.
Rewritten all the answer code, all the variables for connection and command are implicitly created inside the using statement block and destroyed at the End Using, so you don't need to declare and initialize them before the block. The second error doesn't seem to be related to this question
Thank you very much for your help. I realise now I hand;t amended my code to reflect the variables being declared inside the USING statement.
I should also add the javascript runtime error was caused by the update panel containing my form. I think the error was being thrown as the response.write was being loaded outside of the panel as I failed to add a placeholder

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.