索引超出界限错误?

时间:2015-01-23 17:09:39

标签: c# database loops indexing

所以我正在开发一个应用程序,它要求我遍历所有条目并更新提醒日期并将串行密钥与每个用户匹配。出于某种原因,即使我没有迭代所有已知的索引

,我也会出现越界错误
public void updateReminders(RichTextBox rtb, RichTextBox rtb2, DataTable d1)
    {
        for(int i = 0; i < ChildRoot.Nodes.Count; i++)
        {

            dr = d1.Rows[i];
            TreeNode tn = ChildRoot.Nodes[i];

            DateTime dtCal = Convert.ToDateTime(dr["NEXTCONTACT"]);
            DateTime dtCalTime = dtCal.AddDays(60);

            string clientName = tn.Nodes[0].Text;
            string clientNameTrimmed = clientName.Split(new string[] { "NAME: " }, StringSplitOptions.None).Last();

            if(dtCalTime <= DateTime.Today)
            {
                rtb.AppendText("Calibration Client: " + clientNameTrimmed + " is due for a call! " + dtCal + Environment.NewLine);
                rtb2.AppendText("Calibration Client: " + clientNameTrimmed + " is due for a call! " + dtCal + Environment.NewLine);
                tn.Nodes[4].Text = "NEXT CONTACT: " + DateTime.Today.ToLongDateString();

                dr = dts1.Rows[i];
                dr["NEXTCONTACT"] = DateTime.Today.ToLongDateString();
                dr["LASTCONTACT"] = DateTime.Today.ToLongDateString();
                dr.AcceptChanges();

                string currentPath = Directory.GetCurrentDirectory();

                if (!Directory.Exists(Path.Combine(currentPath, "AppData")))
                {
                    Directory.CreateDirectory(Path.Combine(currentPath, "AppData"));
                }

                try
                {
                    string savePath = string.Concat(Environment.CurrentDirectory, @"\AppData\ClientHistory_Calibration.ini");
                    string notePath = "Calibration Client: " + clientNameTrimmed + " is due for a call! " + dtCal;
                    string[] lines = { notePath };

                    System.IO.File.WriteAllLines(savePath, lines);
                    MessageBox.Show("Remind History has automatically been saved to 'Install directory/AppData/ClientHistory_Calibration.ini'", "Saved!");
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error", e.ToString());
                }
            }
        }
    }

异常消息:

System.IndexOutOfRangeException: There is no row at position 3.
   at System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex)
   at System.Data.DataRowCollection.get_Item(Int32 index)
   at ClientDataBase.Code.ClientTreeNodeView.updateReminders(RichTextBox rtb, RichTextBox rtb2, DataTable d1) in c:\Users\AdamSMI\Desktop\DatabaseTest\ClientDataBase\ClientDataBase\Code\ClientTreeNodeView.cs:line 541
   at ClientDataBase.Form1.Form1_Load(Object sender, EventArgs e) in c:\Users\AdamSMI\Desktop\DatabaseTest\ClientDataBase\ClientDataBase\Code\Form1.cs:line 57

1 个答案:

答案 0 :(得分:1)

请注意,当您在ChildRoot.Nodes中检查了计数时,您检查了Rowdtdtsl的数量 - 通过特别提到row at position 3的错误消息判断,其中一个是罪魁祸首。确保您检查行是否存在 - 如果不存在则可能是在应用程序中某处保持同步的问题,或者您可能需要在代码执行时添加它,而不是检索它。