views:

110

answers:

1

I'm kind of new to ASP.NET and using OLEDB. I'm trying to add a row to a table in an ACCESS database. The code first reads how many rows a user already has and if they're not over the limit, it allows them to submit a new row. I've set up a dataset and that seems to be fine. The problem is when I try to update the new row back into the database the Insert command makes a blank command, which fails the requirements in Access-Database. I'm kind of lost at what I'm doing wrong.

If Page.IsValid = True Then
        objConnection = New OleDbConnection(data_vars.dbConnString())
        objConnection.Open()

        strSQL = "SELECT * FROM tickets WHERE Created_by = " + CStr(Session("ID")) + " AND Status = 0"

        objAdapter = New OleDbDataAdapter(strSQL, objConnection)
        objAdapter.Fill(objDataSet, "tickets")

        If objDataSet.Tables("tickets").Rows.Count < CInt(ConfigurationManager.AppSettings("max_new_tickets")) And CInt(Session("access")) <= 1 Then
            'things are good so make ticket

            objTable = objDataSet.Tables("tickets")

            objRow = objTable.NewRow()

            objRow("name") = Server.HtmlEncode(txtTName.Text)
            objRow("description") = Replace(Server.HtmlEncode(txtTDesc.Text), vbCrLf, "<br />")
            objRow("contact_pref") = Replace(Server.HtmlEncode(txtContactPref.Text), vbCrLf, "<br />")
            objRow("log") = "New ticket created on " + CStr(Now()) + " from " + Server.HtmlEncode(Request.ServerVariables("REMOTE_ADDR")) + " with " + Server.HtmlEncode(Request.ServerVariables("HTTP_USER_AGENT")) + "<br />"
            objTable.Rows.Add(objRow)

            'generate the update commands
            Dim objBuilder As New OleDbCommandBuilder(objAdapter)

            objAdapter.UpdateCommand = objBuilder.GetUpdateCommand()
            objAdapter.InsertCommand = objBuilder.GetInsertCommand()
            objAdapter.DeleteCommand = objBuilder.GetDeleteCommand()

            'debug ---
            lblError.Text = lblError.Text + "<br/>" + objBuilder.GetInsertCommand.CommandText

            dgDebug.DataSource = objTable.DefaultView
            dgDebug.DataBind()
            '----

            'update the database
            objAdapter.Update(objDataSet, "tickets")
        Else
            'customer (access level) exceeded new ticket max
            lblError.Text = "<b>Cannot create ticket.</b><br /> You currently have the maximum amount of newly opened tickets allowed. Please wait until a tech reviews some of your tickets before making new ones."
        End If

        objConnection.Close()
    End If

The error I'm getting is:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.

Source Error: Line 58: objAdapter.Update(objDataSet, "tickets")

A: 

After struggling with my code for 2 hours, I found this error was caused by Access Database keywords in the column names.

http://support.microsoft.com/kb/286335

Renamed the column names and it worked fine.

Usually bracketing the object names allows Jet/ACE to manage to deal with it (Access is not involved here at all).
David-W-Fenton