Monday, May 17, 2010

eConnect "System Error", Part D'eux

Back in January I wrote about some fun I was having with eConnect 9 and the dreaded "System Error" message. I must really, really like that error; it certainly seems to like me.

Back then I made sure that my eConnect calls had a SqlException error handler, and things worked fine. Until, of course, I got the System Error yet again on a recent integration with eConnect 9.

I actually developed the integration in GP 10, and it worked fine, but then realized the client was using GP 9, so I quickly made the simple changes required to convert it to GP 9. After the first successful test import, the error started to occur.

Staring at the error in disbelief (Why me???!!!!), I checked my try / catch block and confirmed that I did have the SqlException handler. I checked my document XML, I checked my connection string, I tried adding and removing different fields from my document, but nothing worked: System Error.

So then I copied the project over to a new server and tried it, and it worked! So then I was really puzzled, as the issue seemed machine specific.

Having run out of ideas, I went to check the PartnerSource support KB to see if there were any hints. And miraculously, I found the gem that is called KB 943133.

When I first started reading the knowledge base article, and it just mentioned including a SqlException exception, I started to get disappointed, since I already had that.

But, after reading the sample code in the article, I found something new.

Within a SqlException exception, there is a collection of SqlClient.SqlError error objects. When my code hit my SqlException, I was just trying to output "ex.Message". I was not looping through the error objects and getting the multiple potential messages, like this:

catch (eConnectException ex)
{
Console.WriteLine(ex.Message);
}
catch (System.Data.SqlClient.SqlException ex)
{
foreach (System.Data.SqlClient.SqlError myError in ex.Errors)
{
Console.WriteLine(myError.Message);
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}


The code in red was what I was missing.

Once I added this, the System Error disappeared and I got a meaningful error indicating that I had a duplicate primary key error. I had manually deleted some test records via SQL in my TWO database while testing, and I missed a table, so eConnect was failing due to a duplicate insert. Which is why the error only happened on the first server, but not the second.

Once again, this is pretty simple stuff that was actually posted on a few sources and widely available, but since the standard ex.Message had been working for me, I never bothered to look further at any other samples.

Since I can't recall running into the System Error with eConnect 10, my impression is that this is primarily an issue with eConnect 9. But now that I have the code setup properly, hopefully that solves the issue, permanently.

System Error, if I ever see you again, it will be too soon.

No comments: