Results 1 to 17 of 17

Thread: invalidcast exception

  1. #1

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    invalidcast exception

    Hello Everyone:

    I am having problems with the code below. I have copied and pasted the code from a program that I wrote to test and see if the code does work. In that program it does. But when I use the same code in another program I get the following error message. invalidcast exception was unhandled conversion from string “to type integer” is not valid.


    Code:
                If intCurrentIndex < ds.Tables(0).Rows.Count - 1 Then
                    intCurrentIndex = intCurrentIndex + 1 'EDIT LINE!!!!
                    R1011 = ds.Tables(0).Rows(intCurrentIndex).Item("Results01").ToString 'EDIT LINE
                    txtResults01.Text = R1011
    
                    'If intCurrentIndex < ds.Tables(0).Rows.Count - 1 Then
                    'intCurrentIndex = intCurrentIndex + 1
                    'R1011 = ds.Tables(0).Rows(intCurrentIndex).Item("Results01").ToString


    The code on the bottom is the copied code that works, intCurrentIndex is a integer and Results01 is in a MDB table.

    Any Ideas?

    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: invalidcast exception

    What type is R1011? Unless it was declared as an Integer, I don't see how you can get that error.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    Re: invalidcast exception

    Yes R1011 is an integer. I am going nuts. I don’t get it either.

    Results01 is a number, general number in the table also.

    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: invalidcast exception

    You'll get the error whenever the string value of Results01 cannot be converted to an integer, for example, an empty string.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: invalidcast exception

    In the test code, you might have had Option Strict OFF, so that the conversion was quietly performed, which would account for the code working in one project and not in the other. Option Strict OFF is not a good idea, but it is the default setting for some versions of VS, so it is likely.

    This should work, unless the item is ever empty, in which case it will raise an exception:

    R1011 = CInt(ds.Tables(0).Rows(intCurrentIndex).Item("Results01"))
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    Re: invalidcast exception

    Hey Guys,

    Thanks for the tips I have turned strict on and off, it didn’t work either time. If I change intCurrentIndex to 0 in the line with R1011 the program will run. But it doesn’t do what I need it to do. I am going to check out the other code you suggested and see if it works?

    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

  7. #7

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    Re: invalidcast exception

    Hey Guys,

    I did load the code suggested and I got the same error message?


    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: invalidcast exception

    First up, this code is bad no matter what:
    Code:
    R1011 = ds.Tables(0).Rows(intCurrentIndex).Item("Results01").ToString
    if R1011 is type Integer then how can converting something that isn't already a string into a string to assign to an Integer variable be a good thing? Either the value is already an Integer or its not but either way, why would converting it to a String be a good idea? Have you actually checked what the value of ds.Tables(0).Rows(intCurrentIndex).Item("Results01") is when the exception is thrown? I can only assume that it's not an Integer or a value that can be converted to an Integer. My guess is that it's DBNull.Value. What exactly do you expect to happen in that case?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    Re: invalidcast exception

    Hey Everyone,

    I tried it without the convert. It did the same thing. If that was the problem why did it work when I changed the intCurrentIndex to 0.


    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: invalidcast exception

    Quote Originally Posted by Art W. View Post
    Hey Everyone,

    I tried it without the convert. It did the same thing. If that was the problem why did it work when I changed the intCurrentIndex to 0.


    Thanks

    Art W.
    You have an Integer column, right? Does that column allow nulls? If a null value is present then calling ToString on it will create an empty string, which obviously can't be converted to an Integer. If you changed the current index to 0 then you were accessing a different row, which presumably didn;t contain a null value.

    Is it reasonable for your column to contain null values? If not then why is it configured to do so? If not then how can you simply assign the value to an Integer without checking for null first?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    Re: invalidcast exception

    Hey Everyone,

    There are no null values and there won’t be any, the intCurrentIndex should return a value of 14, and it does if intCurrentIndex is 0. The table and its values are the same as the one from the program that works and the one that doesn’t?


    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: invalidcast exception

    I ask again:
    Have you actually checked what the value of ds.Tables(0).Rows(intCurrentIndex).Item("Results01") is when the exception is thrown?
    Also, this makes no sense:
    the intCurrentIndex should return a value of 14, and it does if intCurrentIndex is 0
    intCurrentIndex can't be 14 and 0 at the same time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    Re: invalidcast exception

    Hey Everyone,

    That is the line the program stops on and that line is highlighted in yellow, I hope that answers the first part of your question.

    And yes if I change it to 0, it returns 14. But that is only the first record in the set. And I need to get the rest of them.

    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: invalidcast exception

    Quote Originally Posted by Art W. View Post
    That is the line the program stops on and that line is highlighted in yellow, I hope that answers the first part of your question.
    I feel like I've entered the twilight zone. I asked what the value of ds.Tables(0).Rows(intCurrentIndex).Item("Results01") is when the exception is thrown. How does telling me that that is the line that throws the exception answer that question. When the exception is thrown, highlight that part of the code, right-click it, select Quick Watch and look at what the actual value is.
    Quote Originally Posted by Art W. View Post
    And yes if I change it to 0, it returns 14. But that is only the first record in the set. And I need to get the rest of them.
    So what you actually mean was that ds.Tables(0).Rows(intCurrentIndex).Item("Results01") returns 14 when intCurrentIndex is 0, not that intCurrentIndex returns 14 when intCurrentIndex is 0. I don't care what the field value is when intCurrentIndex is 0. I care what it is when the exception is thrown. That is where the problem is so that is the value that YOU need to get.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    Re: invalidcast exception

    Hey Everyone,

    Sorry, I didn’t understand your question. I am nowhere near your level. It says that Results01 is not declared. I am going to try that. But I didn’t declare it in the other program that works?

    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: invalidcast exception

    Quote Originally Posted by Art W. View Post
    Hey Everyone,

    Sorry, I didn’t understand your question. I am nowhere near your level. It says that Results01 is not declared. I am going to try that. But I didn’t declare it in the other program that works?

    Thanks

    Art W.
    Then you've done it wrong. I said:
    When the exception is thrown, highlight that part of the code, right-click it, select Quick Watch and look at what the actual value is.
    You need to highlight the expression you want to evaluate. In this case that is ds.Tables(0).Rows(intCurrentIndex).Item("Results01"). You need to highlight exactly that; no more, no less.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: invalidcast exception

    Shift+F9 also works for taking a leisurely look at what is in that line. Taking a look at what is in there is the key step, though, because whatever it is is not what you think it is.
    My usual boring signature: Nothing

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