Click here to Skip to main content
15,745,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi folks, I have a ListView that contains customer name, address and email address. I need to find the email address and then populate a textbox.

Code below but not working error message;

An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code
Additional information: Index was out of range. Must be non-negative and less than the size of the collection

My code so far...

int idx = ListView1.SelectedIndex;
TextBox GarageEmail.Text = (TextBox)this.ListView1.Items[idx].FindControl("SupplierEmailAddress");
Karthik_Mahalingam 27-Apr-15 7:35am    
your so far code is wrong.
what is SupplierEmailAddress ?
post your aspx code also

1 solution

SelectedIndex is "The zero-based index of the selected item in a ListView control. The default is -1, which indicates that no item is currently selected." (from MSDN[^])...So you should check if it is NOT -1 to be sure that any item has been selected...
if(ListView1.SelectedIndex != -1)
  int idx = ListView1.SelectedIndex;
  TextBox GarageEmail.Text = (TextBox)this.ListView1.Items[idx].FindControl("SupplierEmailAddress");

Now for the second line...It is completely wrong? The left side part if the text property of a TextBox (not clear from where it comes), where the right side is a TextBox control...You probably meant to do something like this:
TextBox GarageEmail = (TextBox)this.ListView1.Items[idx].FindControl("SupplierEmailAddress");
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900