Intermittent "Specified cast is not valid" Exception

F

Fabian

Hi,

I work with asp.net 2.0 and I have a intermittent error, only happens a few
times a day.
In the page I evaluate a Query String and then I get data form a database.

The code snipped:

try
{
queryId = int.Parse(Request.QueryString["id"].ToString());
...
}
catch (Exception exc)
{
throw new PortalException("Web : " + exc.Message);
}

The QueryString is always the same!
Every now and again somthing goes odd and I get this Exception:
................
Exception Details: Portal.Web.PortalException: Web : Specified cast is not
valid.

Stack Trace:
[PortalException: Web : Specified cast is not valid.]
Portal.Web.Content.Tool.Page_Load() in
d:\Website\Portal\Web\Content\Tools.aspx.cs:40
System.Web.Util.CalliHelper.ArglessFunctionCaller(IntPtr fp, Object o) +5
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) +28
System.Web.UI.Control.OnLoad(EventArgs e) +87
System.Web.UI.Control.LoadRecursive() +55
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2836


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

-Fabian
 
H

Hans Kesting

Fabian said:
Hi,

I work with asp.net 2.0 and I have a intermittent error, only happens a few
times a day.
In the page I evaluate a Query String and then I get data form a database.

The code snipped:

try
{
queryId = int.Parse(Request.QueryString["id"].ToString());
...
}
catch (Exception exc)
{
throw new PortalException("Web : " + exc.Message);
}

The QueryString is always the same!
Every now and again somthing goes odd and I get this Exception:
...............
Exception Details: Portal.Web.PortalException: Web : Specified cast is not
valid.

Stack Trace:
[PortalException: Web : Specified cast is not valid.]
Portal.Web.Content.Tool.Page_Load() in
d:\Website\Portal\Web\Content\Tools.aspx.cs:40
System.Web.Util.CalliHelper.ArglessFunctionCaller(IntPtr fp, Object o) +5
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) +28
System.Web.UI.Control.OnLoad(EventArgs e) +87
System.Web.UI.Control.LoadRecursive() +55
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2836


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

-Fabian

Are you sure it's the querystring that gives the problems?
I would expect NullReference or Format exceptions then!

When you get data from a database (as you specified), you
could get null values, or DbNull.Value in .Net. When you
were retrieving a varchar column you can cast the datavalue
to string. This works if there really was a string, but you
would get a "specified cast is not valid" if it was DbNull.Value!

For string fields there is an easy way around it: don't use
cast, but use the ToString() method: DbNull.Value.ToString() == "".
 
F

Fabian

Hi Hans,

Thanks for answer!
The exeption was not thrown by the data access, because this layer has an
own Exception handling.
Could it be that the using of a global object in Global.asax throw the
exeption.
In Global.asax I create an object over this line

<object Class="Portal.Web.Components.Categories" Id="categories"
RunAt="server" Scope="session" />

and in the aspx.page I get data from this object.
The using of an global object was always makes me uneasy but I need this.

-Fabian

Hans Kesting said:
Fabian said:
Hi,

I work with asp.net 2.0 and I have a intermittent error, only happens a
few times a day.
In the page I evaluate a Query String and then I get data form a
database.

The code snipped:

try
{
queryId = int.Parse(Request.QueryString["id"].ToString());
...
}
catch (Exception exc)
{
throw new PortalException("Web : " + exc.Message);
}

The QueryString is always the same!
Every now and again somthing goes odd and I get this Exception:
...............
Exception Details: Portal.Web.PortalException: Web : Specified cast is
not valid.

Stack Trace:
[PortalException: Web : Specified cast is not valid.]
Portal.Web.Content.Tool.Page_Load() in
d:\Website\Portal\Web\Content\Tools.aspx.cs:40
System.Web.Util.CalliHelper.ArglessFunctionCaller(IntPtr fp, Object o)
+5
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) +28
System.Web.UI.Control.OnLoad(EventArgs e) +87
System.Web.UI.Control.LoadRecursive() +55
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+2836


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

-Fabian

Are you sure it's the querystring that gives the problems?
I would expect NullReference or Format exceptions then!

When you get data from a database (as you specified), you
could get null values, or DbNull.Value in .Net. When you
were retrieving a varchar column you can cast the datavalue
to string. This works if there really was a string, but you
would get a "specified cast is not valid" if it was DbNull.Value!

For string fields there is an easy way around it: don't use
cast, but use the ToString() method: DbNull.Value.ToString() == "".
 

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,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top