sql - C# OleDB Update statement not updating -


i'm trying update access database through c# web application i'm facing problem in statement not updating records , not returning errors.

i'm trying update text field , condition in statement integer.

        oledbcommand cmd = new oledbcommand("update data set title = @title id = @id");         cmd.commandtype = commandtype.text;         cmd.connection = conn;         cmd.parameters.addwithvalue("@title", textbox1.text);         cmd.parameters.addwithvalue("@id", param2); 

i tried doing way

        oledbcommand cmd = new oledbcommand("update data set title = ? id = ?");         cmd.commandtype = commandtype.text;         cmd.connection = conn;         cmd.parameters.addwithvalue("?", textbox1.text);         cmd.parameters.addwithvalue("?", param2); 

but it's still not updating!
found out trying fix when replace first parameter string between single quotes (see below), updates table.

oledbcommand cmd = new oledbcommand("update data set title = 'test' id = @id"); 

does of guys have idea why happening?

edit: full code

    using (oledbconnection conn = new oledbconnection(connectionstring))     {         oledbcommand cmd = new oledbcommand("update data set title = @title id = @id");         cmd.commandtype = commandtype.text;         cmd.connection = conn;         cmd.parameters.addwithvalue("@title", textbox1.text);         cmd.parameters.addwithvalue("@id", param2);         conn.open();         try         {             cmd.executenonquery();         }         catch (exception ex)         {             response.write(ex.message);          } 

edit 2:
code after trying set data types, it's still not working.
clarify, in access database id "autonumber" , title "long text"

    using (oledbconnection conn = new oledbconnection(connectionstring))     {         oledbcommand cmd = new oledbcommand("update data set title = ? id = ?");         cmd.commandtype = commandtype.text;         cmd.connection = conn;         cmd.parameters.addwithvalue("title", textbox1.text).oledbtype = oledbtype.varchar;         cmd.parameters.addwithvalue("id", param2).oledbtype = oledbtype.integer;         conn.open();         try         {             var recordsupdated = cmd.executenonquery();         }         catch (exception ex)         {             response.write(ex.message);         } 

when check value of recordsupdated, returns "1" database isn't being updated.

you need call executenonquery executes statement.

// oledbconnection should wrapped in using statement ensure closed using (oledbconnection conn = new oledbconnection(connectionstring)) {     conn.open(); // open connection     using(oledbcommand cmd = new oledbcommand("update data set title = ? id = ?")) // wrap in using block because oledbcommand implements idisposable     {         cmd.commandtype = commandtype.text;         cmd.connection = conn;         cmd.parameters.addwithvalue("title", textbox1.text).oledbtype = oledbtype.varchar; // title, set parameter type         cmd.parameters.addwithvalue("id", param2).oledbtype = oledbtype.integer; // id, guessing integer should replace correct oledbtype         var recordsupdated = cmd.executenonquery(); // execute query          // recordsupdated contains number of records affected     } } 
  • the order op parameters add must match order of parameters in sql statement.
  • be sure specify oledbtype each parameter command not have guess is. guessed title varchar , id integer, correct if wrong assumption.
  • you can replace "?" parameter names make addition of parameters easier read , keep ? placeholders in update statement. microsoft in many of examples in documentation
  • executenonquery returns number of rows affected, capture result , use debugging (see code update)

when check value of recordsupdated, returns "1" database isn't being updated.

if 1 being returned 1 record being updated. here 2 trouble shooting tips.

  • you should make sure query updating same record checking. during debugging capture value of param2 , manually use in select statement in access see title column of record select title data id = [id here]. value of textbox1.text in debugging , check value against returned manually executed select query.
  • as ms access , file resides on disk, make sure manually checking against same database connecting in connection string. if have 2 copies of database lead wrong conclusion update not being performed.

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -