Click to See Complete Forum and Search --> : ADO.net ListView in C#


engotpacs
March 23rd, 2010, 11:30 PM
Hi Guys... need some help.. I have this code..

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{

MessageBox.Show(listView1.SelectedItems[0].SubItems[0].Text);

}

and I always get this kind of error message evry time I change the selected Item im my listbox...

Argumentoutofrangeexception
InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index

i need a way to get each value for the selected item in my listbox so that I can use it as a parameter for my sqlcommand.

need help.. =)

boudino
March 24th, 2010, 03:04 AM
Check SelectedItems and SubItems if they contain something, because [0] means first element, which may not be presented (e.g. if the list is empty).

nelo
March 24th, 2010, 04:06 AM
Just to add to what boudino has said...you should run it through the debugger and inspect the contents of 'SelectedItems' and 'SubItems' at runtime. They may not be what you expect. If there is any chance those collections could be empty you should put a test around access to the values (e.g. using an 'if' statement...)

engotpacs
March 25th, 2010, 12:42 AM
Ok I'l go and check it out...

engotpacs
March 25th, 2010, 02:12 AM
why is it that every time I change the selected item in my listview using the SelectedIndexChanged event, it resets its value to nothing?