none
Specified cast is not valid error

    Question

  •  

    private void PopulateUUTComboBoxList()
            {
                try
                {
                    //Clear and populate the combobox of existing uuts
                    _uutTestScriptcmbox.Items.Clear();
                    List<UUT> uuts = new List<UUT>();
                    if (_tcc.GetUUTList(uuts) == DBResults.Success)
                    {
                        uuts.Sort();
                        _uutTestScriptcmbox.DataSource = uuts;
                        _uutTestScriptcmbox.DisplayMember = "Name";
                        _uutTestScriptcmbox.ValueMember = "ID";

                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Error(s) populating the UUT list: " + ex.Message);
                }
            }

     

     

    private void PopulateTCCComboBoxList()
            {
                try
                {

    //THIS IS WHAT'S CAUSING THE EX TO BE THROWN 
                    _currentTCC.UUT.ID = (int) _uutTestScriptcmbox.SelectedValue;
                    List<TestCellConfig> tccs = new List<TestCellConfig>();

                    if (_tcc.GetTCCHeaderList(_currentTCC.UUT.ID, tccs) == DBResults.Success)
                    {
                        tccs.Sort();
                        _testcellconfigTestScriptcmbox.DataSource = tccs;
                        _testcellconfigTestScriptcmbox.DisplayMember = "Name";
                        _testcellconfigTestScriptcmbox.ValueMember = "ID";
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Error(s) populating the Test Cell Configuration list: " + ex.Message);
                }

           }

     

     

      private void _uutTestScriptcmbox_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (_uutTestScriptcmbox.SelectedIndex != -1)
                    PopulateTCCComboBoxList();
            }

     

     

    This is the code that is causing my exception to be thown with the "Specified cast is not valid" error. After I press okay on my window form, the code works perfectly.

     

    **I'm new to the forum and didn't really know where to post, I really apologize if this in in the wrong forum.
         


    Tuesday, December 18, 2007 8:23 PM

Answers

  • Can you try something like:

     

     

    UUT selectedItem =  _uutTestScriptcmbox.SelectedItem as UUT;

    if(selectedItem != null)

    {

     _currentTCC.UUT.ID =  selectedItem.ID;

    List<TestCellConfig> tccs = new List<TestCellConfig>();

    //.... so on               

    }

    Wednesday, December 19, 2007 6:20 PM

All replies

  • please put a break point at:

    _currentTCC.UUT.ID = (int) _uutTestScriptcmbox.SelectedValue;

    debug the code and take a look at what
    _uutTestScriptcmbox.SelectedValue is, is it null or other data type?
    Tuesday, December 18, 2007 8:33 PM
  •  

    Okay, I entered my breakpoint, however, there is no _uutTestScriptcmbox.SelectedValue, only _uutTestScriptcmbox.SelectedItem, which gives me my first item in the combobox and the id is the id(2) that is assigned to that selection.
    Tuesday, December 18, 2007 8:48 PM
  • so _uutTestScriptcmbox.SelectedValue is null. what is the definition of UUT?

     maramckmc wrote:

     

    Okay, I entered my breakpoint, however, there is no _uutTestScriptcmbox.SelectedValue, only _uutTestScriptcmbox.SelectedItem, which gives me my first item in the combobox and the id is the id(2) that is assigned to that selection.
    Tuesday, December 18, 2007 8:55 PM
  •  

    UUT is a list that holds uuts, tccs are derived from the uuts. Therefore, for example, You may have 1, 2, 3 in the UUT list, then in the tccs, you have 4, which is derived from 2, 5, derived from 2, and 6, derived from 1.

     

    UUT   id

    1        a

    2        b

    3        c

     

    TCCs   UUTid

    4         b

    5         b

    6         a

     

    I know explanation is kinda iffy.

     

    I'm trying to get my list of UUTs in comboBox1, upon selecting one of my UUTs from that comboBox, it automatically populates my tccs in comboBox2 given the id from comboBox1.

    Tuesday, December 18, 2007 9:06 PM
  • make sure you handle the case when there's nothing selected.

    following code works for me.

            private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
            {
                if (comboBox1.SelectedIndex != -1)
                {
                    textBox1.Text = comboBox1.SelectedValue.ToString();
                }
            }
    Tuesday, December 18, 2007 9:40 PM
  • I added SelectedValueChanged, however adding textBox1.Text = comboBox1.SelectedValue.ToString(); only allows the number of corresponding UUTs to my comboBox instead of the actual names in the list.

     

    I am logging off, but would really like for you to reply. Thanks, be back tomorrow.

    Tuesday, December 18, 2007 9:54 PM
  • I thought that you only need that ID to populate your 2nd combbox? you dont have to use ToString(), that's just a way to display the value.

     maramckmc wrote:

    I added SelectedValueChanged, however adding textBox1.Text = comboBox1.SelectedValue.ToString(); only allows the number of corresponding UUTs to my comboBox instead of the actual names in the list.

     

    I am logging off, but would really like for you to reply. Thanks, be back tomorrow.

    Tuesday, December 18, 2007 10:09 PM
  • _currentTCC.UUT.ID = (int) _uutTestScriptcmbox.SelectedValue

    Why don't you do something like this:

    _currentTCC.UUT.ID = Convert.ToInt16 (_uutTestScriptcmbox.SelectedValue);

     

    I think the Combobox values are all strings, aren't they?

     

     

     

    Wednesday, December 19, 2007 1:40 AM
  • My problem wasn't displaying the value. The problem is that before I could get to my form, I was getting the "Specified cast is not valid" error. Once, I "ok" the pop-ups, my form comes up and my comboboxes performed perfectly.

    Wednesday, December 19, 2007 2:46 PM
  •  

    If I add that, I get the "Unable to cast object of type "" to type 'System.IConvertible'."
    Wednesday, December 19, 2007 2:49 PM
  • I think you need to add condition when it's not null, then cast it to int and populate combobox2 with the ID.

     maramckmc wrote:

     

    If I add that, I get the "Unable to cast object of type "" to type 'System.IConvertible'."
    Wednesday, December 19, 2007 2:53 PM
  • Okay, I've done a little more debugging and the selectedindex is null, causing my cast error.

    I need to figure out how to make my selectedindex a value or maybe test to make sure a value is already selected before populating my box. Any suggestions, b/c just adding _comboBox1.SelectedIndex = 0 is currently not working correctly. No matter which index I choose, it causes my indext to continue to be 0.

     

    Wednesday, December 19, 2007 3:09 PM
  • like I mentioned, just put

    if (_comboBox1.SelectedIndex != null)
    {
    //code to populate _comboBox2
    }

    since if nothing is selected in _comboBox1, you dont have the ID to populate 2 any way

     maramckmc wrote:

    Okay, I've done a little more debugging and the selectedindex is null, causing my cast error.

    I need to figure out how to make my selectedindex a value or maybe test to make sure a value is already selected before populating my box. Any suggestions, b/c just adding _comboBox1.SelectedIndex = 0 is currently not working correctly. No matter which index I choose, it causes my indext to continue to be 0.

     

    Wednesday, December 19, 2007 3:13 PM
  •  

    I have tried your suggestion and I still get the cast error b/f my form loads.

    I also get this warning "The result of the expression is always 'true' since a value of type 'int' is never equal to 'null' of type 'int?'

    Wednesday, December 19, 2007 3:21 PM
  • sorry, my mistake, it should be selectedIndex != 0 OR

    private void PopulateTCCComboBoxList()
            {

    if (_uutTestScriptcmbox.SelectedValue != null)
    {

                try


                {

                    _currentTCC.UUT.ID = (int) _uutTestScriptcmbox.SelectedValue;


                    List<TestCellConfig> tccs = new List<TestCellConfig>();

                    if (_tcc.GetTCCHeaderList(_currentTCC.UUT.ID, tccs) == DBResults.Success)


                    {


                        tccs.Sort();


                        _testcellconfigTestScriptcmbox.DataSource = tccs;


                        _testcellconfigTestScriptcmbox.DisplayMember = "Name";


                        _testcellconfigTestScriptcmbox.ValueMember = "ID";


                    }


                }


                catch (System.Exception ex)


                {


                    MessageBox.Show("Error(s) populating the Test Cell Configuration list: " + ex.Message);


                }

    }


           }

     



     maramckmc wrote:

     

    I have tried your suggestion and I still get the cast error b/f my form loads.

    I also get this warning "The result of the expression is always 'true' since a value of type 'int' is never equal to 'null' of type 'int?'

    Wednesday, December 19, 2007 5:33 PM
  • Can you try something like:

     

     

    UUT selectedItem =  _uutTestScriptcmbox.SelectedItem as UUT;

    if(selectedItem != null)

    {

     _currentTCC.UUT.ID =  selectedItem.ID;

    List<TestCellConfig> tccs = new List<TestCellConfig>();

    //.... so on               

    }

    Wednesday, December 19, 2007 6:20 PM
  •  

    When trying  selectedIndex != 0, I do not get the cast error, however, whenever selecting index0 in comboBox1, my populated comboBox2 list is not correct, I end up getting the results that's intended for index2.

     

    Adding  if (_uutTestScriptcmbox.SelectedValue != null) { still gives me the error.

    Wednesday, December 19, 2007 6:31 PM
  • Thnks DV,

    Casting as UUT, worked great!

     

     

     

    Wednesday, December 19, 2007 7:16 PM