Intermittent System.InvalidCastException: Specified cast is not valid it's driving me mad

A

Allan Wenham

Hello,

I have a Intermittent error, only happens once or twice a day:

This is the basic code:

Dim strSeachLetter As String

strSeachLetter =Request.QueryString("letter")

If strSeachLetter <> "" Then
strSeachLetter.Trim()
If TypeOf strSeachLetter Is String Then
Call filterAuthorByLetter(strSeachLetter.ToString)
End If
End If

Public Sub filterAuthorByLetter(ByVal inletter As String)
'does something with the inletter
end sub

Every now and again somthing goes odd and I get:

System.InvalidCastException: Specified cast is not valid. at
mysite.WebForm1.filterAuthorByLetter(String inletter)

I don't undersatand why I am getting it as I know the input in the sub
IS a string it won't run the line otherwise, I can't get it to repleat
on my local site, it only happens on the 'live' site where I have had
to supress the error and have it email me when it fails. Other bizar
thing that might help is that when this first cropped up I was hitting
a URL such as 'mypage.aspx?letter=R' that would fail but
'mypage.aspx?letter=r' would work, after a recompile the 'R' started
working again but it seems that every now and then it falls down for
no reason. The sub doesn't care what case the letter is by the way it
makes no difference to the code in side.

Anybody got any ideas, at all, it diving me mad.

-Wenham
 
K

Ken Cox [Microsoft MVP]

Hi Allan,

Assuming you haven't gone totally mad yet....

You might want to check the incoming Request.QueryString("letter")
for IsNothing() before assuming it is a string. Sometimes you might get
Nothing.

As for the case issue, not sure what that could be. Are you caching pages or
URLs? Make sure your browser is set to fetch a new page every time during
development. You may be getting a bad old page.

Let us know how you make out?

Ken
Microsoft MVP [ASP.NET]
 
A

Allan Wenham

Hello Ken,

Thanks for the reply, I havn't gone mad yet, but it's getting there!

I've tried checking the Query string is not nothing. The bug still
happens when there is a vaild string coming in, I know this as when the
bug rises the exception the page emails me with the
request.querryString("letter") and also the value of '(strSeachLetter
is nothing).tostring)' (to which the q string is applied) of the 5 times
over the weekend the bug has happened they all had a vaild 'letter' and
strSeachLetter was not nothing.

I am going to try the caching idea now, I already have the view state
turned off, I have added the following lines to my on_load:

Response.CacheControl = "no-cache"
Response.AddHeader("Pragma", "no-cache")
Response.Expires = -1

I should know in a few hours if it worked or not.

Thanks,

-Wenham
 
A

Allan Wenham

Well in the end I rebuilt the page from the bottom up, the only
difference this time round I didn't use a reaper on the page at all.
It's been running overnight has hasn't crashed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top