Reputation: 11
C# Foreach not completing iterations: if I have 10 XML records it only creates about 6 XML files.
public void PRODUCT()
{
try
{
var toProdSignlCRMList = _db.kv_sp_Product().ToList();
if (toProdSignlCRMList.Count > 0)
{
foreach (kv_sp_Product_Result myProdSignalLoop in toProdSignlCRMList)
{
var erp_prod_signal = new erp_crm_class.PRODUCT
{
CODE = myProdSignalLoop.Code.ToString().Trim(),
SHORTDESC = myProdSignalLoop.Description_1.ToString().Trim(),
INTERNATIONAL = false,
};
XmlSerializer xsSubmit = new XmlSerializer(typeof(erp_crm_class.PRODUCT));
var ProdSignalxml = "";
using (var sww = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sww))
{
xsSubmit.Serialize(writer, erp_prod_signal);
ProdSignalxml = sww.ToString();
using (StreamWriter outputFile = new StreamWriter(Convert.ToString("C:\\Upload\\" + "PRODUCT" + DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".xml")))
{
outputFile.Write(ProdSignalxml);
}
}
}
}
}
}
catch (Exception ex)
{
}
}
Upvotes: 0
Views: 70
Reputation: 239
You are using a Try/Catch block.
A Try/Catch block exits the Try block any time the code fails, and immediately runs the Catch block.
Normally you'd have some sort of error-handling in the catch block but for debugging you could also add the following to help you find out what is going on:
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
If the actual message doesn't contain any useful information, another way to go about is stepping through each loop and see what part fails and why.
Third option and most likely the best one is to remove your Try/Catch block and make sure you are using a debugger that is configured to break at exceptions - this way you get the actual error in your debugger.
Upvotes: 1