0

Currently doing a project and I am having a serious problem with some code. I keep getting the cmd.ExecuteNonQuery() and i have searched all over the internet to find the solution. I have inserted [] in certain catagories and still produces the same error.

All I am doing is creating a page where people can input data for certain catagories, in which once added, it will be updated to the access database. I had this working on an earlier version but ever since i added more characters, it has come with this error.

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="addcar.aspx.vb" Inherits="WebApplication1.addcar" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
       <p>
        <br />
        CarName<asp:TextBox ID="tb_CarName" runat="server"></asp:TextBox>
    </p>
      <p>
        <br />
        Year<asp:TextBox ID="tb_Year" runat="server"></asp:TextBox>
    </p>
      <p>
        <br />
        EngineSize<asp:TextBox ID="tb_EngineSize" runat="server"></asp:TextBox>
    </p>
      <p>
        <br />
        BHP<asp:TextBox ID="tb_BHP" runat="server"></asp:TextBox>
    </p>
      <p>
        <br />
        Origin<asp:TextBox ID="tb_Origin" runat="server"></asp:TextBox>
    </p>
      <p>
        <br />
        Description<asp:TextBox ID="tb_Description" runat="server"></asp:TextBox>
    </p>
       <p>
        <br />
        Pictures<asp:TextBox ID="tb_Pictures" runat="server"></asp:TextBox>
    </p>
         <p>
             <asp:Button ID="Button" runat="server" Text="Add Car Details" />
    </p>
   </asp:Content>

Followed by the button code which is:

Imports System.Data.OleDb

Public Class addcar
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Protected Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("CarsConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into Table1(CarName,Year,EngineSize,BHP,Origin,Description,Pictures) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_CarName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_Year.Text)
        cmd.Parameters.AddWithValue("@f3", tb_EngineSize.Text)
        cmd.Parameters.AddWithValue("@f4", tb_BHP.Text)
        cmd.Parameters.AddWithValue("@f5", tb_Origin.Text)
        cmd.Parameters.AddWithValue("@f6", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f7", tb_Pictures.Text)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("confirmation.aspx")


    End Sub

End Class

Help? What am I doing wrong?

Error is as follows:

Syntax error in INSERT INTO statement.

7
  • 1
    The markup (.aspx) is not needed here. What is needed is the error message. And perhaps examples of the categories that cause the issue and those that do not. The database definition of Table1 can also help us help you. Commented Nov 17, 2013 at 17:18
  • Hi,Thanks for your reply. Error is as follows. When I revert back to my old settings it still comes with the same error now :( Commented Nov 17, 2013 at 17:22
  • Error is as follows i40.tinypic.com/5lozm1.png Commented Nov 17, 2013 at 17:23
  • I suggest you check that INSERT statement then. The error says there is a syntax error there. Try running the same in SSMS (SQL Server Management Studio). Also - you have the text of the error don't post an image of it. Commented Nov 17, 2013 at 17:24
  • Hi, Text version of the picture is here pastebin.com/ywKHHmUZ. I am a complete newbie to all this asp.net (uni project) and cant get my head around this error. Commented Nov 17, 2013 at 17:29

1 Answer 1

2

Year is a reserved keyword in access / Jet. You need to encapsulate it with square brackets.

Dim SqlString As String = "Insert into Table1(CarName, [Year],EngineSize,BHP,Origin,Description,Pictures) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"

You can check every reserved keyword in the jet engine here: http://support.microsoft.com/kb/248738

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

1 Comment

Then please be so kind and mark this as an answer by clicking on the checked symbol left to my answer

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.