C # foreach not getting next XElement name in loop

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++)
    {
        //ZJR: Populate the string arrays with Error
        ErrorCode[i] = srca.FailedSeparationResponse[i].ErrorOccurrence[0].ErrorCode;
        ErrorMessage[i] = srca.FailedSeparationResponse[i].ErrorOccurrence[0].ErrorMessage;
        StateRequestRecordGUID = srca.FailedSeparationResponse[i].StateRequestRecordGUID;

        //ZJR: Database connection to insert Error Records
        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;
                    }
                    //param.NextNode;
                }
                catch (Exception ee)
                {
                    string asdf = ee.ToString();
                }
            }
            SqlCommandBuilder sb = new SqlCommandBuilder(da);
            da.Update(dt);
        }

        if (conn4 != null)
        {
            conn4.Close();
        }

        //ZJR: Build the Email Body
        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
+3
source share
1 answer

I found a problem. It was a line (da). I got rid of this and the code works fine.

0
source

All Articles