views:

217

answers:

3

I am attempting to scroll through a database of Album Names using ListView. When I initially select an album, _SelectedIndexChanged returns the appropriate album; however in subsequent attempts to select an album, ListView returns a System.ArgumentOutOfRangeException - InvalidArgument=Value of '0' is not valid for 'index'. Could someone please steer me in the right direction as to what can be done to avoid this error?

private void ScrollThroughAlbums()
    {
      string selectStatement = "SELECT * FROM Albums ORDER BY Artist";
      OleDbCommand selectCommand = new OleDbCommand(selectStatement, oleDatabaseConnectionString);
      OleDbDataReader myReader = selectCommand.ExecuteReader();
      ColumnHeader columnHeader1 = new ColumnHeader();
      ColumnHeader columnHeader2 = new ColumnHeader();
      ColumnHeader columnHeader3 = new ColumnHeader();

      columnHeader1.Text = "Album";
      columnHeader2.Text = "Artist";
      columnHeader3.Text = "Tracks";

      listView1.Columns.Add(columnHeader1);
      listView1.Columns.Add(columnHeader2);
      listView1.Columns.Add(columnHeader3);
      listView1.Columns[0].Width=130;
      listView1.Columns[1].Width=130;

      listView1.View = View.Details;
      listView1.AllowColumnReorder=true;
      listView1.FullRowSelect=true;
      listView1.GridLines=true;
      listView1.MultiSelect = false;
      listView1.Sorting= SortOrder.Ascending;

      while (myReader.Read())
      {
         string frontCoverXML = myReader.GetString(3).ToString();
         string Artist = myReader.GetString(1).ToString();
         string Album = myReader.GetString(2).ToString();
         string TracksXML = myReader.GetString(4).ToString();
         ListViewItem item = new ListViewItem(new []{Album,Artist}); 
         listView1.Items.Add(item); 

        } myReader.NextResult();
        myReader.Close();
    }

   private void listView1_SelectedIndexChanged(object sender,EventArgs e)
   {
      ListView.SelectedListViewItemCollection album = this.listView1.SelectedItems;
      MessageBox.Show(album[0].ToString());
   }
+1  A: 

The error indicates the event handler fired whilst there were no items selected so your call to album[0] failed as there were no items. check to see if the count property of albumis greater than zero before popping up your message box.

Ben Robinson
Ben - Thanks very much for your reply. As I wrote in my note to Volody above, I am hoping to identify the source of the OutOfRange error and correct the language to hopefully avoid the issue. Please let me know if you believe there could be any improvements to the language. Thanks again. Bill
Bill
Ben - I should have tried your suggestion before responding. As it turns out, testing alleviated the error situation (not sure how) and the code works perfectly. Thank you again. Bill
Bill
volody's solution is the correct one. SelectedIndexChanged event will fire whenever the selection is changed. You will get the error whenever the selection changes from something selected to nothing selected. As such you need to acount forthis posibility and volody's code does this, simply doing nothing when nothing is selected.
Ben Robinson
+2  A: 
   private void listView1_SelectedIndexChanged(object sender,EventArgs e)
   {
      ListView.SelectedListViewItemCollection album = this.listView1.SelectedItems;
      if(album.Count>0)
         MessageBox.Show(album[0].ToString());
   }
volody
Volody - Thank you very much for your reply. Although your suggested language would identify if an error condition exists and work-around the issue, I was hoping to determine the reason why the second use of listview would return the System.ArgumentOutOfRangeException error in the first place and correct the language. Please let me know if you should see any problem with the language included, and advise if you should have any suggestions. Thanks again. Bill
Bill
Volody - After responding to your suggestion, I tried your suggested language and found that your language handles the error (not sure how, but it works). Thank you for your help.Bill
Bill
+1  A: 

It's because, behind the scenes, the selected index change event is actually called three times in the switch from one item to another. First item A is selected, then no item, then item B. A count of the indexed items referenced is maintained at each stage. In the interim step the index count is 0 and so there is nothing to reference in the index check. Trying to refer to it returns the error. Checking the count skips that middle step.

Cavanuk