I have the following code that should do the following:
If there are records returned in XML format from the web service, scroll through each record to get ErrorCode, ErrorMessage and ID. Email this information. Then analyze each record and write the children to the table. The problem is that it records the same record as many times as the number of records. I canβt understand why. Part of the email is working properly.
Here is the code:
#region If There Are RecordsInError From Broker Acknowledgement
if (srca.NumberOfResponseRecordsInError != "0")
{
int TotalCount ;
int.TryParse(srca.NumberOfResponseRecordsInError,out TotalCount);
string[] ErrorCode = new string[TotalCount];
string[] ErrorMessage = new string[TotalCount];
string EmailBody = null;
string StateRequestRecordGUID = null;
for (int i = 0; i < TotalCount; i++)
{
ErrorCode[i] = srca.FailedSeparationResponse[i].ErrorOccurrence[0].ErrorCode;
ErrorMessage[i] = srca.FailedSeparationResponse[i].ErrorOccurrence[0].ErrorMessage;
StateRequestRecordGUID = srca.FailedSeparationResponse[i].StateRequestRecordGUID;
SqlConnection conn4 = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=test_BdbCSSQL01;Persist Security Info=False;Integrated Security=SSPI;");
conn4.Open();
string sql = "SELECT * FROM ERROROfSIDESStagingOUT";
SqlDataAdapter da = new SqlDataAdapter(sql, conn4);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr;
dr = dt.NewRow();
dt.Rows.Add(dr);
XDocument doc = XDocument.Load("XmlString.xml");
XNamespace ns = "https://uidataexchange.org/schemas";
var node = doc.Descendants(ns + "EmployerTPASeparationResponse");
using (da)
{
foreach (var param in node.Elements())
{
try
{
if (dr.Table.Columns.Contains(param.Name.LocalName))
{
dr[param.Name.LocalName] = param.Value;
}
}
catch (Exception ee)
{
string asdf = ee.ToString();
}
}
SqlCommandBuilder sb = new SqlCommandBuilder(da);
da.Update(dt);
}
if (conn4 != null)
{
conn4.Close();
}
EmailBody = EmailBody + "StateRequestRecordGUID: " + StateRequestRecordGUID + Environment.NewLine + "ErrorCode: " + ErrorCode[i] + " : " + ErrorMessage[i] + Environment.NewLine + Environment.NewLine;
SendEmail mail = new SendEmail();
mail.Send(EmailBody, "Sides Error Event");
}
}
#endregion
Zach source
share