System.IndexOutOfRangeException
-
Hi, I have a weird problem, hopefully someone can shine a light on it? The user gets a list of defects on a product from the database. I put them in a List, like so:
while (rdr.Read())
{
DrumDefect defect = new DrumDefect()
{
AVI_Defect = new AVIDefect()
{
SequenceNumber = (rdr["AVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["AVIProdUDefectSeqNr"].ToString()) : (rdr["MVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["MVIProdUDefectSeqNr"].ToString()) : -1,
Code = (rdr["AVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["AVIProdUDefectCode"].ToString(),
Name = (rdr["AVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["AVIProdUDefectName"].ToString(),
Severity = (rdr["AVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["AVISeverityLevelID"].ToString())
},
MVI_Defect = new Defect()
{
Code = (rdr["MVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["MVIProdUDefectCode"].ToString(),
Name = (rdr["MVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["MVIProdUDefectName"].ToString(),
Severity = (rdr["MVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["MVISeverityLevelID"].ToString())
},
FVI_Defect = new Defect()
};
list.Add(defect);
}Then I import them in a datagridview like so:
\_SelectedDrum.Defects = database.GetList(); dataGridViewDefects.DataSource = null; dataGridViewDefects.DataSource = \_SelectedDrum.Defects; dataGridViewDefects.Refresh();
The user can add a defect, so I add one like this:
DrumDefect defect = new DrumDefect() { AVI\_Defect = new AVIDefect() { SequenceNumber = -1, Code = "# 0", Name = "NODEFECT", Severity = DefectSeverity.NODEFECT }, MVI\_Defect = new Defect() { Code = "# 0", Name = "NODEFECT", Severity = DefectSeverity.NODEFECT }, FVI\_Defect = new Defect() }; \_SelectedDrum.Defects.Add(defect);
it all works fine, except when the database list is empty, the user adds a def
-
Hi, I have a weird problem, hopefully someone can shine a light on it? The user gets a list of defects on a product from the database. I put them in a List, like so:
while (rdr.Read())
{
DrumDefect defect = new DrumDefect()
{
AVI_Defect = new AVIDefect()
{
SequenceNumber = (rdr["AVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["AVIProdUDefectSeqNr"].ToString()) : (rdr["MVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["MVIProdUDefectSeqNr"].ToString()) : -1,
Code = (rdr["AVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["AVIProdUDefectCode"].ToString(),
Name = (rdr["AVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["AVIProdUDefectName"].ToString(),
Severity = (rdr["AVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["AVISeverityLevelID"].ToString())
},
MVI_Defect = new Defect()
{
Code = (rdr["MVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["MVIProdUDefectCode"].ToString(),
Name = (rdr["MVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["MVIProdUDefectName"].ToString(),
Severity = (rdr["MVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["MVISeverityLevelID"].ToString())
},
FVI_Defect = new Defect()
};
list.Add(defect);
}Then I import them in a datagridview like so:
\_SelectedDrum.Defects = database.GetList(); dataGridViewDefects.DataSource = null; dataGridViewDefects.DataSource = \_SelectedDrum.Defects; dataGridViewDefects.Refresh();
The user can add a defect, so I add one like this:
DrumDefect defect = new DrumDefect() { AVI\_Defect = new AVIDefect() { SequenceNumber = -1, Code = "# 0", Name = "NODEFECT", Severity = DefectSeverity.NODEFECT }, MVI\_Defect = new Defect() { Code = "# 0", Name = "NODEFECT", Severity = DefectSeverity.NODEFECT }, FVI\_Defect = new Defect() }; \_SelectedDrum.Defects.Add(defect);
it all works fine, except when the database list is empty, the user adds a def
Do you have an event handler method set up for the click event?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Do you have an event handler method set up for the click event?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
yes i have, but it does not reach the breakpoint.
private void dataGridViewDefects\_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { }
in fact i have the following event procedures, but none of them is the source of the problem (breakpoint not hit when the error occures)
this.dataGridViewDefects.CellMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects\_CellMouseClick); this.dataGridViewDefects.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects\_CellMouseDoubleClick); this.dataGridViewDefects.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridViewDefects\_RowEnter); this.dataGridViewDefects.Enter += new System.EventHandler(this.dataGridViewDefects\_Enter); this.dataGridViewDefects.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGridViewDefects\_KeyDown); this.dataGridViewDefects.Leave += new System.EventHandler(this.dataGridViewDefects\_Leave);
-
yes i have, but it does not reach the breakpoint.
private void dataGridViewDefects\_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { }
in fact i have the following event procedures, but none of them is the source of the problem (breakpoint not hit when the error occures)
this.dataGridViewDefects.CellMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects\_CellMouseClick); this.dataGridViewDefects.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects\_CellMouseDoubleClick); this.dataGridViewDefects.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridViewDefects\_RowEnter); this.dataGridViewDefects.Enter += new System.EventHandler(this.dataGridViewDefects\_Enter); this.dataGridViewDefects.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGridViewDefects\_KeyDown); this.dataGridViewDefects.Leave += new System.EventHandler(this.dataGridViewDefects\_Leave);
Are any of the methods in the stack trace yours, or are they all framework?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Are any of the methods in the stack trace yours, or are they all framework?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
they are all framework, except maybe OnRowEnter, but the breakpoint i set did not get hit.
private void dataGridViewDefects_RowEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
if (_SelectedDrum != null && e.RowIndex > -1) _SelectedDefect = _SelectedDrum.Defects[e.RowIndex];
}
catch (Exception exc)
{
Logger.LogException(exc);
}
}the application crashes in the line:
Application.Run(new TwisterForm());
-
they are all framework, except maybe OnRowEnter, but the breakpoint i set did not get hit.
private void dataGridViewDefects_RowEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
if (_SelectedDrum != null && e.RowIndex > -1) _SelectedDefect = _SelectedDrum.Defects[e.RowIndex];
}
catch (Exception exc)
{
Logger.LogException(exc);
}
}the application crashes in the line:
Application.Run(new TwisterForm());
Can you try enabling all exceptions? From the menu, "Debug...Exceptions", and tick everything in the "Thrown" column. That way, even if an exception is being handled, the code should break (and the reference sources should help you locate why). Trouble is that if I do a "minimum setup" to mimic your problem, I get no exception:
public class MyObject
{
public string Text { get; set; }
public int Value { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
MyObject mo = new MyObject();
mo.Text = "hello";
mo.Value = 666;
myList.Add(mo);
myDataGridView.Refresh();
}
private List<MyObject> myList = new List<MyObject>();
private void MyButton_Click(object sender, EventArgs e)
{
myDataGridView.DataSource = null;
myList = new List<MyObject>();
myDataGridView.DataSource = myList;
myDataGridView.Refresh();So I can't get any additional info to work from.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Can you try enabling all exceptions? From the menu, "Debug...Exceptions", and tick everything in the "Thrown" column. That way, even if an exception is being handled, the code should break (and the reference sources should help you locate why). Trouble is that if I do a "minimum setup" to mimic your problem, I get no exception:
public class MyObject
{
public string Text { get; set; }
public int Value { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
MyObject mo = new MyObject();
mo.Text = "hello";
mo.Value = 666;
myList.Add(mo);
myDataGridView.Refresh();
}
private List<MyObject> myList = new List<MyObject>();
private void MyButton_Click(object sender, EventArgs e)
{
myDataGridView.DataSource = null;
myList = new List<MyObject>();
myDataGridView.DataSource = myList;
myDataGridView.Refresh();So I can't get any additional info to work from.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
ok, thanks. I will try.
-
Hi, I have a weird problem, hopefully someone can shine a light on it? The user gets a list of defects on a product from the database. I put them in a List, like so:
while (rdr.Read())
{
DrumDefect defect = new DrumDefect()
{
AVI_Defect = new AVIDefect()
{
SequenceNumber = (rdr["AVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["AVIProdUDefectSeqNr"].ToString()) : (rdr["MVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["MVIProdUDefectSeqNr"].ToString()) : -1,
Code = (rdr["AVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["AVIProdUDefectCode"].ToString(),
Name = (rdr["AVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["AVIProdUDefectName"].ToString(),
Severity = (rdr["AVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["AVISeverityLevelID"].ToString())
},
MVI_Defect = new Defect()
{
Code = (rdr["MVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["MVIProdUDefectCode"].ToString(),
Name = (rdr["MVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["MVIProdUDefectName"].ToString(),
Severity = (rdr["MVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["MVISeverityLevelID"].ToString())
},
FVI_Defect = new Defect()
};
list.Add(defect);
}Then I import them in a datagridview like so:
\_SelectedDrum.Defects = database.GetList(); dataGridViewDefects.DataSource = null; dataGridViewDefects.DataSource = \_SelectedDrum.Defects; dataGridViewDefects.Refresh();
The user can add a defect, so I add one like this:
DrumDefect defect = new DrumDefect() { AVI\_Defect = new AVIDefect() { SequenceNumber = -1, Code = "# 0", Name = "NODEFECT", Severity = DefectSeverity.NODEFECT }, MVI\_Defect = new Defect() { Code = "# 0", Name = "NODEFECT", Severity = DefectSeverity.NODEFECT }, FVI\_Defect = new Defect() }; \_SelectedDrum.Defects.Add(defect);
it all works fine, except when the database list is empty, the user adds a def
That looks like a bug in the framework. According to this post from 2011[^], you should be able to work around it by creating a new list if the existing list is empty when you add the new item:
if (_SelectedDrum.Defects.Count == 0)
{
_SelectedDrum.Defects = new List<DrumDefect> { defect };dataGridViewDefects.DataSource = null; dataGridViewDefects.DataSource = \_SelectedDrum.Defects; dataGridViewDefects.Refresh();
}
else
{
_SelectedDrum.Defects.Add(defect);
}Or there's this StackOverflow answer[^] which suggests using a
BindingList<T>
instead of aList<T>
. If you can reproduce the problem with a simple example application, it might be worth reporting the bug on Connect[^] to see if it can be fixed in a future version.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
That looks like a bug in the framework. According to this post from 2011[^], you should be able to work around it by creating a new list if the existing list is empty when you add the new item:
if (_SelectedDrum.Defects.Count == 0)
{
_SelectedDrum.Defects = new List<DrumDefect> { defect };dataGridViewDefects.DataSource = null; dataGridViewDefects.DataSource = \_SelectedDrum.Defects; dataGridViewDefects.Refresh();
}
else
{
_SelectedDrum.Defects.Add(defect);
}Or there's this StackOverflow answer[^] which suggests using a
BindingList<T>
instead of aList<T>
. If you can reproduce the problem with a simple example application, it might be worth reporting the bug on Connect[^] to see if it can be fixed in a future version.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
very interesting, thanks a lot!
-
Hi, I have a weird problem, hopefully someone can shine a light on it? The user gets a list of defects on a product from the database. I put them in a List, like so:
while (rdr.Read())
{
DrumDefect defect = new DrumDefect()
{
AVI_Defect = new AVIDefect()
{
SequenceNumber = (rdr["AVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["AVIProdUDefectSeqNr"].ToString()) : (rdr["MVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["MVIProdUDefectSeqNr"].ToString()) : -1,
Code = (rdr["AVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["AVIProdUDefectCode"].ToString(),
Name = (rdr["AVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["AVIProdUDefectName"].ToString(),
Severity = (rdr["AVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["AVISeverityLevelID"].ToString())
},
MVI_Defect = new Defect()
{
Code = (rdr["MVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["MVIProdUDefectCode"].ToString(),
Name = (rdr["MVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["MVIProdUDefectName"].ToString(),
Severity = (rdr["MVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["MVISeverityLevelID"].ToString())
},
FVI_Defect = new Defect()
};
list.Add(defect);
}Then I import them in a datagridview like so:
\_SelectedDrum.Defects = database.GetList(); dataGridViewDefects.DataSource = null; dataGridViewDefects.DataSource = \_SelectedDrum.Defects; dataGridViewDefects.Refresh();
The user can add a defect, so I add one like this:
DrumDefect defect = new DrumDefect() { AVI\_Defect = new AVIDefect() { SequenceNumber = -1, Code = "# 0", Name = "NODEFECT", Severity = DefectSeverity.NODEFECT }, MVI\_Defect = new Defect() { Code = "# 0", Name = "NODEFECT", Severity = DefectSeverity.NODEFECT }, FVI\_Defect = new Defect() }; \_SelectedDrum.Defects.Add(defect);
it all works fine, except when the database list is empty, the user adds a def
When modifying the contents of "list controls", you have to be sensitive to where the "selected index" and "selected item" wind up. Often, it needs to be "reset" to an existing item (if there are any) from "-1" (after some operation); which signifies no items OR "nothing selected". (In WPF, for example, the "selected event" will fire during control initializing; which can foul up your app if you don't anticipate it).
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal