Hi,

I am getting the error (End Of Statement Expected) for the following line:

Please guide me on this issue.

Error Line:

Dim MySQL As String = "INSERT INTO Contact Us (Name, Company) VALUES ('" & TextBox1.Text & "')" "('" & TextBox4.Text & "')"

Entire Code:

<script runat="server">
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & Server.MapPath("sociomarker.mdb") & ";"

        Dim MySQL As String = "INSERT INTO Contact Us (Name, Company) VALUES ('" & TextBox1.Text & "')" "('" & TextBox4.Text & "')"
       Dim MyConn As New OleDbConnection(strConn)
       Dim cmd As New OleDbCommand(MySQL, MyConn)
       MyConn.Open()
       cmd.ExecuteNonQuery()
       MyConn.Close()

        Response.Redirect("index.html")
    End Sub
    </script>

Thanks and Regards,

utpal

Dani AI

Generated

The immediate compiler error "End Of Statement Expected" is caused by malformed string literals in the SQL assignment (mismatched quotes and a missing concatenation operator), and the Access identifiers need brackets when they contain spaces or could be reserved words. As correctly points out, wrap names like Contact Us (and field names) in square brackets and fix the string concatenation. Beyond that, building SQL by concatenating TextBox values is fragile and unsafe.

A safer, clearer approach is to use a parameterized OleDbCommand and Using blocks so connections and commands are always disposed. Example (VB.NET / .NET 3.5):

Using conn As New OleDbConnection(strConn)
    conn.Open()
    Using cmd As New OleDbCommand("INSERT INTO [Contact Us] ([Name], [Company]) VALUES (?, ?)", conn)
        cmd.Parameters.AddWithValue("p1", TextBox1.Text)
        cmd.Parameters.AddWithValue("p2", TextBox4.Text)
        cmd.ExecuteNonQuery()
    End Using
End Using

Notes and troubleshooting tips:

  • Bracket any table/column with spaces or ambiguous names: [Contact Us], [Name], [Company].
  • OleDb uses positional parameters (the ? placeholders); parameter order matters.
  • Prefer explicit OleDbParameter types for production (AddWithValue can infer incorrect types).
  • Use Try/Catch to log the exact exception text; that helps diagnose permission, provider, or SQL-syntax issues.
  • On 64-bit hosts, Jet OLEDB may not be available; use the ACE provider or run the app pool in 32-bit mode.

For : validating control IDs (TextBox1/TextBox4), confirming the connection string, and switching to parameterized commands will resolve the compile error and avoid SQL injection or quoting problems.

Dim MySQL As String = "INSERT INTO [Contact Us] ([Name], [Company]) VALUES ('" & TextBox1.Text & "','" & TextBox4.Text & "')"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.