【发布时间】:2015-03-22 19:10:08
【问题描述】:
所以我正在开发一个应用程序,该应用程序需要我遍历所有条目并更新提醒日期并匹配每个用户的序列键。出于某种原因,即使我没有遍历所有已知索引,我也会遇到越界错误
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
【问题讨论】:
-
为什么
i < ChildRoot.Nodes.Count是d1.Rows[i];的有效守卫? -
你知道越界错误发生在哪一行吗?是否抛出 IndexOutOfRangeException?
-
使用调试器或至少在堆栈跟踪中发布异常信息。
-
你不能使用调试器告诉我们异常发生在哪一行吗?
-
不要在 cmets 中添加信息,使用 edit 链接改进您的问题。然后您也可以正确格式化它。
标签: c# database loops indexing