Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
int selcetedComboValue = Int32.Parse(comboBox1.SelectedItem.ToString());
Posted 12-Nov-12 0:49am
Edited 12-Nov-12 3:45am
v2
Comments
digimanus at 12-Nov-12 5:51am
   
have you debugged? what is the value of comboBox1.SelectedItem?
Mohd. Mukhtar at 12-Nov-12 6:02am
   
use int selcetedComboValue = Int32.Parse(comboBox1.SelectedItem.Value.ToString());
@AmitGajjar at 12-Nov-12 6:06am
   
What exactly you want to do ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As the error message has already suggested you, the input is not correct and it can not be converted in to an int.
 
The best thing you can do is put a break-point on this line of code and then see what is the value you have selected comboBox1.
 
As per your comments, you are having values like 1-3, 5-6. If you want both values, you will have to have it in two different integer variables as shown below
 
string[] splitValues = Int32.Parse(comboBox1.SelectedItem.Split('-');
int firstValue = Int32.Parse(splitValues[0]);
int firstValue = Int32.Parse(splitValues[1]);
 
Hope that helps.
Milind
  Permalink  
v2
Comments
vinayak chavan at 12-Nov-12 5:57am
   
0
Milind Thakkar at 12-Nov-12 5:59am
   
If it is NOT integer i.e. 1-3 and 5-6, how can you convert to integar?
vinayak chavan at 12-Nov-12 5:59am
   
what i hav to do for that?
digimanus at 12-Nov-12 6:04am
   
has your combobox a SelectedIndex?
Milind Thakkar at 12-Nov-12 6:45am
   
Well if customer selects 1-3, what is the value you want ? 1 or 3 ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi Vinayak,
 
Please note that you as per your input you are conveting wrong value into integer.
 
Your combobox values are in the below form
 
1-5
6-10
11-20
 
to convet this value into integer you need to split these value into two part by separetor "-". And then you can convert this value into integer.
 

But the question is what value you want from this combobox.
  Permalink  
v2
Comments
vinayak chavan at 12-Nov-12 6:29am
   
i need 1-5 from combobox
digimanus at 12-Nov-12 6:51am
   
do you use a multiselect combobox?
Mohd. Mukhtar at 12-Nov-12 6:35am
   
you can get this value from the combobox as below.
string selectedValue = comboBox1.SelectedItem.ToString();
 
But you can not convert it directly into integer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Remove all non-int characters from the string you are converting. If you have a ComboBox item which contains non-int characters but you need the int part then use a character array and passing the character array(of the string to be converted) remove the non-int characters and store the final string to be converted in a new variable. Then convert this new string you got containing only int-characters.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi
 
try
 
 int selcetedComboValue = Convert.ToInt32(comboBox1.SelectedItem.ToString());
  Permalink  
Comments
vinayak chavan at 12-Nov-12 6:06am
   
same error :
Anele Ngqandu at 12-Nov-12 7:04am
   
int selcetedComboValue = Convert.ToInt32(comboBox1.SelectedValue.ToString());

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 12 Nov 2012
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