Hello there im trying to save some data from dynamic textboxes into XML file. But everytime i try i get this error
"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
XmlDocument document = new XmlDocument();
document.Load(@"myGrades.xml");
for (int i = 0; i < 7; i++)
{
**TextBox tb1 = (TextBox)textboxComputer[i];** //<- error is thrown here
string markGained = tb1.Text;
this.Text = markGained;
XmlElement root = document.DocumentElement;
XmlElement newAssessmentMark = document.CreateElement("Assessment");
XmlElement newMark = document.CreateElement("AssessmentMark");
XmlText mark = document.CreateTextNode(markGained);
newAssessmentMark.AppendChild(newMark);
newMark.AppendChild(mark);
XmlNode parentNode = document.SelectSingleNode("myGrades/courseStructure/level4");
parentNode.InsertAfter(newAssessmentMark, parentNode.LastChild);
document.Save(@"myGrades.xml");
}
this.Close();
Help appreciated :)
textboxComputer
seems to be an array, so in your for loop do:
for( int i = 0; i < textboxComputer.Length; i++ ) ...
Alternatively you can pre-compute it:
int length = textboxComputer.Length;
for( int i = 0; i < length; i++ ) ...