c# - Problem with ADO.NET UPDATE code -


could take quick peek @ ado.net code? trying update row dataset, isn't working. missing elemental piece of code, , eluding me. have verified datarow has correct data in it, row accurate.

many in advance.

 try             {                 //basic ado.net objects                 sqldataadapter dbadapter = null;                 dataset returnds2 = new dataset();                  //a new sql connection                 sqlconnection myconn = new sqlconnection();                 myconn.connectionstring = "server=myserver.mydomain.com;"                      + "database=mydatabase;"                      + "user id=myuserid;"                      + "password=mypassword;"                      + "trusted_connection=true;";                  //the sqlquery                 string sqlquery = "select * avlupdatemessages id = 21";                  //another ado.net object command                 sqlcommand cmd = new sqlcommand();                 cmd.connection = myconn;                 cmd.commandtext = sqlquery;                  //open connection, execute sql statement , close connection.                 myconn.open();                  //instantiate , fill sqldataadapter                 dbadapter = new sqldataadapter(cmd);                 dbadapter.fill(returnds2, @"avlupdatemessages");                  //loop through of rows; have verified rows correct , returns correct data db                 (int = 0; <= returnds2.tables[0].rows.count - 1; i++)                 {                     datarow row = returnds2.tables[0].rows[i];                     row.beginedit();                     row["updatedtext"] = @"this test...";                     row.endedit();                 }                  //let's accept changes                 dbadapter.update(returnds2, "avlupdatemessages");                 returnds2.acceptchanges();                  myconn.close();              }   

you might able use sqlcommandbuilder out. after fill call, add following statement. associate command builder data adapter , (if there primary key available) should generate update statement you. note there expense behind command builder. may not relative else, involve looking @ schema information (to primary key information, field names, field types, etc.) table , generating insert, delete, , update statements involving fields in table.

sqlcommandbuilder cb = new sqlcommandbuilder(dbadapter); 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -