Results 1 to 5 of 5

Thread: [RESOLVED] Invalid cast exception

  1. #1

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Resolved [RESOLVED] Invalid cast exception

    I'm getting an invalid cast exception which makes no sense whatsoever (see attachment).

    It occurs no matter what the number in the cell is.

    The ids variable is defined as so:
    c# Code:
    1. List<int> ids = new List<int>();

    Just below the line creating the error is another line...
    c# Code:
    1. link_Main_RFITableAdapter.DeleteID((int)row.Cells[0].Value);
    ... which contains the same cast. This line does not produce the error whien I run the program without the first line.

    Can anyone help? This is pretty urgent: I can't do much until I've fixed it.

    Thanks in advance,
    Qu.
    Attached Images Attached Images  
    Last edited by Quasar6; Jul 6th, 2008 at 07:08 PM.
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Invalid cast exception

    According to your screen shot the cell's Value property contains a string, not an int. If it was an int it wouldn't have the double quotes around it, which indicate a string literal.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Invalid cast exception

    Note that you can convert a string to an int, assuming the value is valid, but you cannot cast a string as an int. To cast one of the types must inherit or implement the other, i.e. there must be an IS A relationship between the two types. Neither string nor int inherit the other so there is no such relationship, so there is no valid cast.

  4. #4

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: Invalid cast exception

    Thanks jmc! Using an int.Parse and a ToString() method isn't pretty, but it works.

    Code:
     ids.Add(int.Parse(row.Cells[2].Value.ToString()));
    I just noticed that I wasn't using the same cell index in the second line. No wonder it worked when the first didn't.

    *headdesk*
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  5. #5
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: [RESOLVED] Invalid cast exception

    Or I think you could use System.Convert
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width