I have a function, InsertItems:
public void InsertItems()
{
todoitemList.Clear();
todoSelect.Items.Clear();
foreach (XmlNode xmlNode in xmlDoc.SelectNodes("ToDoList/ToDo"))
{
ToDoItem item = new ToDoItem();
item.ID = xmlNode.SelectSingleNode("ID").InnerText;
item.Title = xmlNode.SelectSingleNode("Title").InnerText;
item.Description = xmlNode.SelectSingleNode("Desc").InnerText;
item.PriorityLevel = xmlNode.SelectSingleNode("Priority").InnerText;
item.Date = Convert.ToDateTime(xmlNode.SelectSingleNode("Date").InnerText);
item.TimeHour = Convert.ToInt32(xmlNode.SelectSingleNode("TimeHour").InnerText);
item.TimeMinute = Convert.ToInt32(xmlNode.SelectSingleNode("TimeMinute").InnerText);
item.TimeSecond = Convert.ToInt32(xmlNode.SelectSingleNode("TimeSecond").InnerText);
item.Completed = xmlNode.SelectSingleNode("Completed").InnerText;
todoitemList.Add(item);
todoSelect.Items.Add(item.Title);
todoIDList.Add(item.ID);
}
}
This function clears both a list and the combo box to which is used to select the items and then fills the list with the relevant data. ToDoItem is a class which contains properties – ID, Title and so forth.
When the function is executed within Form1.cs, it fully works as expected, clearing the list and adding the new data. However, when the function is executed within Form2.cs (main.InsertItems()), the foreach loop appears to be never ran and I’ve really no idea what is causing this.
Any help is much appreciated!
—
Edit:
main.InsertItems() is called in the following function:
private void createNew_Click(object sender, EventArgs e)
{
if (CheckAll())
{
XmlNode xmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "ToDo", null);
XmlNode xmlNodeID = xmlDoc.CreateElement("ID");
xmlNodeID.InnerText = CreateRandomID();
XmlNode xmlNodeTitle = xmlDoc.CreateElement("Title");
xmlNodeTitle.InnerText = textBoxTitle.Text;
XmlNode xmlNodeDesc = xmlDoc.CreateElement("Desc");
xmlNodeDesc.InnerText = textBoxDesc.Text;
XmlNode xmlNodePriority = xmlDoc.CreateElement("Priority");
xmlNodePriority.InnerText = Convert.ToString(priorityLevel.SelectedItem);
XmlNode xmlNodeDate = xmlDoc.CreateElement("Date");
string currentDate = Convert.ToString(monthCalendar.SelectionRange.Start);
string strippedDate = currentDate.Substring(0, currentDate.Length - 8);
strippedDate += timeHour.Text + ":" + timeMinute.Text + ":" + timeSecond.Text;
xmlNodeDate.InnerText = strippedDate;
XmlNode xmlNodeTimeHour = xmlDoc.CreateElement("TimeHour");
xmlNodeTimeHour.InnerText = timeHour.Text;
XmlNode xmlNodeTimeMinute = xmlDoc.CreateElement("TimeMinute");
xmlNodeTimeMinute.InnerText = timeMinute.Text;
XmlNode xmlNodeTimeSecond = xmlDoc.CreateElement("TimeSecond");
xmlNodeTimeSecond.InnerText = timeSecond.Text;
XmlNode xmlNodeCompleted = xmlDoc.CreateElement("Completed");
xmlNodeCompleted.InnerText = "False";
xmlNode.AppendChild(xmlNodeID);
xmlNode.AppendChild(xmlNodeTitle);
xmlNode.AppendChild(xmlNodeDesc);
xmlNode.AppendChild(xmlNodePriority);
xmlNode.AppendChild(xmlNodeDate);
xmlNode.AppendChild(xmlNodeTimeHour);
xmlNode.AppendChild(xmlNodeTimeMinute);
xmlNode.AppendChild(xmlNodeTimeSecond);
xmlNode.AppendChild(xmlNodeCompleted);
xmlDoc.DocumentElement.AppendChild(xmlNode);
try
{
xmlDoc.Save(_fileName);
MessageBox.Show("Item successfully added!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
main.InsertItems();
}
catch (XmlException)
{
MessageBox.Show("Error! The item could not be added due to an XML error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (IOException)
{
MessageBox.Show("Error! The file could not be found or written to. Item could not be added.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception)
{
MessageBox.Show("Error! An unknown error occured. Item could not be added.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Close();
}
}
CheckAll returns a boolean -> true if all fields are valid.
_fileName variable is correct and is saving to the correct file.
The problem is that you’re not reloading
xmlDocinForm1.InsertItems.In the code you posted, before calling
Form1.InsertItemsfromForm2, you write the file to disk, but since you’re not passing a reference to the updatedxmlDocfromForm2on toForm1, you don’t see the changes show up onForm1.InsertItemsworks as expected when you call it fromForm1becausexmlDocis a member variable forForm1, so the changes are available inInsertItemswhen called fromForm1.Try either reloading the
XmlDocumentfrom the file system at the beginning ofInsertItemsor pass anXmlDocumenttoInsertItemsas a parameter.