DBConcurrencyException

  • Thread starter Thread starter 2G
  • Start date Start date
2

2G

Hi,

I seem to have some problems updating my dataset, I always get a error that
no rows were updated...:

An unhandled exception of type 'System.Data.DBConcurrencyException' occurred
in system.data.dll
Additional information: Concurrency violation: the UpdateCommand affected 0
records.

If anyone would have a look at it , I would appreciate it.
Al works fine for the insert and the update works in another dataaccessor so
I shouldn't be my OleDbHelper.

Thanks.

public class ReagentDataAccessor : DataAccessor{



public ReagentDataAccessor() {

ReagentDataset ds = this.GetReagents();

ds.Reagents[0].Name = "Blabla";

this.Update(ds);

}



public ReagentDataset GetReagents() {

ReagentDataset ds = new ReagentDataset();

string sql = "Select * from Reagents";

OleDbHelper.FillDataset(this.Connection, CommandType.Text,

sql, ds, new string[]{ "Reagents" });

return ds;

}



public void Update(ReagentDataset ds){

OleDbHelper.UpdateDataset(this.GetInsertCommand(),

this.GetDeleteCommand(),

this.GetUpdateCommand(),

ds,

"Reagents");

}



protected override OleDbCommand GetInsertCommand(){

string sql = "Insert into Reagents (SkillId, Name, NPCPrice,
StackSize, PlacesFound, BImg) values (@SkillId, @Name, @NPCPrice,
@StackSize, @PlacesFound, @BImg)";

OleDbCommand cmd = new OleDbCommand(sql, this.Connection);

cmd.Parameters.Add("@SkillId", OleDbType.Integer, 0, "SkillId");

cmd.Parameters.Add("@Name", OleDbType.VarChar, 255, "Name");

cmd.Parameters.Add("@NPCPrice", OleDbType.Double, 0, "NPCPrice");

cmd.Parameters.Add("@StackSize", OleDbType.Integer, 0, "StackSize");

cmd.Parameters.Add("@PlacesFound", OleDbType.WChar, 0,
"PlacesFound");

cmd.Parameters.Add("@BImg", OleDbType.VarChar, 255, "BImg");

return cmd;

}



protected override OleDbCommand GetUpdateCommand(){

string sql = "UPDATE Reagents SET SkillId=@SkillId, Name=@Name WHERE
Id=@Id";

OleDbCommand cmd = new OleDbCommand(sql, this.Connection);

cmd.Parameters.Add("@Id", OleDbType.Integer, 0, "Id");

cmd.Parameters.Add("@SkillId", OleDbType.Integer, 0, "SkillId");

cmd.Parameters.Add("@Name", OleDbType.VarChar, 255, "Name");

// string sql = "Update Reagents set SkillId=@skillid, Name=@name,
NPCPrice=@npcprice, StackSize=@stacksize, PlacesFound=@placesfound,
BImg=@bimg Where Id=@id";

// OleDbCommand cmd = new OleDbCommand(sql, this.Connection);

// cmd.Parameters.Add("@id", OleDbType.Integer, 0, "Id");

// cmd.Parameters.Add("@skillid", OleDbType.VarChar, 255,
"SkillId");

// cmd.Parameters.Add("@name", OleDbType.VarChar, 255, "Name");

// cmd.Parameters.Add("@npcprice", OleDbType.Double, 0, "NPCPrice");

// cmd.Parameters.Add("@stacksize", OleDbType.Integer, 0,
"StackSize");

// cmd.Parameters.Add("@placesfound", OleDbType.WChar, 0,
"PlacesFound");

// cmd.Parameters.Add("@bimg", OleDbType.VarChar, 255, "BImg");

return cmd;

}



protected override OleDbCommand GetDeleteCommand(){

string sql = "Delete from Reagents where Id=@Id";

OleDbCommand cmd = new OleDbCommand(sql, this.Connection);

cmd.Parameters.Add("@Id", OleDbType.Integer, 0, "Id");

return cmd;

}

}
 
2G said:
I seem to have some problems updating my dataset, I always get a error that
no rows were updated...:

An unhandled exception of type 'System.Data.DBConcurrencyException' occurred
in system.data.dll
Additional information: Concurrency violation: the UpdateCommand affected 0
records.

If anyone would have a look at it , I would appreciate it.
Al works fine for the insert and the update works in another dataaccessor so
I shouldn't be my OleDbHelper.

The problem is that you're using parameters as if the names meant
something - for the OleDb provider, they don't (when using
CommandType=Text). They're just positional parameters - which may be
made clearer if you just use ? instead of names. If you reorder your
parameters for the update statement to be the same order as *in* the
statement, it should work.

See OleDbCommand.Parameters for more information.
 
Ok, I changed it to :

string sql = "UPDATE Reagents SET SkillId=?, Name=? WHERE Id=?";

OleDbCommand cmd = new OleDbCommand(sql, this.Connection);

cmd.Parameters.Add("@skillid", OleDbType.Integer, 0, "SkillId");

cmd.Parameters.Add("@name", OleDbType.VarChar, 255, "Name");

cmd.Parameters.Add("@id", OleDbType.Integer, 0, "Id");



and now it works, thanks Jon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top