473,287 Members | 1,588 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,287 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 3065
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: ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.