开发者

Concurrency Error updating a row a second time C#

HI I am getting the "Concurrency violation the updatecommand affected 0 of the expected 1 records." error while trying to update a row, weird thing is that I CAN update the row a single time and next time I repeat the process on the same row I get the error, I have already tryed the endinit and endedit thingys, any help would be appreciated!

I am using c# and MySQL innodb

string cs = "server=" + fidaConfig.dtBDConfig.Rows[0][2] + ";user id=root;Password=" + fidaConfig.dtBDConfig.Rows[0][4] + ";persist security info=True;database=facturas"; 
MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection() { ConnectionString = cs }; 
switch (tableInvoice) 
{ 
case "factura_idj": 
    dsLuiscencioTableAdapters.factura_idjTableAdapter idjAdapter = new Control_Administrativo.dsLuiscencioTableAdapters.factura_idjTableAdapter() { Connection = conn }; 
    dsLuiscencio.factura_idjDataTable idj = idjAdapter.GetData(); 
    var facturaidj = (from f in idj where f.no_factura_idj == InvoiceNumber select f).Single(); 
    if (DateTime.Today.Date >= Convert.ToDateTime("01-01-2010") && facturaidj.fecha.Date <= Convert.ToDateTime("01-01-2010")) 
    { 
        var quieresactualizar = MessageBox.Show("Desea Actualizar el total de acuerdo a los nuevos impuestos?", "Reforma Fiscal", MessageBoxButtons.YesNo); 
        if (quieresactualizar == DialogResult.Yes) 
        { 
            switch (facturaidj.opt_iva) 
            { 
        case 1: 
            facturaidj.iva = 0; 
            facturaidj.total = facturaidj.subtotal; 
            break; 
        case 2: 
            facturaidj.iva = facturaidj.subtotal * 0.11; 
            facturaidj.total = facturaidj.subtotal * 1.11; 
            break; 
        case 3: 
            facturaidj.iva = facturaidj.subtotal * 0.16; 
            facturaidj.total = facturaidj.subtotal * 1.16; 
            break; 
        default: 
            break; 

            } 
            Number2Letter.Number2Letter n2l = new Number2Letter.Number2Letter(); 
            string totalwithn开发者_开发问答ocents = n2l.Numero2Letra(facturaidj.total.ToString(), 0, 0, "peso", "", 
        Number2Letter.Number2Letter.eSexo.Masculino, 
        Number2Letter.Number2Letter.eSexo.Masculino).ToUpper(); 

            string strtotalconivaretenido = Math.Round(facturaidj.total, 2, MidpointRounding.ToEven).ToString("###########.00"); 
            string cents = strtotalconivaretenido.Substring(strtotalconivaretenido.IndexOf(".") + 1); 

            facturaidj.total_letra = string.Format(@"{0} {1}/100 {2}", totalwithnocents, cents, facturaidj.tipo_moneda).ToUpper(); 
            idj.EndInit(); 
            idjAdapter.Update(facturaidj);//this runs only the first time on a row, then throws the error 
        } 
    } 
    break; 
    continues......


I don't know if this will have anything to do but:

I've finaly found my problem, this was not related to concurency at all, i've found the problem by testing the same code on a SQL Server database, it was giving me another error message, so then i figure out that maybe MS Access was wrong with his error, and the problem was something else!

In addition you have the FLOAT issue mentioned before:

MySqlCommandBuilder composes the UPDATE command using WHERE col=?1 AND col=?2 ... In FLOAT columns you'll never find an exact value in this way.

Which can perfectly be extended to double values... the problem seems to be a combination of a incorrect error message when the update tries to find the value to update

Could you try running the update command without actually updating anything? That is, instead of making an actual update, remove all the code except for the Update command so that the factura object is exactly the same and see if that works.


I am not sure if you are using float data types in your database, but this article might be what is happening to you: MySql - Float Issue


What is the SQL behind idjAdapter.Update() and how was it generated? Did you use the drag/drop designer in Visual Studio? If you did, then you will want to check the auto-generated update script? In my experience with the designer code in the MS SQL world, it will often include a very complicated WHERE statement where it only updates if every single old field of the row still exists.

If your table happens to contain an auto-generated time stamp field, or something similar, then you can run into issues with two users where this field may get updated behind your back, and so the WHERE in the update will fail, causing this concurrency exception to be thrown by the adapter.

You will then need to decide how to handle the issue. If you simply want to have last in wins logic, then you need to manually change the Update SQL in the designer (from the properties window of the designer for that table adapter) to only include your primary key for the table in the WHERE clause of the Update statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜