WhatupE
asked on
Index out of range for listBox.selectedIndex
I have a table called Runs with columns RunID and RunName. I have a comboBox (comboBox1) and an arrayList (al2). The comboBox already has 3 items. I add two items to the comboBox (and the arrayList) in another function. Then I delete the two added items. When I delete the first added one (fourth overall), everything is fine. But when I delete the second added one (last in the list), I get the error message:
An unhandled exception of type 'System.ArgumentOutOfRange Exception' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
The message points to the arrowed line, however I beleive the error is occuring in the line before it.
private void LoadRuns(){
string strQry="Select RunID, RunName from Runs Order By RunID";
comboBox1.DataSource = null;
al2.RemoveRange(0, al2.Count);
myConnection.Open();
System.Data.OleDb.OleDbCom mand myCommand = new System.Data.OleDb.OleDbCom mand(strQr y, myConnection);
System.Data.OleDb.OleDbDat aReader myReader = myCommand.ExecuteReader();
while(myReader.Read())
{
TestObj obj1 = new TestObj(Convert.ToInt16(my Reader[0]) , Convert.ToString(myReader[ 1]));
//listBox1.Items.Add(myRea der[0]);
al2.Add(obj1);
}
myReader.Close();
comboBox1.DataSource = al2;
comboBox1.DisplayMember = "Value";
//MessageBox.Show(comboBox 1.Items.Co unt.ToStri ng());
comboBox1.SelectedIndex = 0;
myConnection.Close(); <---------------
}
Any help is greatly appreciated
An unhandled exception of type 'System.ArgumentOutOfRange
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
The message points to the arrowed line, however I beleive the error is occuring in the line before it.
private void LoadRuns(){
string strQry="Select RunID, RunName from Runs Order By RunID";
comboBox1.DataSource = null;
al2.RemoveRange(0, al2.Count);
myConnection.Open();
System.Data.OleDb.OleDbCom
System.Data.OleDb.OleDbDat
while(myReader.Read())
{
TestObj obj1 = new TestObj(Convert.ToInt16(my
//listBox1.Items.Add(myRea
al2.Add(obj1);
}
myReader.Close();
comboBox1.DataSource = al2;
comboBox1.DisplayMember = "Value";
//MessageBox.Show(comboBox
comboBox1.SelectedIndex = 0;
myConnection.Close(); <---------------
}
Any help is greatly appreciated
ASKER
They didn't fix the error...I am running 2003. The message box always displayed the correct # of items, which was always positive, so the if statement you have will always be true. The method is filled because instead of writing new code after i delete the item from the database, I just reload everything. Meaning I rerun the query, reload the arrayList, and reset the comboBox's datasource. Should I be doing something else? The code that calls this function is here:
DeleteRun(){
myConnection.Open();
string strDel="Delete from Runs Where RunName = '" +comboBox1.Text+ "'";
System.Data.OleDb.OleDbCom mand myCommand = new System.Data.OleDb.OleDbCom mand(strDe l, myConnection);
myCommand.ExecuteNonQuery( );
string strQry="Select RunID from Runs Where RunName ='" +comboBox1.Text+ "'";
System.Data.OleDb.OleDbCom mand myCommand2 = new System.Data.OleDb.OleDbCom mand(strQr y, myConnection);
System.Data.OleDb.OleDbDat aReader myReader = myCommand2.ExecuteReader() ;
if(myReader.HasRows)
{
myReader.Read();
string strDel2 = "Delete From TestRun Where RunID =" +Convert.ToInt16(myReader[ 0]);
System.Data.OleDb.OleDbCom mand myCommand3 = new System.Data.OleDb.OleDbCom mand(strDe l2, myConnection);
myCommand3.ExecuteNonQuery ();
}
myReader.Close();
myConnection.Close();
LoadRuns();
}
DeleteRun(){
myConnection.Open();
string strDel="Delete from Runs Where RunName = '" +comboBox1.Text+ "'";
System.Data.OleDb.OleDbCom
myCommand.ExecuteNonQuery(
string strQry="Select RunID from Runs Where RunName ='" +comboBox1.Text+ "'";
System.Data.OleDb.OleDbCom
System.Data.OleDb.OleDbDat
if(myReader.HasRows)
{
myReader.Read();
string strDel2 = "Delete From TestRun Where RunID =" +Convert.ToInt16(myReader[
System.Data.OleDb.OleDbCom
myCommand3.ExecuteNonQuery
}
myReader.Close();
myConnection.Close();
LoadRuns();
}
Why do yo make two delitions? Just asking.... You could do it with only the first one....
You could simplify (and also fasten) the application if you just make:
comboBox1.Items.Remove (comboBox1.SelectedItem);
This has no call to the database, so it should be faster.
Otherwise try changing this:
al2.RemoveRange(0, al2.Count); -> al2.Clear();
This is a problem, because the range is actually from 0 to al2.Count - 1!
Try it and post again
You could simplify (and also fasten) the application if you just make:
comboBox1.Items.Remove (comboBox1.SelectedItem);
This has no call to the database, so it should be faster.
Otherwise try changing this:
al2.RemoveRange(0, al2.Count); -> al2.Clear();
This is a problem, because the range is actually from 0 to al2.Count - 1!
Try it and post again
ASKER
I tried that last nite after I posted...no effect. I am doing deletions to two different tables...the second of which is not important to this question. And I can't just remove the item because it is connected to an arrayList as it's dataSource, and your statement only deletes from the comboBox.
However, I have found a solution this morning. At the beginning of my LoadRuns() function, I set the selectedIndex to -1. That's it.
But thanks again for your help TheAvenger.
However, I have found a solution this morning. At the beginning of my LoadRuns() function, I set the selectedIndex to -1. That's it.
But thanks again for your help TheAvenger.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi WhatupE:
> comboBox1.SelectedIndex = 0;
> myConnection.Close(); <---------------
Can only give you limited help here, since I am more a VB.NET person than a C# person
First of all, I would recomment you use try..catch.. end try (or the c# equivalent to it)
I have the feeling the error is not in LoadRuns but in comboBox1_SelectedIndexCha nged
Are you handling that event?
When your code calls comboBox1.SelectedIndex = 0, it triggers that event, and might be causing your error.
Since you are not try catching there, .NET shows the error to be in the next statement.
Dabas
> comboBox1.SelectedIndex = 0;
> myConnection.Close(); <---------------
Can only give you limited help here, since I am more a VB.NET person than a C# person
First of all, I would recomment you use try..catch.. end try (or the c# equivalent to it)
I have the feeling the error is not in LoadRuns but in comboBox1_SelectedIndexCha
Are you handling that event?
When your code calls comboBox1.SelectedIndex = 0, it triggers that event, and might be causing your error.
Since you are not try catching there, .NET shows the error to be in the next statement.
Dabas
You should remove the item in combobox before deleting from object or database object that combobox binding to. And if reload automatically, you should check if after delete, binded object has no item then you clear the datasource property of combobox control. So i think the problem will be done.
What did the line:
MessageBox.Show(comboBox1.
show you? you could try to change the line with the selected index to:
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = 0;
Then you will not have a crash but maybe you have another problem with the logic. Why is this method (which filles in the combo box) called when you remove an item?