Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Silverlight
Hello everyone,
 
I am working with listbox and image controls in silverlight 5. Trying to display a image in image control by selecting a image name in the listbox. Below is the code using in listbox_selection changed event. What could be the reason for this exception.
 
Code:
ImgesList.Source = 
new BitmapImage(new Uri(App.Current.Host.Source.OriginalString.Substring(0, App.Current.Host.Source.OriginalString.IndexOf("ClientBin")) + "Images/" + listBox1.SelectedItem));
 
Exception:
 
Length cannot be less than zero. Parameter name: length
 
Thanks in advance.
Posted 20-Mar-13 23:28pm
Edited 21-Mar-13 1:31am
_duDE_8.1K
v2
Comments
Richard MacCutchan at 21-Mar-13 5:08am
   
The parameter name length does not appear in those two lines of code. Go back and check exactly where the exception is occurring.
bapu_reddy at 21-Mar-13 7:18am
   
This is the only statement using in "Listbox_selectionchanged" event, where do I go back and check.....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Add the following if statement before your code:
if(listbox1.SelectedIndex >= 0)
... // your normal code here
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This error occurs if the lenght of string in the method "Substring" is negative.
You can catch this exception like this:
 
try {
   string str = App.Current.Host.Source.OriginalString.Substring(0, App.Current.Host.Source.OriginalString.IndexOf("ClientBin");
}
catch (ArgumentOutOfRangeException e) {
   
}
 
That means, check whether your string have "ClientBin" at all:
 
int n = App.Current.Host.Source.OriginalString.IndexOf("ClientBin");
if (n > -1)
 ...
or something like this Wink | ;)
  Permalink  
v3

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

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 249
2 Sergey Alexandrovich Kryukov 205
3 Aajmot Sk 197
4 Sinisa Hajnal 176
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100