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

System.InvalidCastException

100+
P: 149
Hi,

I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it says ProcessRequestMain... +3742??

Here's the error;

Expand|Select|Wrap|Line Numbers
  1. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  2.  
  3. Exception Details: System.InvalidCastException: Specified cast is not valid.
  4.  
  5. Source Error: 
  6.  
  7.  
  8. Line 126:        private static ComponentData DisplayComponent(IDataReader reader)
  9. Line 127:        {
  10. Line 128:            ComponentData component = new ComponentData(
  11. Line 129:               null, 
  12. Line 130:               (int)reader["section_id"],
  13.  
  14.  
  15. Source File: c:\Documents and Settings\Me\Desktop\tests\TMG_test\App_Code\DAL\ComponentData.cs    Line: 128 
  16.  
  17. Stack Trace: 
  18.  
  19.  
  20. [InvalidCastException: Specified cast is not valid.]
  21.    TMG.DAL.ComponentData.DisplayComponent(IDataReader reader) in c:\Documents and Settings\Me\Desktop\tests\TMG_test\App_Code\DAL\ComponentData.cs:128
  22.    TMG.DAL.ComponentData.GetComponentById(String pageId) in c:\Documents and Settings\Me\Desktop\tests\TMG_test\App_Code\DAL\ComponentData.cs:120
  23.    TMG.BLL.Components.ComponentInfo.GetComponentFromCache(String pageId) in c:\Documents and Settings\Me\Desktop\tests\TMG_test\App_Code\BLL\Components\ComponentInfo.cs:118
  24.    TMG.BLL.Components._components_featureComponent.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\Me\Desktop\tests\TMG_test\_components\featureComponent.ascx.cs:20
  25.    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
  26.    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +45
  27.    System.Web.UI.Control.OnLoad(EventArgs e) +80
  28.    System.Web.UI.Control.LoadRecursive() +49
  29.    System.Web.UI.Control.LoadRecursive() +132
  30.    System.Web.UI.Control.LoadRecursive() +132
  31.    System.Web.UI.Control.LoadRecursive() +132
  32.    System.Web.UI.Control.LoadRecursive() +132
  33.    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3742

Any help would be appreciated.
Jul 13 '07 #1
Share this Question
Share on Google+
4 Replies


100+
P: 149
Found the answer in another forum, so thought I'd share.

The problem was in a user control that I was populating with data in Visual Web Developer 2005 the page automatically sets AutoEventWireUp to true. This needs to be set to false otherwise it loads before the page_load event.

Cheers.
Jul 13 '07 #2

radcaesar
Expert 100+
P: 759
Try using Convert.Toint32(id);
Jul 13 '07 #3

100+
P: 149
Thanks RadCeaser.

The problem was AutoEventWireUp. Glad to have a fix:)
Jul 13 '07 #4

100+
P: 149
Ha!

I was being completely stupid:)

AutoEventWireUp just turned of the page_load function so no wonder the error went away.

The actual problem was a (bool)reader which I had entered into the DB as a string value=True rather than bit.

So now it's solved.
Jul 13 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.