Groups | Blog | Home
all groups > dotnet ado.net > march 2008 >

dotnet ado.net : Update not working


Neil B
3/31/2008 11:06:07 AM
I'm developing a Web App in C#. The following method executes without
exceptions but the database does not get changed. The data is changed in the
record[0] at the point prior to executing the " adapter.Update(record);"
instruction.

Is there something else I should be doing to insure the update to take
effect???

Thanks, Neil


protected void UpdateUserFields(String id, String field, String value)
{

//***********************************************************// ***** Make
connection, process request, break connection
//***********************************************************
using (OdbcConnection connection =
new OdbcConnection(ConnectionBuilder().ConnectionString))
{
// ***** Make connection and get dataRows *****
String sql = "select * from Register where Id='" + id + "'";
OdbcDataAdapter adapter = new OdbcDataAdapter(sql,
connection);
DataSet dataSet = new DataSet();
try { connection.Open(); adapter.Fill(dataSet, "Register"); }
catch (Exception ex) { Console.WriteLine(ex.Message); }
DataRow[] record = dataSet.Tables["Register"].Select();
if (record.Count() == 0) return;

//****** Process update request ******
// Update the cpu id list in the user's registation record
if (field=="CpuId") record[0]["CpuId"]=value;


//****** Update the data base with changes ******
// Mark the changes for updating
record[0].AcceptChanges();

// Update the changes
adapter.Update(record);
}
}


Norman Yuan
3/31/2008 1:12:13 PM
Remove this line of code:

// Mark the changes for updating
record[0].AcceptChanges();

You need a bit study on when/why to use/not use
AcceptChanges()/RejectChanges().



[quoted text, click to view]
AddThis Social Bookmark Button