1

I'm currently working on a project in winforms and run into a little trouble with setting the selected item of a populated ListBox. Basically, I have an SQL database which holds what I guess could be called meta-data about individual clients, one of the fields being the server and name of the database all of their data is in. In addition to this, I have a form where I would like the user to be able to update the client information. There is a dropdown list of clients on this form and when the user selects a client, the form is populated with their data. One of the pieces that is populated is a ListBox of databases on the same server as their currently selected database. I would then like to select the database on that list that corresponds to the selected client, but I get an "Array index out of bounds" exception. Here's what the code basically looks like:

//getClientInfo returns all of the fields for the selected client from the database
//getDatabases returns the database names using "SELECT name FROM master.sys.databases"
Dictionary<string, object> clientData = getClientInfo(clients_ListBox.SelectedItem);
server_ComboBox.SelectedItem = clientData["Server"].ToString();
databases_ListBox.DataSource = getDatabases("Data Source=" + server_ComboBox.Text + "; Trusted_Connection=yes");
.
.
.    -----set other fields-----
.
.
 databases_ListBox.SelectedItem = clientData["Database"].ToString();

And then I get an exception on the last line. The weird thing is that this doesn't seem to occur in Debug mode, as well as when the client before I switch and the client I switch to are on the same server. If they are on different databases, I get the exception, otherwise it's fine. Could anyone help explain this to me? I can only think of some sort of timing issue, where the item is being set before the ListBox finishes being populated, but I'm not using any sort of threading, so I have no idea what's going on.

In typing this, I realized that it might be smarter to have the connection string use the retrieved client data instead of the ComboBox text, but I would still like it if someone could venture a guess as to why I'm having this problem. Thanks a lot!

4
  • In which line do you get this exception? Probably you got the error the line that is clientData["Server"].ToString(); assignment. Am I right? Nov 20, 2013 at 3:25
  • Yes, databases_ListBox.SelectedItem = clientData["Database"].ToString(); is where the exception is thrown.
    – Mac
    Nov 20, 2013 at 13:24
  • The ListBox.SelectedItem wants you to point to 'the' object that you have that 'is' the same as what is stored in the ListBox, not the database entry that corresponds to it/is derived from. My advice is to use SelectedIndex, as long as there would be no major requirement changes to accommodate this. Else, you may have to loop through your items to find the content that matches clientData["Database"] and then setting the SelectedItem from the item you are looping with. Nov 20, 2013 at 13:32
  • I tried doing databases_List.SelectedIndex = databases_List.FindString(clientData["Database"].ToString()); and it continues to give me the same error. I actually managed to force the error during debugging and the breakpoint showed me that when this usually runs, the selected index is initially set to the first element of the ListBox list of items. When the error occurs, the selected index is throwing an exception. Is this at all helpful?
    – Mac
    Nov 20, 2013 at 14:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.