I have small form with one dropbox, one textBox, two ComboBoxes and one list(for selection).
Code has no errors, but when I attempt to save the form and store data in database I get this:
System.InvalidCastException: 'Unable to cast object of type
'Windows.UI.Xaml.Controls.ComboBoxItem' to type
'System.IConvertible'.'
+ $exception {System.InvalidCastException: Unable to cast object of type 'Windows.UI.Xaml.Controls.ComboBoxItem' to type 'System.IConvertible'.
at System.Convert.ToInt32(Object value)
Here is the code:
Db_Helper.InsertData(new
Data((Convert.ToString(listBox1.SelectedItem)),
(Convert.ToString(SelectedProductName.Text.Trim())),
(Convert.ToInt32(TextBox1.Text)),
//error below
(Convert.ToInt32(ComboBox1.SelectedItem)),
(Convert.ToString(ComboBox2.SelectedItem))));
I believe the problem is with ComboBox1 Conversion.
You do your conversion wrongly. 'Windows.UI.Xaml.Controls.ComboBoxItem' is a usual CLR Object as everything in the .NET. Each object ships overloadable .ToString() method, so whenever you want default string representations of whatever object or it's subclass, you rather call myObject.ToString().
Same applies to the ToInt32(...). Proceed on .int.Parse/TryParse to get strongly typed integer from the string. When you'll do this, you'll get a clearer error at least.
I think your casting syntax is wrong to convert into string we use toString method at the end of the statement like below.
ComboBox2.SelectedItem.toString();
I Hope This Will Help You
Or instead of SelectedItem use Text property of combobox like below
ComboBox2.Text
and you don't have to convert it into string it always returns string
I Hope This Will Help You
I got the solution myself.
The error was indeed at this line:
(Convert.ToInt32(ComboBox1.SelectedItem)),
I had to modify it a bit like this:
(int.Parse(ComboBox1.SelectedIndex.ToString())),
I was getting errors until I changed
SelectedItem
to
SelectedIndex
. Program works well now and I am able to pull selected data from ComboBox.
The SelectedItem property gives you the ComboBoxItem. What you want is only the value, so use the SelectedValue property instead.
You wanted to convert the value to an integer, but the Convert.ToInt32() takes a string while the SelectedValue property is an object. You can use the .ToString() on it to convert it to string.
Also don't convert string values (like SelectedProductName.Text) to string, that's redundant.
Db_Helper.InsertData(new
Data(listBox1.SelectedValue.ToString(),
SelectedProductName.Text.Trim(),
Convert.ToInt32(TextBox1.Text),
Convert.ToInt32(ComboBox1.SelectedValue.ToString()),
ComboBox2.SelectedValue.ToString());
EDIT You need to bind your ComboBoxes and ListBox properly using the DisplayMemberPath and SelectedValuePath properties. The first one will display the items' text, and the second one will store the items' ID. Then you can get the ID using the SelectedValue property in the code above.
Here is an example of how to bind the ComboBox to a list of vegetables sorted alphabetically:
var data = new Dictionary<int, string>{
{100, "Eggplant"},
{102, "Onion"},
{300, "Potato"},
{105, "Tomato"},
{200, "Zuccini"}
};
ComboBox1.ItemsSource = data;
ComboBox1.DisplayMemberPath = "Value";
ComboBox1.SelectedValuePath = "Key";
Your ComboBox1 in XAML must have no items. As you can see, I bound the DisplayMemberPath to the Value of the Dictionary and the SelectedValuePath to the Key. You can use any values you want for IDs and they don't have to be sequential like the index. This way, if you add a new item in the future, the IDs of the current items will not change. Let say you want to add Garlic in the alphabetical order:
var data = new Dictionary<int,string>{
{100, "Eggplant"},
{302, "Garlic"},
{102, "Onion"},
{300, "Potato"},
{105, "Tomato"},
{200, "Zuccini"}
};
If you use int for the Key, you don't need to convert it like you did in your code, you can simply use a direct cast. Similarly, for listBox1 and ComboBox2 you can use string for the Key if you like and you won't need conversion:
Db_Helper.InsertData(new
Data((string)listBox1.SelectedValue,
SelectedProductName.Text.Trim(),
Convert.ToInt32(TextBox1.Text),
(int)ComboBox1.SelectedValue),
(string)ComboBox2.SelectedValue);
If you prefer to keep the items in XAML instead of code-behind, there is no equivalent to the SelectedValuePath property, but you can simulate it using the Tag property like this:
<ComboBox x:Name="ComboBox1" SelectedValuePath="Tag">
<ComboBoxItem Content="Eggplant" Tag="100" />
<ComboBoxItem Content="Onion" Tag="102" />
<ComboBoxItem Content="Potato" Tag="300" />
<ComboBoxItem Content="Tomato" Tag="105" />
<ComboBoxItem Content="Zuccini" Tag="200" />
</ComboBox>
I'm having problems with a datagridview. I'm trying to export the row selection of datagridview1 in a array to be used in a chart in the same solution. I need to convert those id's to the actual collection name. I tried with a enum list to convert the id to the collection name, but compiler gives me a error so in the end I used a case switch. Is enum the correct way to go to convert an id to a collection name or should I follow a different road?
e.Graphics.DrawLine(Pen, position1, position2(Enum.GetName(typeof(collectioname),idofcollectioname), positionx2, positiony2));
I am not sure if I got your question but,
I need to convert those id's to the actual collection name
If you mean by Id some value that can be one of Limited Set of values, the answer is YES, you can use an Enum.
But if the Id something else, the answer is NO, you can't use it.
I have created a checkbox list type of data type in umbraco. see below screen shot
I am able to get the text value of this data type which is "All" in this case. But I am unable to get the Value in code.
umbraco.library.IProperty property = node.GetProperty("countries");
string val = property.Value;
I also looked at all the property fields but Value of this is not available.
Any one has any idea
I am using Umbraco 6.2.4
Looks like you should be using the GetPreValueAsString for your soultion.
string val = umbraco.library.GetPreValueAsString(node.GetProperty<int>("countries"));
Hi I have windows form with a Combo filled with 3 options where their values are Guid's and that has a DataBinding to a selected object.
EmployerMemberDefault item = EmployerMemberDefault();
item.GroupUid = Guid.Empty;
cbGroupEmployer.DataBindings.Clear();
cbGroupEmployer.DataBindings.Add("Value", item, "GroupUid");
When I create a new object the property that is databinded to the combo is set to Guid.Empty. If I compile and run, when I create that new object I see "00000000-0000-0000-0000-000000000000" in the combo. Is there any way I can see empty text in the combo instead of the Guid.Empty value?
Thanks.
Change the source property from Guid to Guid?/Nullable<Guid> I haven't done this with Guid, but it works with int/DateTime and other types that do not allow null.
"00000000-0000-0000-0000-000000000000" is what Guid.ToString returns. To show something different you'd have to translate that value in some way--i.e. bind to something else that stores the guid and translates it.
Hello I have an object which has value "B11" i.e
object data = "B11";
i have a variable called string resultCell. How do I get B11 into resultCell.
it doesn't seem to work for me in c#. I have tried things like ....
resultCell = (string)data;
resultCell = Convert.ToString(data);
More information
I am using excel services to get a value in a cell. the excel services returns a object[]. Its a single element though. I want the value in a string. i.e. B11 in the object as string.
Much of the time, calling data.ToString() will work. Without the specific type of the data object, there isn't any way to answer the question.
Is the value stored in a property of data?
Edit: Could you try (string)object[i], where i is the index of the particular piece of data you want to retrieve? If you need every value in the array, you'll probably want to use a foreach loop.
i think you need to just assign value as data.ToString() to cell.
hope this work for you.