By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

Intermittent "Specified cast is not valid" Exception

P: n/a
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.Cal lback(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
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Fabian wrote:
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.Cal lback(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() == "".
--
Hans Kesting
Nov 19 '05 #2

P: n/a
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" <ne***********@spamgourmet.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Fabian wrote:
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.Cal lback(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() == "".
--
Hans Kesting

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.