I’m using Visual C# Studio 2010 express and have been trying to batch update my table. Everytime that I try to update 1 record I get “Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.” I asked this question at dream in code but I have been a little dissapointed in some of the responses to some honest questions. Link to my question at D.I.C
I took that last suggestions and changed my code to affect the dataset..or so I think but i am getting the concurrent issue still. Here is my updated code.
private void SendNewPotentialsToZoho()
{
Console.WriteLine("Trying to send potentials to Zoho");
var newZoho = potentialDatabaseDataSet.Potential.DefaultView;
var poster = new ZohoPoster(ZohoPoster.Fields.Potentials, ZohoPoster.Calls.insertRecords);
var count = 0;
//newZoho.RowFilter = "IsNull(ZohoID,'zoho') = 'zoho'";
newZoho.RowFilter = "Soid = 1234";
poster.Debugging = !UseZoho;
for (int i = 0; i < 1; i++)//newZoho.Count; i++)
{
var xmlString = Potential.GetPotentialXML(newZoho[i][1], newZoho[i][2], newZoho[i][4], newZoho[i][3], newZoho[i][5], newZoho[i][7], newZoho[i][0]);
Console.WriteLine("Sending New Records to Zoho: {0}", xmlString);
poster.PostItem.Set("xmlData", xmlString);
var result = poster.Post(3);
Console.WriteLine(result);
if (!string.IsNullOrEmpty(result))
{
try
{
var rowLength = newZoho[i].Row.ItemArray.Length;
var rowOffset = Math.Abs((int)newZoho[i][rowLength - 1])-1;
potentialDatabaseDataSet.Potential.Rows[rowOffset]["ZohoID"] = ReadResult(result);
potentialTableAdapter.Update(potentialDatabaseDataSet.Potential.Rows[rowOffset]);
}
catch (Exception ex)
{
Console.WriteLine("Failed to update: {0}", ex.Message);
}
}
}
}
The bit variable called poster works great. it returns a xml like result that has my zohoID which I parse out and try to store. For testing purposes I try to update only one record. I get the error with potentialTableAdapter.Update(potentialDatabaseDataSet). What is strange to me is that I use a very similar code to make a brand new record and it works great. Infact it is how I made the row with Soid = 1234. I am the only one that has access to this program and as far I know it is not multithreaded so I just don’t understand why it is having concurrency issues. Please help 🙂
EDIT
Ok so I was playing around with it some and found that if I add a EndEdit to it I dont get the concurrent issue. On the flip side though Although my bound datagridview shows the updated data, the data doesn’t actually get updated. So it’s not that i’m back to square one I think i am actually rather close. I’m going from memory on this small bit of code so don’t hate if it isn’t right..it’s mainly for an idea of what i’m talking about
for (int i = 0; i < 5; i++) //emailRecord.Count; i++)
{
if (ZohoEmail.EmailExpectedShipping(emailRecord[i], "12/10/2012"))
{//true means that the email went through just fine
try
{
var rowLengh = emailRecord[i].Row.ItemArray.Length;
var rowOffset = Math.Abs((int)emailRecord[i][rowLengh - 1]) - 1;
potentialDatabaseDataSet.Potential.Rows[rowOffset][17] = true; //17 is Expected Email
potentialDatabaseDataSet.Potential.Rows[rowOffset].AcceptChanges();
}
catch (Exception ex)
{
Console.WriteLine("Failed to update: {0}", ex.Message);
}
}
potentialTableAdapter.Update(potentialDatabaseDataSet.Potential);
}
}
Well I think i figured it out. Thank you very much Marcin for helping me so much on this problem of mine. You were right with the problem being in my generated table adapter. I ended up making my own based on what the autogenerated code was. Here is my code just incase anyone else is having problem with Autogenerated code.
now for my wrapper class. I find this to be rather useful to do it this way.
using System;
using System.Data.SqlServerCe;
using System.Data;
and lastly for the snippet of code that actually matters. The one that Marcin helped me figure out. It is super easy actually.
I tried it out and it said success so i opened up the database in a different program and low and behold my fake data has the zoho id in it 🙂 i’m so excited. Now I can actually get this program up and running. The only problem I am having now is that in my datagridview it is only showing like 4 or 5 records instead of 201 records (and i just thought about it i bet that filter has soemthign to do with it!). So well time to finish up my program now that it works! 🙂 thank you again Marcin for being patient and helping me out. Since you told me that the problem was in my tableAdapter I am going to mark the question as answered by you.