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. Database & SysAdmin
  3. Database
  4. Update Query Cant seem to spot the problem

Update Query Cant seem to spot the problem

Scheduled Pinned Locked Moved Database
databasedebuggingsaleshelpannouncement
7 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.
  • E Offline
    E Offline
    ekynox
    wrote on last edited by
    #1

    Hi there, Well it seems I have stumbled upon a rather basic problem and I just cant seem to work it out. I have written a simple update query as follows: private readonly string ACCESS_CUSTOMER_UPDATE = "UPDATE Customer SET Customer.Name = @Name,Customer.Age = @Age WHERE Customer.ID = @ID"; I have used the approach to execute the query in the method listed below. For the life of me I cant seem to work out why the update wont be taking place. I have executed the query listed above in Access without any problems but when encapsulated in the method below it just wont update. I'd appreciate if someone could point out the mistake I have overlooked. Thanks private string PARAM_ID = "@ID"; private string PARAM_NAME = "@Name"; private string PARAM_AGE = "@Age"; private string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Temp\\temp.mdb"; private OleDbConnection connection = new OleDbConnection(connectionstring); public void UpdateCustomer(CustomerInfo customer) { OleDbParameter[] parameters = new OleDbParameter[]{ new OleDbParameter(PARAM_ID,OleDbType.Integer), new OleDbParameter(PARAM_NAME,OleDbType.VarChar), new OleDbParameter(PARAM_AGE,OleDbType.Integer)}; parameters[0].Value = customer.ID; parameters[1].Value = customer.Name; parameters[2].Value = customer.Age; OleDbCommand command = new OleDbCommand(); command.Connection = this.connection; command.Connection.Open(); command.CommandText = "UPDATE Customer SET Customer.Name = @Name,Customer.Age = @Age WHERE Customer.ID = @ID"; for (int i = 0; i < parameters.Length; i++) { OleDbParameter parm = (OleDbParameter)parameters[i]; command.Parameters.Add(parm); } try { int x = command.ExecuteNonQuery(); Debug.WriteLine(x.ToString()); } catch (Exception e) { throw new Exception(e.Message, e.InnerException); } finally { command.Connection.Close(); command.Dispose(); } } edit: Added the connection string.

    S P R 3 Replies Last reply
    0
    • E ekynox

      Hi there, Well it seems I have stumbled upon a rather basic problem and I just cant seem to work it out. I have written a simple update query as follows: private readonly string ACCESS_CUSTOMER_UPDATE = "UPDATE Customer SET Customer.Name = @Name,Customer.Age = @Age WHERE Customer.ID = @ID"; I have used the approach to execute the query in the method listed below. For the life of me I cant seem to work out why the update wont be taking place. I have executed the query listed above in Access without any problems but when encapsulated in the method below it just wont update. I'd appreciate if someone could point out the mistake I have overlooked. Thanks private string PARAM_ID = "@ID"; private string PARAM_NAME = "@Name"; private string PARAM_AGE = "@Age"; private string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Temp\\temp.mdb"; private OleDbConnection connection = new OleDbConnection(connectionstring); public void UpdateCustomer(CustomerInfo customer) { OleDbParameter[] parameters = new OleDbParameter[]{ new OleDbParameter(PARAM_ID,OleDbType.Integer), new OleDbParameter(PARAM_NAME,OleDbType.VarChar), new OleDbParameter(PARAM_AGE,OleDbType.Integer)}; parameters[0].Value = customer.ID; parameters[1].Value = customer.Name; parameters[2].Value = customer.Age; OleDbCommand command = new OleDbCommand(); command.Connection = this.connection; command.Connection.Open(); command.CommandText = "UPDATE Customer SET Customer.Name = @Name,Customer.Age = @Age WHERE Customer.ID = @ID"; for (int i = 0; i < parameters.Length; i++) { OleDbParameter parm = (OleDbParameter)parameters[i]; command.Parameters.Add(parm); } try { int x = command.ExecuteNonQuery(); Debug.WriteLine(x.ToString()); } catch (Exception e) { throw new Exception(e.Message, e.InnerException); } finally { command.Connection.Close(); command.Dispose(); } } edit: Added the connection string.

      S Offline
      S Offline
      squattyarun
      wrote on last edited by
      #2

      Can you also write the error which you are getting?

      Arun Singh Noida.

      1 Reply Last reply
      0
      • E ekynox

        Hi there, Well it seems I have stumbled upon a rather basic problem and I just cant seem to work it out. I have written a simple update query as follows: private readonly string ACCESS_CUSTOMER_UPDATE = "UPDATE Customer SET Customer.Name = @Name,Customer.Age = @Age WHERE Customer.ID = @ID"; I have used the approach to execute the query in the method listed below. For the life of me I cant seem to work out why the update wont be taking place. I have executed the query listed above in Access without any problems but when encapsulated in the method below it just wont update. I'd appreciate if someone could point out the mistake I have overlooked. Thanks private string PARAM_ID = "@ID"; private string PARAM_NAME = "@Name"; private string PARAM_AGE = "@Age"; private string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Temp\\temp.mdb"; private OleDbConnection connection = new OleDbConnection(connectionstring); public void UpdateCustomer(CustomerInfo customer) { OleDbParameter[] parameters = new OleDbParameter[]{ new OleDbParameter(PARAM_ID,OleDbType.Integer), new OleDbParameter(PARAM_NAME,OleDbType.VarChar), new OleDbParameter(PARAM_AGE,OleDbType.Integer)}; parameters[0].Value = customer.ID; parameters[1].Value = customer.Name; parameters[2].Value = customer.Age; OleDbCommand command = new OleDbCommand(); command.Connection = this.connection; command.Connection.Open(); command.CommandText = "UPDATE Customer SET Customer.Name = @Name,Customer.Age = @Age WHERE Customer.ID = @ID"; for (int i = 0; i < parameters.Length; i++) { OleDbParameter parm = (OleDbParameter)parameters[i]; command.Parameters.Add(parm); } try { int x = command.ExecuteNonQuery(); Debug.WriteLine(x.ToString()); } catch (Exception e) { throw new Exception(e.Message, e.InnerException); } finally { command.Connection.Close(); command.Dispose(); } } edit: Added the connection string.

        P Offline
        P Offline
        Paddy Boyd
        wrote on last edited by
        #3

        I think that you're going to find that the order of the parameters going into your oledbcommand for access is important...

        E 1 Reply Last reply
        0
        • P Paddy Boyd

          I think that you're going to find that the order of the parameters going into your oledbcommand for access is important...

          E Offline
          E Offline
          ekynox
          wrote on last edited by
          #4

          I am getting no error. The output of ExecuteNonQuery is an "int" type. The value of this int variable indicates how many rows have been modified. In my case its zero when infact it should be one. I have one row of data in the table that needs to be updated so it should return 1 not 0. I can verify that order of parameters matches what is in the table. Any other suggestions I can try out ? Edit: Running the code like this works fine: public void UpdateCustomer(CustomerInfo customer) { string query = "UPDATE CUSTOMER SET Name = " + "'" + customer.Name + "'," + "Age = " + customer.Age + " WHERE ID = " + customer.ID; OleDbCommand command = new OleDbCommand(); command.CommandText = query; command.Connection = this.connection; command.Connection.Open(); try { int x = command.ExecuteNonQuery(); Debug.WriteLine(x.ToString()); } catch (Exception ex) { throw new Exception(ex.Message, ex.InnerException); } finally { command.Connection.Close(); command.Dispose(); } }

          P 1 Reply Last reply
          0
          • E ekynox

            I am getting no error. The output of ExecuteNonQuery is an "int" type. The value of this int variable indicates how many rows have been modified. In my case its zero when infact it should be one. I have one row of data in the table that needs to be updated so it should return 1 not 0. I can verify that order of parameters matches what is in the table. Any other suggestions I can try out ? Edit: Running the code like this works fine: public void UpdateCustomer(CustomerInfo customer) { string query = "UPDATE CUSTOMER SET Name = " + "'" + customer.Name + "'," + "Age = " + customer.Age + " WHERE ID = " + customer.ID; OleDbCommand command = new OleDbCommand(); command.CommandText = query; command.Connection = this.connection; command.Connection.Open(); try { int x = command.ExecuteNonQuery(); Debug.WriteLine(x.ToString()); } catch (Exception ex) { throw new Exception(ex.Message, ex.InnerException); } finally { command.Connection.Close(); command.Dispose(); } }

            P Offline
            P Offline
            Paddy Boyd
            wrote on last edited by
            #5

            UPDATE Customer SET Customer.Name = @Name,Customer.Age = @Age WHERE Customer.ID = @ID

            private string PARAM_ID = "@ID";
            private string PARAM_NAME = "@Name";
            private string PARAM_AGE = "@Age";

            What i think you'll find is happening here is that the sql statement is being executed and is trying to set name = id, age = name where id = age. Awkward problem.

            1 Reply Last reply
            0
            • E ekynox

              Hi there, Well it seems I have stumbled upon a rather basic problem and I just cant seem to work it out. I have written a simple update query as follows: private readonly string ACCESS_CUSTOMER_UPDATE = "UPDATE Customer SET Customer.Name = @Name,Customer.Age = @Age WHERE Customer.ID = @ID"; I have used the approach to execute the query in the method listed below. For the life of me I cant seem to work out why the update wont be taking place. I have executed the query listed above in Access without any problems but when encapsulated in the method below it just wont update. I'd appreciate if someone could point out the mistake I have overlooked. Thanks private string PARAM_ID = "@ID"; private string PARAM_NAME = "@Name"; private string PARAM_AGE = "@Age"; private string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Temp\\temp.mdb"; private OleDbConnection connection = new OleDbConnection(connectionstring); public void UpdateCustomer(CustomerInfo customer) { OleDbParameter[] parameters = new OleDbParameter[]{ new OleDbParameter(PARAM_ID,OleDbType.Integer), new OleDbParameter(PARAM_NAME,OleDbType.VarChar), new OleDbParameter(PARAM_AGE,OleDbType.Integer)}; parameters[0].Value = customer.ID; parameters[1].Value = customer.Name; parameters[2].Value = customer.Age; OleDbCommand command = new OleDbCommand(); command.Connection = this.connection; command.Connection.Open(); command.CommandText = "UPDATE Customer SET Customer.Name = @Name,Customer.Age = @Age WHERE Customer.ID = @ID"; for (int i = 0; i < parameters.Length; i++) { OleDbParameter parm = (OleDbParameter)parameters[i]; command.Parameters.Add(parm); } try { int x = command.ExecuteNonQuery(); Debug.WriteLine(x.ToString()); } catch (Exception e) { throw new Exception(e.Message, e.InnerException); } finally { command.Connection.Close(); command.Dispose(); } } edit: Added the connection string.

              R Offline
              R Offline
              Rob Graham
              wrote on last edited by
              #6

              Your problem is with the order of your parameters. System.Data.Oledb does not work like sqlclient (or Access internally) The parameter names are irrelevant, only order matters. change your parameters to: parameters[0].Value = customer.Name; parameters[1].Value = customer.Age; parameters[2].Value = customer.ID; So that they match the order of occurrence in the query.

              E 1 Reply Last reply
              0
              • R Rob Graham

                Your problem is with the order of your parameters. System.Data.Oledb does not work like sqlclient (or Access internally) The parameter names are irrelevant, only order matters. change your parameters to: parameters[0].Value = customer.Name; parameters[1].Value = customer.Age; parameters[2].Value = customer.ID; So that they match the order of occurrence in the query.

                E Offline
                E Offline
                ekynox
                wrote on last edited by
                #7

                Thanks for the tip, I'll have to makesure I am more observant next time :-)

                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