Re: invalidcast exception
What type is R1011? Unless it was declared as an Integer, I don't see how you can get that error.
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.
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.
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"))
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.
Re: invalidcast exception
Hey Guys,
I did load the code suggested and I got the same error message?
Thanks
Art W.
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?
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.
Re: invalidcast exception
Quote:
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?
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.
Re: invalidcast exception
I ask again:
Quote:
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:
Quote:
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.
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.
Re: invalidcast exception
Quote:
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.
Quote:
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.
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.
Re: invalidcast exception
Quote:
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:
Quote:
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.
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.