Exception when try to INSERT into Access-DB

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jazper

    #1

    Exception when try to INSERT into Access-DB

    Hi i got a big problem and don't know how to solve it...

    i have an access DB and try to insert a new record!
    Fild1: ID - Numeric - PrimaryKey
    Fild2: User - Text

    //---My ConnectionStrin g----------------------------------
    Application["OleConStr"]
    = "Provider=Micro soft.Jet.OLEDB. 4.0;Mode=Share Deny None;"
    + @"Data Source=C:\db\ja z.mdb";

    //---My Code for Select----------------------------------
    //---(works great)---------------------------------------

    OleDbCommand cmdS = new OleDbCommand("S elect *
    from jaz_test", this.con);
    OleDbDataReader rd = cmdS.ExecuteRea der();
    while( rd.Read() )
    Response.Write( rd["ID"].ToString() + "-" +
    rd["User"].ToString());
    rd.Close();
    cmdS.Dispose();

    //RESULT = 1-Webmaster
    //RESULT = 2-Webmaster II

    //---My Code for Insert (Exception raises)--------------
    OleDbCommand cmdI = new OleDbCommand("I NSERT INTO
    jaz_test(ID, User) VALUES(3, 'UserX')", this.con);
    cmdI.ExecuteNon Query();
    cmdI.Dispose();


    //---My Problem!----------------------------------
    cmdI.ExecuteNon Query() throws Exception "Syntax error in
    INSERT INTO statement."

    WHERE is the Error?. SQL-Insert statement must be OK! when
    i execute it in Access itself, it works great! When i
    execute it by VB6 it works great! When i execute it in C++
    it works great! BUT NOT IN DOTNET with OleProvider!

    What is the problem...?

    Thanx for every hint!
    Jazper
  • Tobin Harris

    #2
    Re: Exception when try to INSERT into Access-DB

    "Jazper" <j.a.z@bluewin. ch> wrote in message
    news:036e01c379 15$77f93240$a00 1280a@phx.gbl.. .[color=blue]
    > Hi i got a big problem and don't know how to solve it...
    > //---My Code for Insert (Exception raises)--------------
    > OleDbCommand cmdI = new OleDbCommand("I NSERT INTO
    > jaz_test(ID, User) VALUES(3, 'UserX')", this.con);[/color]

    I'm guessing a little here, but are you sure you can insert into an
    Autonumber column? Does this work?

    INSERT INTO jaz_test(User) VALUES('UserX')

    then try a

    SELECT @@IDENTITY

    to get the newly created autonum?

    HTH

    Tobin Harris


    Comment

    • Jazper

      #3
      Exception when try to INSERT into Access-DB

      I think i found the error
      User is a Access Reserved Word. When i do the sql like
      this:
      INSERT INTO jaz_test([ID], [User]) VALUES(3, 'UserX')"
      then it works...

      but why? why can execute the query-editor of access the
      query and Dotnet not???

      is there a list of reserved words like "User"?

      Regards, Jazper

      Comment

      • Jazper

        #4
        Re: Exception when try to INSERT into Access-DB

        Hi Tobin

        No it did not work. Also with Autocolumn. But i think i
        found the error..

        User is a Access Reserved Word. When i do the sql like
        this:
        INSERT INTO jaz_test([ID], [User]) VALUES(3, 'UserX')"
        then it works...

        but why? why can execute the query-editor of access the
        query and Dotnet not???

        is there a list of reserved words like "User"?

        Regards, Jazper




        [color=blue]
        >-----Original Message-----
        >"Jazper" <j.a.z@bluewin. ch> wrote in message
        >news:036e01c37 915$77f93240$a0 01280a@phx.gbl. ..[color=green]
        >> Hi i got a big problem and don't know how to solve it...
        >> //---My Code for Insert (Exception raises)--------------
        >> OleDbCommand cmdI = new OleDbCommand("I NSERT INTO
        >> jaz_test(ID, User) VALUES(3, 'UserX')", this.con);[/color]
        >
        >I'm guessing a little here, but are you sure you can[/color]
        insert into an[color=blue]
        >Autonumber column? Does this work?
        >
        >INSERT INTO jaz_test(User) VALUES('UserX')
        >
        >then try a
        >
        >SELECT @@IDENTITY
        >
        >to get the newly created autonum?
        >
        >HTH
        >
        >Tobin Harris
        >
        >
        >.
        >[/color]

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Exception when try to INSERT into Access-DB

          Jazper,

          Apparently one of the words "ID" or "User" is reserved, and you need to
          use the bracket notation to escape it so that it can be interpreted as a
          column name and not a reserved word.

          Hope this helps.

          --
          - Nicholas Paldino [.NET/C# MVP]
          - nicholas.paldin o@exisconsultin g.com

          "Jazper" <j.a.z@bluewin. ch> wrote in message
          news:0c9901c379 1c$d8b658e0$a10 1280a@phx.gbl.. .[color=blue]
          > Hi Tobin
          >
          > No it did not work. Also with Autocolumn. But i think i
          > found the error..
          >
          > User is a Access Reserved Word. When i do the sql like
          > this:
          > INSERT INTO jaz_test([ID], [User]) VALUES(3, 'UserX')"
          > then it works...
          >
          > but why? why can execute the query-editor of access the
          > query and Dotnet not???
          >
          > is there a list of reserved words like "User"?
          >
          > Regards, Jazper
          >
          >
          >
          >
          >[color=green]
          > >-----Original Message-----
          > >"Jazper" <j.a.z@bluewin. ch> wrote in message
          > >news:036e01c37 915$77f93240$a0 01280a@phx.gbl. ..[color=darkred]
          > >> Hi i got a big problem and don't know how to solve it...
          > >> //---My Code for Insert (Exception raises)--------------
          > >> OleDbCommand cmdI = new OleDbCommand("I NSERT INTO
          > >> jaz_test(ID, User) VALUES(3, 'UserX')", this.con);[/color]
          > >
          > >I'm guessing a little here, but are you sure you can[/color]
          > insert into an[color=green]
          > >Autonumber column? Does this work?
          > >
          > >INSERT INTO jaz_test(User) VALUES('UserX')
          > >
          > >then try a
          > >
          > >SELECT @@IDENTITY
          > >
          > >to get the newly created autonum?
          > >
          > >HTH
          > >
          > >Tobin Harris
          > >
          > >
          > >.
          > >[/color][/color]


          Comment

          • Dr.Bob

            #6
            Re: Exception when try to INSERT into Access-DB

            Once I had this problem. I finally determined that the reserved word was
            not in Access, but in the SQL. "User" is reserved in SQL - So, the Access
            SQL editor maybe is not fully compliant, or the OLEDB pass through chokes.
            One place to find the SQL reserved words is:
            http://www.jsoftware.com/books/help/...rved_words.htm

            UnDo my email.


            "Jazper" <j.a.z@bluewin. ch> wrote in message
            news:0c8a01c379 1b$6a360970$a10 1280a@phx.gbl.. .[color=blue]
            > I think i found the error
            > User is a Access Reserved Word. When i do the sql like
            > this:
            > INSERT INTO jaz_test([ID], [User]) VALUES(3, 'UserX')"
            > then it works...
            >
            > but why? why can execute the query-editor of access the
            > query and Dotnet not???
            >
            > is there a list of reserved words like "User"?
            >
            > Regards, Jazper
            >[/color]


            Comment

            Working...