Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access a dictionary outside of a method
    primarykey
    data
    text
    <p>Actually I have two question.<br/></p> <p>I have a method named <code>FillPersonList</code> which is always loaded when form start. Inside that method I access the database and put required value inside a dictionary. Previously, I can't reload the related listbox when new value is added. So I create a refresh button and when it is clicked it make that listbox datasource to null and call that <code>FillPersonList</code> method again. Well it works fine.<br/></p> <p>Now I add a textbox. When user type a word, listbox should sort itself according to that word. I do not wish to access if every time user type a word. So I declared previous dictionary outside of that method and access that method inside textBox changed method. OK, now it also work but after typing if i clear that textbox listbox only show the last value. And second problem is now if I click on refresh button It shows an "<code>ArgumentException</code>" error.<br/></p> <p><strong>Sample code:</strong><br/></p> <pre><code>Dictionary&lt;int, string&gt; personListDictionary = new Dictionary&lt;int, string&gt;(); private void FillPersonList() { //connect to database SqlCommand prsonListCmd = new SqlCommand(SQL_QUERY, conn); SqlDataReader dr = prsonListCmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { //Access value personListDictionary.Add(key, value); //Here ArgumentException occurs } } PersonList.DisplayMember = "Value"; PersonList.ValueMember = "Key"; PersonList.DataSource = new BindingSource(personListDictionary, null); } private void sortListBox_TextChanged(object sender, EventArgs e) { //Access personListDictionary value } </code></pre> <p>I need only one solution. If I have to put dictionary inside <code>FillPersonList</code> method how can I access its value. Or if keep it as it is now how reload database without exception error.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload