Invalid Cast Exception - Class Objects

R

ratnakarp

hi,

I'm using c#, asp.net 2003 and sql server 2000 to develop a portal
application. my web.config file looks as follows


<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="dsnstore"
type="System.Configuration.NameValueSectionHandler,System,Version=1.0.5000.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"
/>
</sectionGroup>
</configSections>

<system.web>
<compilation debug="true" />

<authorization>
<allow users="*" /> <!-- Allow all (other) users -->
</authorization>
<authentication mode="Forms" />

<httpModules>
<add name="Portal Personalization"
type="Personalization.UserStateModule,Personalization" />
</httpModules>

<dsnstore>
<add key="portaldb"
value="server=MAAKI\PORTAL;database=Portal;uid=portal;pwd=portal;Integrated
Security=SSPI" />
</dsnstore>

<globalization requestEncoding="UTF-8" responseEncoding="UTF-8" />
</system.web>
</configuration>



i have 3 cs files which connects to the database and gets the
information namely Personalization.cs , PersonalModuleControl.cs,
PersonalModulePage.cs.

Personalization.cs --->
http://www.dotnetjunkies.com/quicks...=CS\personalization/Personalization.cs&font=3


PortalModuleCOntrol.cs -->

http://www.dotnetjunkies.com/quicks...personalization/PortalModuleControl.cs&font=3


PortalModulePage.cs -->

http://www.dotnetjunkies.com/quicks...CS\personalization/PortalModulePage.cs&font=3

Default.cs -->

http://www.dotnetjunkies.com/quicks...s/portal/portal.src&file=CS\default.cs&font=3


I compiled the cs files in the following way.

csc /t:library /out: .....(localpath)/portal/bin/Personalization.dll
Personalization.cs
csc /t:library /out:
......(localpath)/portal/bin/PortalModuleControl.dll Personalization.cs
PortalModuleCOntrol.cs

csc /t:library /out: .....(localpath)/portal/bin/PortalModulePage.dll
Personalization.cs PortalModulePage.cs

when i try to run my application, i'm getting the following error.

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: Specified cast is not
valid.

Source Error:


Line 25: get
Line 26: {
Line 27: UserState myState = (UserState)
Context.Items["UserState"];
Line 28:
Line 29: if (myState == null)


Source File:
c:\inetpub\wwwroot\portal\personalization\portalmodulepage.cs Line:
27

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
PortalModulePage.get_UserState() in
c:\inetpub\wwwroot\portal\personalization\portalmodulepage.cs:27
DefaultPage.BuildPaneLinkList(HtmlContainerControl container, Int32
currentPageIndex) in c:\inetpub\wwwroot\portal\default.cs:116
DefaultPage.Page_Load(Object sender, EventArgs E) in
c:\inetpub\wwwroot\portal\default.cs:69
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()




Can you please help me out where am i going wrong?

Thanks & Regards,
Ratnakar Pedagani
 
Z

Ziga Jakhel

What exactly is the value and type of Context.Items["UserState"] at line 27?
Does it exist at all?

Regards,

Ziga Jakhel
MCSD.NET



hi,

I'm using c#, asp.net 2003 and sql server 2000 to develop a portal
application. my web.config file looks as follows


<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="dsnstore"
type="System.Configuration.NameValueSectionHandler,System,Version=1.0.5000.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"
/>
</sectionGroup>
</configSections>

<system.web>
<compilation debug="true" />

<authorization>
<allow users="*" /> <!-- Allow all (other) users -->
</authorization>
<authentication mode="Forms" />

<httpModules>
<add name="Portal Personalization"
type="Personalization.UserStateModule,Personalization" />
</httpModules>

<dsnstore>
<add key="portaldb"
value="server=MAAKI\PORTAL;database=Portal;uid=portal;pwd=portal;Integrated
Security=SSPI" />
</dsnstore>

<globalization requestEncoding="UTF-8" responseEncoding="UTF-8" />
</system.web>
</configuration>



i have 3 cs files which connects to the database and gets the
information namely Personalization.cs , PersonalModuleControl.cs,
PersonalModulePage.cs.

Personalization.cs --->
http://www.dotnetjunkies.com/quicks...=CS\personalization/Personalization.cs&font=3


PortalModuleCOntrol.cs -->

http://www.dotnetjunkies.com/quicks...personalization/PortalModuleControl.cs&font=3


PortalModulePage.cs -->

http://www.dotnetjunkies.com/quicks...CS\personalization/PortalModulePage.cs&font=3

Default.cs -->

http://www.dotnetjunkies.com/quicks...s/portal/portal.src&file=CS\default.cs&font=3


I compiled the cs files in the following way.

csc /t:library /out: .....(localpath)/portal/bin/Personalization.dll
Personalization.cs
csc /t:library /out:
.....(localpath)/portal/bin/PortalModuleControl.dll Personalization.cs
PortalModuleCOntrol.cs

csc /t:library /out: .....(localpath)/portal/bin/PortalModulePage.dll
Personalization.cs PortalModulePage.cs

when i try to run my application, i'm getting the following error.

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: Specified cast is not
valid.

Source Error:


Line 25: get
Line 26: {
Line 27: UserState myState = (UserState)
Context.Items["UserState"];
Line 28:
Line 29: if (myState == null)


Source File:
c:\inetpub\wwwroot\portal\personalization\portalmodulepage.cs Line:
27

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
PortalModulePage.get_UserState() in
c:\inetpub\wwwroot\portal\personalization\portalmodulepage.cs:27
DefaultPage.BuildPaneLinkList(HtmlContainerControl container, Int32
currentPageIndex) in c:\inetpub\wwwroot\portal\default.cs:116
DefaultPage.Page_Load(Object sender, EventArgs E) in
c:\inetpub\wwwroot\portal\default.cs:69
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()




Can you please help me out where am i going wrong?

Thanks & Regards,
Ratnakar Pedagani
 
M

Marina

Whatever object you get from Context.Items["UserState"], it is not of type
UserState.

If it's not null, you can call GetType on it, and then take a look at what
the type is.

hi,

I'm using c#, asp.net 2003 and sql server 2000 to develop a portal
application. my web.config file looks as follows


<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="dsnstore"
type="System.Configuration.NameValueSectionHandler,System,Version=1.0.5000.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"
/>
</sectionGroup>
</configSections>

<system.web>
<compilation debug="true" />

<authorization>
<allow users="*" /> <!-- Allow all (other) users -->
</authorization>
<authentication mode="Forms" />

<httpModules>
<add name="Portal Personalization"
type="Personalization.UserStateModule,Personalization" />
</httpModules>

<dsnstore>
<add key="portaldb"
value="server=MAAKI\PORTAL;database=Portal;uid=portal;pwd=portal;Integrated
Security=SSPI" />
</dsnstore>

<globalization requestEncoding="UTF-8" responseEncoding="UTF-8" />
</system.web>
</configuration>



i have 3 cs files which connects to the database and gets the
information namely Personalization.cs , PersonalModuleControl.cs,
PersonalModulePage.cs.

Personalization.cs --->
http://www.dotnetjunkies.com/quicks...=CS\personalization/Personalization.cs&font=3


PortalModuleCOntrol.cs -->

http://www.dotnetjunkies.com/quicks...personalization/PortalModuleControl.cs&font=3


PortalModulePage.cs -->

http://www.dotnetjunkies.com/quicks...CS\personalization/PortalModulePage.cs&font=3

Default.cs -->

http://www.dotnetjunkies.com/quicks...s/portal/portal.src&file=CS\default.cs&font=3


I compiled the cs files in the following way.

csc /t:library /out: .....(localpath)/portal/bin/Personalization.dll
Personalization.cs
csc /t:library /out:
.....(localpath)/portal/bin/PortalModuleControl.dll Personalization.cs
PortalModuleCOntrol.cs

csc /t:library /out: .....(localpath)/portal/bin/PortalModulePage.dll
Personalization.cs PortalModulePage.cs

when i try to run my application, i'm getting the following error.

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: Specified cast is not
valid.

Source Error:


Line 25: get
Line 26: {
Line 27: UserState myState = (UserState)
Context.Items["UserState"];
Line 28:
Line 29: if (myState == null)


Source File:
c:\inetpub\wwwroot\portal\personalization\portalmodulepage.cs Line:
27

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
PortalModulePage.get_UserState() in
c:\inetpub\wwwroot\portal\personalization\portalmodulepage.cs:27
DefaultPage.BuildPaneLinkList(HtmlContainerControl container, Int32
currentPageIndex) in c:\inetpub\wwwroot\portal\default.cs:116
DefaultPage.Page_Load(Object sender, EventArgs E) in
c:\inetpub\wwwroot\portal\default.cs:69
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()




Can you please help me out where am i going wrong?

Thanks & Regards,
Ratnakar Pedagani
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top