concurrency viloation "the update command affect 0 of the expected 1 records" RRS feed

  • Question


    hello everybody,

    when i use the following code i find concurrency violationn exception.

    can anybody help me?.

    note that id is autonumber field.


    public void UpdateRec(

    int id,

    string name,

    string title,

    string home_phone,

    string mobile_phone,

    string address,

    string notes)


    /* First

    * ..........................

    * Update the record from RAM


    DataRow[] DR = dttel.Select("id=" + id);


    DR[0]["id"] = id;

    DR[0]["name"] = name;

    DR[0]["title"] = title;

    DR[0]["home_phone"] = home_phone;

    DR[0]["mobile_phone"] = mobile_phone;

    DR[0]["address"] =address;

    DR[0]["notes"] = notes;


    /* Second

    * .........................

    * Update the record from Harddisk*/

    string Update_str = "update tel set " +

    "name=@name," +

    "title=@title," +

    "home_phone=@home_phone," +

    "mobile_phone=@mobile_phone," +

    "address=@address," +

    "notes=@notes " +

    "where id=@id ";

    //defining sqlcommands.

    OleDbCommand Update_cmd = new OleDbCommand(Update_str, MyConnection.connect());

    //defining the parameters and the textboxes binded to it.


    Update_cmd.Parameters.Add("@id", OleDbType.Integer, 100, "id");

    Update_cmd.Parameters["@id"].Value = id;


    Update_cmd.Parameters.Add("@name", OleDbType.LongVarChar, 100, "name");

    Update_cmd.Parameters["@name"].Value = name;


    Update_cmd.Parameters.Add("@title", OleDbType.LongVarChar, 100, "title");

    Update_cmd.Parameters["@title"].Value = title;


    Update_cmd.Parameters.Add("@home_phone", OleDbType.LongVarChar, 100, "home_phone");

    Update_cmd.Parameters["@home_phone"].Value = home_phone;


    Update_cmd.Parameters.Add("@mobile_phone", OleDbType.LongVarChar, 100, "mobile_phone");

    Update_cmd.Parameters["@mobile_phone"].Value = mobile_phone;


    Update_cmd.Parameters.Add("@address", OleDbType.LongVarChar, 100, "address");

    Update_cmd.Parameters["@address"].Value = address;


    Update_cmd.Parameters.Add("@notes", OleDbType.LongVarChar, 100, "notes");

    Update_cmd.Parameters["@notes"].Value = notes;

    //(defining the adapter command

    DA.UpdateCommand = Update_cmd;

    //update the datasource.





    catch (DBConcurrencyException ex)


    MessageBox.Show("Warning" + ex.Message,

    "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);



    Tuesday, January 22, 2008 2:12 PM

All replies

  • At the risk of stating the obvious, the errror message is basically telling you that, based on the values supplied, it can't find any matching records upon which to perform the update.

    I'd normally not do this by hand but use the automatically generated dataadaptor methods, something like:

    DataRow dr = dttel.FindByid(id);//assumes original row from db has been loaded first

    dr["ColName"] = "new Value"


    ADO.NET will know this is an update rather than an insert and do everything else for you...

    Looking at the code supplied, I''m not seeing an obvious way for .net to tell the difference between what the original record is and the new values you want to replace are...
    Friday, January 25, 2008 4:19 AM