Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Syntax error in UPDATE statement. ?

Syntax error in UPDATE statement. ?

Scheduled Pinned Locked Moved ASP.NET
databaseannouncementcsharpjavascriptsysadmin
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Farraj

    hello, i have a Datalist and ACCESS DataBase, im using the update event in datalist to update two fields in my db. first one is subj the second is text. i get the Syntax error in update statement all the time while updating. the primary key is my ID field in the db, and it was set to be the same in the datalist. this is the C# code for the update method:

    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
    Label lblsubj = (Label)e.Item.FindControl("lblsubj");
    string st = lblid.Text;
    TextBox txtBoxId = (TextBox)e.Item.FindControl("txtBoxId");
    TextBox txtBoxTitle = (TextBox)e.Item.FindControl("txtBoxTitle");
    TextBox txtBoxFile = (TextBox)e.Item.FindControl("txtBoxFile");
    TextBox txtBoxText = (TextBox)e.Item.FindControl("txtBoxText");
    string queryStr = "UPDATE news SET subj='" + txtBoxTitle.Text + "', ";
    queryStr += "text='" + txtBoxText.Text + "', ";
    queryStr += "WHERE id='" + st + "'";

         string connectionStr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/App\_Data/db.mdb");
        OleDbConnection connectObj = new OleDbConnection(connectionStr);
        OleDbCommand cmd = new OleDbCommand(queryStr, connectObj);
        connectObj.Open();
        cmd.ExecuteNonQuery();
        connectObj.Close();
        this.DataList1.EditItemIndex = -1;
        BindTheDataList();
    }
    

    And this is my ASPX page code:

    <asp:DataList ID="DataList1" runat="server" DataKeyField="subj" Height="96px"
    OnCancelCommand="DataList1_CancelCommand" OnDeleteCommand="DataList1_DeleteCommand"
    OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand"
    Style="position: static" Width="641px">
    <EditItemStyle BackColor="DarkGray" ForeColor="Black" />
    <EditItemTemplate>
    <table id="TABLE1" language="javascript" onclick="return TABLE1_onclick()">
    <tr>
    <td style="width: 90px">
    Editing item:
    <asp:Label ID="lblid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"id") %>'
    Visible="False"></asp:Label></td>

    M Offline
    M Offline
    MyDevBank
    wrote on last edited by
    #4

    WHERE id='4' is your ID column type is a VAR/NVARcher type ? MyDevBank.com

    F 1 Reply Last reply
    0
    • M MyDevBank

      WHERE id='4' is your ID column type is a VAR/NVARcher type ? MyDevBank.com

      F Offline
      F Offline
      Farraj
      wrote on last edited by
      #5

      the ID column is an Auto-number.. ? :confused:

      M 1 Reply Last reply
      0
      • F Farraj

        the ID column is an Auto-number.. ? :confused:

        M Offline
        M Offline
        MyDevBank
        wrote on last edited by
        #6

        you got WHERE ID='4' try WHERE ID=4

        F 1 Reply Last reply
        0
        • M MyDevBank

          you got WHERE ID='4' try WHERE ID=4

          F Offline
          F Offline
          Farraj
          wrote on last edited by
          #7

          still the same brother.

          M 1 Reply Last reply
          0
          • F Farraj

            hello, i have a Datalist and ACCESS DataBase, im using the update event in datalist to update two fields in my db. first one is subj the second is text. i get the Syntax error in update statement all the time while updating. the primary key is my ID field in the db, and it was set to be the same in the datalist. this is the C# code for the update method:

            protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
            {
            Label lblsubj = (Label)e.Item.FindControl("lblsubj");
            string st = lblid.Text;
            TextBox txtBoxId = (TextBox)e.Item.FindControl("txtBoxId");
            TextBox txtBoxTitle = (TextBox)e.Item.FindControl("txtBoxTitle");
            TextBox txtBoxFile = (TextBox)e.Item.FindControl("txtBoxFile");
            TextBox txtBoxText = (TextBox)e.Item.FindControl("txtBoxText");
            string queryStr = "UPDATE news SET subj='" + txtBoxTitle.Text + "', ";
            queryStr += "text='" + txtBoxText.Text + "', ";
            queryStr += "WHERE id='" + st + "'";

                 string connectionStr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/App\_Data/db.mdb");
                OleDbConnection connectObj = new OleDbConnection(connectionStr);
                OleDbCommand cmd = new OleDbCommand(queryStr, connectObj);
                connectObj.Open();
                cmd.ExecuteNonQuery();
                connectObj.Close();
                this.DataList1.EditItemIndex = -1;
                BindTheDataList();
            }
            

            And this is my ASPX page code:

            <asp:DataList ID="DataList1" runat="server" DataKeyField="subj" Height="96px"
            OnCancelCommand="DataList1_CancelCommand" OnDeleteCommand="DataList1_DeleteCommand"
            OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand"
            Style="position: static" Width="641px">
            <EditItemStyle BackColor="DarkGray" ForeColor="Black" />
            <EditItemTemplate>
            <table id="TABLE1" language="javascript" onclick="return TABLE1_onclick()">
            <tr>
            <td style="width: 90px">
            Editing item:
            <asp:Label ID="lblid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"id") %>'
            Visible="False"></asp:Label></td>

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #8

            This should do the trick;

            UPDATE news
            SET subj='aasda22'
            ,[text]='sdasd'
            WHERE id='4'

            "Text" should be avoided as a column-name; it's a reserved word.

            I are Troll :suss:

            F M 2 Replies Last reply
            0
            • F Farraj

              still the same brother.

              M Offline
              M Offline
              MyDevBank
              wrote on last edited by
              #9

              one of the fields name is wrong, Cap letter or something like that MyDevBank.com

              1 Reply Last reply
              0
              • L Lost User

                This should do the trick;

                UPDATE news
                SET subj='aasda22'
                ,[text]='sdasd'
                WHERE id='4'

                "Text" should be avoided as a column-name; it's a reserved word.

                I are Troll :suss:

                F Offline
                F Offline
                Farraj
                wrote on last edited by
                #10

                mydevbank thank you :) Eddy Vluggen!! thanks a lot brother, it worked. i never though of that lol!! TEXT is a reserved word. wth !! ive been dealing with this thing for a week and never though of changing columns names thanks anyway for all :)

                L M 2 Replies Last reply
                0
                • F Farraj

                  mydevbank thank you :) Eddy Vluggen!! thanks a lot brother, it worked. i never though of that lol!! TEXT is a reserved word. wth !! ive been dealing with this thing for a week and never though of changing columns names thanks anyway for all :)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #11

                  You're welcome :)

                  1 Reply Last reply
                  0
                  • F Farraj

                    mydevbank thank you :) Eddy Vluggen!! thanks a lot brother, it worked. i never though of that lol!! TEXT is a reserved word. wth !! ive been dealing with this thing for a week and never though of changing columns names thanks anyway for all :)

                    M Offline
                    M Offline
                    MyDevBank
                    wrote on last edited by
                    #12

                    :)

                    1 Reply Last reply
                    0
                    • L Lost User

                      This should do the trick;

                      UPDATE news
                      SET subj='aasda22'
                      ,[text]='sdasd'
                      WHERE id='4'

                      "Text" should be avoided as a column-name; it's a reserved word.

                      I are Troll :suss:

                      M Offline
                      M Offline
                      Muhammad Gouda
                      wrote on last edited by
                      #13

                      Deserve MVP :thumbsup:

                      foreach(Minute m in MyLife) myExperience++;

                      L 1 Reply Last reply
                      0
                      • M Muhammad Gouda

                        Deserve MVP :thumbsup:

                        foreach(Minute m in MyLife) myExperience++;

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #14

                        Thanks :-O

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups