on-error resume next in C# for CRM 2011 how?
-
donderdag 31 mei 2012 19:15
Hi, I am trying to update or insert a record in CRM 2011 say lead. I am able to inser or update the records. But I want to know if there is any error, how could I record the error details and then resume so that the operations will continue. Similar to VB, where you have on-error resume next, but I am writing the code in c#.
I am using the catch to capture the errors and then, how could I resume the operation. Say, program loops through a bunch of records (20) and if it encounters an error at record count 10, then what I intend is that:
1) capture the record detail such as row Id, name etc.,
2) resume to the next record, until the end of the records. (I am using the for loop)
Thanks for your help.
Alle reacties
-
donderdag 31 mei 2012 19:34
EntityCollection failedUpdates = new EntityCollection(); foreach(Entity en in entitiesToUpdate){ try{ service.Update(en); }catch(Exception e){ failedUpdates.Entities.Add(en); } } // here, in failedUpdates.Entites are all entities which couldn't be updated
It's just important to have a try - catch block inside the for/foreach loop. in catch block you can capture the details however you want.
And one more thing, the exception thrown by the webservice probably wont help you att all. Most of the times, if operation failes, the System.Net.WebException ( i think ) is thrown, with the message "Not Found". So it's not very helpfull :)
- Bewerkt door Ivan Kovacek donderdag 31 mei 2012 19:39
-
donderdag 31 mei 2012 20:28
Thanks a lot Ivan. I generally have the try.. catch block. its good idea to capture it in a entity collection. Now, say if the system encounter any kind of error, after I catch the exception, will the system resume to complete the for loop or will it stop the operation?
from records 1 through 10 is fine and 11, 15, 18th records are bad. However, after it encounters the error at 11th record, it will log that in the entity collection and I assume that it will continue.
-
donderdag 31 mei 2012 21:53
Yes, it will resume. For example if you are trying to update 10 entities, and a failure occurs in a 5-th entity, the loop will continue working.
Generally, exception handling mechanism in C# works like this (simplified) :
try{ //do stuff //do more stuff }catch(exception e){ // trace exception } // do next stuff
No matter if exception occurs, or not in try block, the "do next stuff" block will be reached. So, if the try catch block is inside a loop, the loop will go to the next step, because that is the "do next stuff" in this case.
- Als antwoord gemarkeerd door CRM elite donderdag 31 mei 2012 22:18