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());
}