|
-
May 20th, 2010, 03:22 PM
#1
Thread Starter
Hyperactive Member
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!
-
May 20th, 2010, 03:36 PM
#2
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 -
-
May 20th, 2010, 03:44 PM
#3
Thread Starter
Hyperactive Member
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!
-
May 20th, 2010, 04:28 PM
#4
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 -
-
May 20th, 2010, 04:45 PM
#5
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
 
-
May 20th, 2010, 07:15 PM
#6
Thread Starter
Hyperactive Member
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!
-
May 20th, 2010, 07:20 PM
#7
Thread Starter
Hyperactive Member
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!
-
May 20th, 2010, 07:28 PM
#8
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?
-
May 20th, 2010, 07:52 PM
#9
Thread Starter
Hyperactive Member
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!
-
May 20th, 2010, 07:59 PM
#10
Re: invalidcast exception
 Originally Posted by Art W.
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?
-
May 20th, 2010, 08:14 PM
#11
Thread Starter
Hyperactive Member
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!
-
May 20th, 2010, 08:18 PM
#12
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.
-
May 20th, 2010, 08:24 PM
#13
Thread Starter
Hyperactive Member
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!
-
May 20th, 2010, 08:30 PM
#14
Re: invalidcast exception
 Originally Posted by Art W.
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.
 Originally Posted by Art W.
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.
-
May 20th, 2010, 08:40 PM
#15
Thread Starter
Hyperactive Member
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!
-
May 20th, 2010, 08:44 PM
#16
Re: invalidcast exception
 Originally Posted by Art W.
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.
-
May 21st, 2010, 11:08 AM
#17
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|