Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 10th, 2008, 02:01 PM
Registered User
 
Join Date: Jun 2008
Location: São Paulo, SP, Brazil.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem in Globals class

Hellow,

I'm reading asp.net 2.0 website programming Problem Design and Solution and I got a big problem in chapter 3

when I try to do this cast:

Public NotInheritable Class Globals

...

'pag 103
        Public Shared ReadOnly Settings As TheBeerHouseSection = _
                CType(WebConfigurationManager.GetSection("theBeerH ouse"), MB.TheBeerHouse.TheBeerHouseSection)


End Class

I got this error:


Server Error in '/MyTheBeerHouse' Application.
--------------------------------------------------------------------------------

Unable to cast object of type 'MB.TheBeerHouse.TheBeerHouseSection' to type 'MyTheBeerHouse.MB.TheBeerHouse.TheBeerHouseSectio n'.
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.

Exception Details: System.InvalidCastException: Unable to cast object of type 'MB.TheBeerHouse.TheBeerHouseSection' to type 'MyTheBeerHouse.MB.TheBeerHouse.TheBeerHouseSectio n'.

Source Error:


Line 18: 'pag 103
Line 19: 'todo: estou tendo um erro aqui
Line 20: Public Shared ReadOnly Settings As TheBeerHouseSection = _
Line 21: CType(WebConfigurationManager.GetSection("theBee rHouse"), MB.TheBeerHouse.TheBeerHouseSection)
Line 22:


Source File: D:\Visual Studio 2005\LIVRO WebSite Programming\MyTheBeerHouse\MyTheBeerHouse\App_Code \Globals.vb Line: 20

Stack Trace:


[InvalidCastException: Unable to cast object of type 'MB.TheBeerHouse.TheBeerHouseSection' to type 'MyTheBeerHouse.MB.TheBeerHouse.TheBeerHouseSectio n'.]
MyTheBeerHouse.MB.TheBeerHouse.Globals..ccto r() in D:\Visual Studio 2005\LIVRO WebSite Programming\MyTheBeerHouse\MyTheBeerHouse\App_Code \Globals.vb:20

[TypeInitializationException: The type initializer for 'MyTheBeerHouse.MB.TheBeerHouse.Globals' threw an exception.]
MyTheBeerHouse.MB.TheBeerHouse.UI.BasePage.O nPreInit(EventArgs e) in D:\Visual Studio 2005\LIVRO WebSite Programming\MyTheBeerHouse\MyTheBeerHouse\App_Code \BasePage.vb:10
System.Web.UI.Page.ProcessRequestMain(Boolea n includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +663




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433


Someone could help me?


and another question:

What is the difference between 'MB.TheBeerHouse.TheBeerHouseSection' and 'MyTheBeerHouse.MB.TheBeerHouse.TheBeerHouseSectio n'
  #2 (permalink)  
Old June 10th, 2008, 04:14 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi pedroabs,

If you have changed your the root namespace of your application you need to be aware of the impact in doing so. It looks to me like you have not updated all namespace declarations to "MyBeerHouse.MB...". Can I ask what your reason was changing it in the first place? If you wanted to create a more personal namespace heirarchy in your app, you should really use the general convention CompanyName.ApplicationName.ClassName/group (i.e. MB.TheBeerHouse.TheBeerHouseSection).

As for the above error, can you check the type you are declaring for "TheBeerHouseSection" in web.config. It's possible you have not updated this to your new namespace.

Hope that helps.

Retro

  #3 (permalink)  
Old June 10th, 2008, 05:48 PM
Registered User
 
Join Date: Jun 2008
Location: São Paulo, SP, Brazil.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Retro:

Well, I didn't update my namespace, but i realize that when I try to convert like this:

'pag 103
 Public Shared ReadOnly Settings As TheBeerHouseSection = _
                CType(WebConfigurationManager.GetSection("theBeerH ouse"), TheBeerHouseSection)

the first part of my expression
[Settings As TheBeerHouseSection] have the type of ['MB.TheBeerHouse.TheBeerHouseSection']
and the second part of my expression
[WebConfigurationManager.GetSection("theBeerHouse")] have the type of
['MyTheBeerHouse.MB.TheBeerHouse.TheBeerHouseSectio n']


What is the difference between ['MB.TheBeerHouse.TheBeerHouseSection'] and ['MyTheBeerHouse.MB.TheBeerHouse.TheBeerHouseSectio n'] ?

I have just one class [TheBeerHouseSection].... maybe I have to review my web.config...


I declared in my web.config the <section type="MB.TheBeerHouse.TheBeerHouseSection" ...


  #4 (permalink)  
Old June 11th, 2008, 01:09 AM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi pedroabs,

[InvalidCastException: Unable to cast object of type 'MB.TheBeerHouse.TheBeerHouseSection' to type 'MyTheBeerHouse.MB.TheBeerHouse.TheBeerHouseSectio n'.]
   MyTheBeerHouse.MB.TheBeerHouse.Globals..cctor() in D:\Visual Studio 2005\LIVRO WebSite Programming\MyTheBeerHouse\MyTheBeerHouse\App_Code \Globals.vb:20

This would suggest somewhere that you have used the namespace MyBeerHouse.MB....

You ask what the difference is between the two. A namespace is a hierarchical method of organizing your classes into logical groups. If you change the root namespace you are changing that grouping. This is not a problem as long as you update all your namespace declarations.

Problems can occur if not all references are updated. Imagine you have a class "Customers" in the namespace MyCompany.MyApp.DAL which inherits from MyCompany.MyApp.DAL.Provider and then you change your namespace on your Customers class to Fred.MyCompany.MyApp.DAL but don't update your provider. Your Customer class will no longer be able to reference the provider class (unless of course you import it).

Hope that helps explain.

Regards.

  #5 (permalink)  
Old June 11th, 2008, 11:48 AM
Registered User
 
Join Date: Jun 2008
Location: São Paulo, SP, Brazil.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have the same problem described at:

http://p2p.wrox.com/topic.asp?TOPIC_ID=69248

I can't solve it yet...

I never change my namespace, I just see that when I try this in [u]Watch Window</u>:

CType(WebConfigurationManager.GetSection("theBeerH ouse"), MB.TheBeerHouse.TheBeerHouseSection)

the value is

{MB.TheBeerHouse.TheBeerHouseSection}



When I try this:

CType(WebConfigurationManager.GetSection("theBeerH ouse"),MyTheBeerHouse.MB.TheBeerHouse.TheBeerHouseSection)

or that:

CType(WebConfigurationManager.GetSection("theBeerH ouse"), TheBeerHouseSection)

The value is:

Cannot convert to 'Class TheBeerHouseSection'


  #6 (permalink)  
Old June 11th, 2008, 04:21 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can you confirm in Globals.vb you have:

Code:
Imports System.Web.Configuration
Imports Microsoft.VisualBasic

Namespace MB.TheBeerHouse
    Public NotInheritable Class Globals
        Public Shared ReadOnly Settings As TheBeerHouseSection = _
            CType(WebConfigurationManager.GetSection("theBeerHouse"), TheBeerHouseSection)

        Public Shared ThemesSelectorID As String = ""

    End Class
End Namespace
and in web.config:

Code:
        <section name="theBeerHouse" type="MB.TheBeerHouse.TheBeerHouseSection, __code"/>
Thanks

  #7 (permalink)  
Old June 12th, 2008, 09:12 AM
Registered User
 
Join Date: Jun 2008
Location: São Paulo, SP, Brazil.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Retroviz

I solve the problem changing the web.config:

The code below works well

<configSections>
    <section name="theBeerHouse" type="MyTheBeerHouse.MB.TheBeerHouse.TheBeerHouseSecti on"/>

</configSections>
<theBeerHouse defaultConnectionStringName="LocalSqlServer">
    <contactForm mailTo="thebeerhouse@wrox.com" />
</theBeerHouse>


The code below didn't work well:


<configSections>
    <section name="theBeerHouse" type="MB.TheBeerHouse.TheBeerHouseSection, __code"/>

</configSections>
<theBeerHouse defaultConnectionStringName="LocalSqlServer">
    <contactForm mailTo="thebeerhouse@wrox.com" />
</theBeerHouse>



I just put the entire namespace in the type of the Section tag

Thank you very very much !!!
  #8 (permalink)  
Old June 12th, 2008, 09:58 AM
Registered User
 
Join Date: Jun 2008
Location: São Paulo, SP, Brazil.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm confirming:

my code:

Imports System.Web.Configuration

Namespace MB.TheBeerHouse
    ''' <summary>
    ''' Esta classe apresenta propriedades globais para todo o site
    ''' </summary>
    ''' <remarks>Pag 40</remarks>
    Public NotInheritable Class Globals

        'pag 40
        Public Shared ThemesSelectorID As String = ""

        'pag 103
        Public Shared ReadOnly Settings As TheBeerHouseSection = _
                CType(WebConfigurationManager.GetSection("theBeerH ouse"), TheBeerHouseSection)

    End Class


My web.config:





  <configSections>
    <section name="theBeerHouse" type="MyTheBeerHouse.MB.TheBeerHouse.TheBeerHouseSection"/>

  </configSections>
  <theBeerHouse defaultConnectionStringName="LocalSqlServer">
    <contactForm mailTo="thebeerhouse@wrox.com" />
  </theBeerHouse>

I hope to help someone else who have this problem !!!

Attention:

MyTheBeerHouse is the root namespace of my project !!!


  #9 (permalink)  
Old June 12th, 2008, 01:11 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

great. Glad to see you got it working.



Similar Threads
Thread Thread Starter Forum Replies Last Post
Globals nabeelalkaff BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 April 29th, 2007 08:26 PM
Register Globals File Upload Problem fshequin Beginning PHP 0 April 24th, 2005 11:56 PM
$GLOBALS natmaster Beginning PHP 10 August 4th, 2004 03:00 PM
register globals problem nulogix Beginning PHP 4 June 16th, 2004 08:49 AM
Register Globals Off cmiller Beginning PHP 4 August 18th, 2003 05:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.