views:

112

answers:

9

An exception occured. Exception is specified cast is not valid

int s = (int)comboBox1.SelectedItem;
A: 
int s = comboBox.SelectedIndex
CommanderZ
What is your point?
leppie
@leppie I wouldn't be much surprised if sheena meant to get the index of the selected item, not the value of the selected item itself. So this suggestion makes sense, even if it is an assumption (which you're almost forced to make, seeing as how little information the question contains).
Rob
@Rob: You are making an assumption, I am not. What is wrong with getting an `int` out of the selected item? Nothing!
leppie
If the OP wanted to get the int out of SelectedItem, he would most likely be aware he needs to do some kind of conversion. In my eyes it is pretty obvious he wants the index of the selected item.
CommanderZ
@leppie of course I'm making an assumption, but I've explained why.
Rob
+3  A: 

It means the value in the combobox's items is not an int.

leppie
This answer is absolutely correct, but useless. The OP still doesn't know what he should do. I'd prefer those voted-down answers, which is more help to a beginner.
Danny Chen
@Danny: well this question is completely bloated with tangent discussions by now, so I'm not sure that OP will be able to distill anything of value out of it, but ultimately, the accept tick is the only marker of what OP found most helpful, so I'm sure that this will sort itself out. Having said that, I do agree that the assumptions made in the voted-down answers are not senseless ones. It's always good practice to point out when you're making an assumption, though: "Hey, if what you want to do is *A* then you could do, this, if you really meant *B* then ..."
David Hedlund
this not help full to sheena at all..............
Pranay Rana
A: 

try

int s = int.Parse(comboBox1.SelectedItem.ToString());

you can not convert any object to an int by just casting. If you have a string you need to use int.Parse() to convert a string to an int.

If you insert your own objects as Items in the combobox you can cast comboBox1.SelectedItem to your type instead.

ComboBox.SelectedItem.ToString() only returns the content if you have inserted string objects in the combobox, a more reliable way is to check the ComboBox.Text property instead. This will also save you from some null checking.

Albin Sunnanbo
`SelectedItem.ToString()` will yield something like `System.Net.UI.Controls.ComboBoxItem.OrWhatever` which cannot be parsed to an int. This won't work at all.
David Hedlund
@David: If the content is strings it will work. I'l edit my post to recommend comboBox1.Text instead.
Albin Sunnanbo
@David Hedlund: What are you talking about? There is no combobox in ASP.NET.
leppie
@leppie: hence the `OrWhatever`. I didn't bother to look at exactly where the class belonged, because that wasn't my point at all.
David Hedlund
@Albin: yeah, with `.Text` I don't have a problem with your solution at all =)
David Hedlund
@David Hedlund: It's a DropDownList in ASP.NET. So we are dealing with WinForms.
leppie
@leppie: yes, I gathered. That doesn't change the fact that `ComboBox.Text` is the more reliable and semantically correct way of getting the text of the combo box.
David Hedlund
+1  A: 

Check SelectedIndex > -1 or SelectedItem != null

Fredrik Leijon
And as @leppie says it does assume that all values in the comboBox are ints
Fredrik Leijon
A: 

You're trying to cast a ComboBox Item to an int.

Try int s =(int) comboBox1.SelectedIndex if you want the index of the item.

Brissles
Why -1? I don't think @Brissles is wrong, except a flaw: `s = (int) comboBox1.SelectedIndex`, no `(int)` is needed because `SelectedIndex` is an `int`.
Danny Chen
@Danny: the -1 is probably because somebody's crusading against the assumption that `SelectedIndex` is what OP was trying to get at all. I don't have a problem with the assumption myself. Given the complete lack of an actual question, I guess we're all at liberty to fill in the blanks here.
David Hedlund
@David: Exactly. Some OPs don't describe their questions well, it's not because they are lazy or they don't want to. The reason is probably they are not familiar with the issue. So I think a good answer should point out the error, and giving a possible solution is much more important.
Danny Chen
A: 

If you want to get the value that is displayed in the combo box, maybe you should try :

int s = (int)comboBox.SelectedValue;
LouG
That all depends on how you databind the ValueMember. But if left default and empty, it should work. But if selected item fails, this will also fail.
leppie
Yes. But are we supposed to guess this kind of details...?
LouG
A: 

You require to cast your comboBox1.SelectedItem in the type with which your binding

by default its of Type Object

For example

System.Data.DataSet ds = new System.Data.DataSet();
ds = Database.Read("select * from [TaskTimer$]");

cbClient.SelectedIndex = -1;
cbClient.DataSource = ds.Tables[0];
cbClient.DisplayMember = "CLIENTS";
cbClient.ValueMember = "CLIENTS";
...
... The user can do stuff like select a Client from the ComboBox DropDown
...
string sSelectedClient = cbClient.SelectedItem.ToString();

As show above I bound datatable with the item so that it gives me error when I am qurying data directly

so to get the item I have to cast like as below

  string sSelectedClient = ((DataRowView)cbClient.SelectedItem).Row("CLIENTS")

hope that you will get proper idea by above example

Pranay Rana
A: 

Example usage on MSDN here

But really, you need to provide more details in the question :)

Total guess but a common thing to do may be to store a database id in a comboboxes value property and the database item text in the text property. If this is what you are doing then you can use the below syntax if you know for certain that the value of the combobox is always castable to an int.

int i = (int)ComboBox1.SelectedValue.ToString();

or if your not sure it's always an int you can...

try
{
int i = int.Parse(ComboBox1.SelectedValue.ToString());
}
catch
{
//handle the non int situation here
}

or

int i;
bool result = int.TryParse(ComboBox1.SelectedValue.ToString(), out i);

            if (result)
            {
                //you can use the variable i now
            }
            else
            {
                //The parse failed so handle a non int situation here
            }
Rich Andrews
A: 

Try Convert.ToInt32(combo.Items[combo.SelectedIndex].Value.ToString());

KMan