473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Intermittent "Specified cast is not valid" Exception

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
2 3073
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
11
by: Jim H | last post by:
I am trying to go through my Outlook (2003) address book. The code goes through the Items list and prints all the last names but after it hits 114 (out or 126 contacts) I get a "sepcified cast is...
13
by: Jack MacRank | last post by:
Hello, I'm coding a webform application in C# (ASP.NET 1.1 SP1 with VS.NET 2003 Pro on WinXP SP2 using IIS 5.1). I created a seperate "data" class to house all the MySQL connection and sql...
11
by: Roy Lawson | last post by:
I have no idea what is going on here. I wrote a simple application in VB.NET to generate a Crystal Report, and I am now trying to move it to ASP.NET with Crstal Enterprise. I wish I could tell...
8
by: Charles | last post by:
I do not understand why I am getting a "Specified cast is not valid" error, since it has worked before. Something has changed and I am not really sure what it could be. I am looking for something...
3
by: Pieter Coucke | last post by:
Hi, In my VB.NET 2005 application I'm generating and sending emails using the outlook-object model (2003). When a mail is Send (MailObject_Send), I raise an event in a global class, that is...
3
by: Jayyde | last post by:
Not sure why all of a sudden this line of code: cmbProductCategoryTreeDisplayName.SelectedIndex = 0; is producing a "Specified cast is not valid error". In the watch it has 1 item in it at...
1
by: bharathreddy | last post by:
Hai, In general why do we get the "This system cannot find the file specified". exception eventhough all the files used in the C# application are present. Here in my application I am refering...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.