Wrox Programmer Forums
|
BOOK: ASP.NET Website Programming Problem-Design-Solution
This is the forum to discuss the Wrox book ASP.NET Website Programming: Problem - Design - Solution, Visual Basic .NET Edition by Marco Bellinaso, Kevin Hoffman; ISBN: 9780764543869
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET Website Programming Problem-Design-Solution 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
 
Old October 3rd, 2004, 08:43 AM
Registered User
 
Join Date: Feb 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Answer for "Specified cast is not valid"

First:I Click "Submit_Click" of login.aspx. All is right

  private void Submit_Click(object sender, System.EventArgs e)
  {
 SitePrincipal newUser = SitePrincipal.ValidateLogin( UserName.Text, Password.Text );
 if (newUser == null)
 {
   LoginResult.Text = UserName.Text + "Login failed";
   LoginResult.Visible = true;
 }
 else
 {
   LoginResult.Visible = false;
   Context.User = newUser;
   FormsAuthentication.SetAuthCookie(UserName.Text, true);
   Wrox.WebModules.Accounts.Business.User currentUser = new Wrox.WebModules.Accounts.Business.User( (Wrox.WebModules.Accounts.Business.SitePrincipal)C ontext.User );
   Response.Write(currentUser.EmailAddress);
 }

  }


Second :"Specified cast is not valid" creates When I visit test.aspx.


private void Page_Load(object sender, System.EventArgs e)
{
  if (Context.User.Identity.IsAuthenticated)
  {
Wrox.WebModules.Accounts.Business.User currentUser = new Wrox.WebModules.Accounts.Business.User( (Wrox.WebModules.Accounts.Business.SitePrincipal)C ontext.User );
Response.Write(currentUser.EmailAddress);
  }
  else
  {
Response.Write("I don't know");
  }
}
 
Old October 3rd, 2004, 01:54 PM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't see the problem, but I this error occurs when trying to cast the GenericPrincipal Context.User as a SitePrincipal Context.User before the SitePrincipal object has been loaded into the Context.User, so look for that. You should be able to step into the code and see exactly where it breaks.
It should look like this:
(SitePrincipal)Context.User
(boom)

 
Old October 4th, 2004, 02:20 AM
Registered User
 
Join Date: Feb 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i dont know "before the SitePrincipal object has been loaded into the Context.User"
can i cast the GenericPrincipal Context.User as a SitePrincipal Context.User after the SitePrincipal object has been loaded into the Context.User

 
Old October 4th, 2004, 09:39 PM
Registered User
 
Join Date: Feb 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

my answer:
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeComponent();
        }
error:
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

 
Old October 10th, 2004, 05:22 PM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's very strange...I ended up getting the same "Cast Not Valid" error soon after replying to your above post, and my problem was that I had to switch the two lines of code also, but OPPOSITE of what you did.
From:
InitializeComponent();
base.OnInit(e);

To:
base.OnInit(e);
InitializeComponent();

There's a recent post about this same error, and englere explains that you MUST HAVE base.OnInit(e) first on all pages. You should be careful about your solution because it may be wrong. He explains that base.OnInit(e) coming first causes the PhilePage.cs base class to load before the child classes, which is important because the PhilePage.cs base class is where the loading of SitePrincipal and SiteIdentity get loaded into Context.User in place of the normal GenericPrincipal and GenericIdentity.

 
Old October 10th, 2004, 05:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You both agree. yingmingming said the first code segment worked, and the second one did not.

base.OnInit(e); has to come first.

Eric
 
Old October 10th, 2004, 05:35 PM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Funny. I missed that. Guess I need a nap. I don't want to corrupt posts, but I'm still wondering about the URL path issue when it comes to remote web hosting. I'm thinking that unless there's a better solution(and it seems like there should be a simple setting)I'll set a global 'virtualroot' variable to something like "/thePhile/" and "" when it's time to deploy
Guess I do want to corrupt posts.

 
Old October 10th, 2004, 10:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

>I'll set a global 'virtualroot' variable

That's not a bad idea. But there's some places where it's hard to use this - like in the user controls and HTML code. You have to start doing it this way before you can see the problems you're going to face. It's hard to itemize every case where it will be a challenge. I urge you to take notes on all the changes you make - in case you decide to make different changes later.

Just to prove that I don't hate your idea, I'll give you this code snippet from ThePhileII. This gets the location of the Application root directory and saves it in a private field that can be accessed from a readonly property in all the other modules.

in global.asax.cs:

public class Global : System.Web.HttpApplication
{
   public Global()
   {
      InitializeComponent();
   }

   // private field to hold property value
   private String _myRoot;

   // Here's the readonly property definition:
   public String myRoot
   {
     get
     {
       return _myRoot;
     }
   }
   ...

You need to set the value of the private field in the application_Start event handler:

protected void Application_Start(Object sender, EventArgs e)
{
   _myRoot = HttpContext.Current.Request.ApplicationPath;

   // Here's a helpful trace listener to log to a file:
   TextWriterTraceListener tr = new TextWriterTraceListener
      (System.IO.File.CreateText(Server.MapPath("Trace.l og")));
   Debug.Listeners.Add(tr); // Trace and Debug share this collection
   Trace.AutoFlush = true;

   // Here's an example of how to write a message to the trace log
   Trace.WriteLine("Application_Start: myroot=" + myRoot);
}

But this doesn't solve all your problems! Now you have to figure out how to read this property from a user control. It's not impossible - see if you can work it out. FYI - you don't want to read Request.ApplicationPath directly in a user control because it might not point to the correct application root - remember that user controls get called from various directories.

Eric
 
Old October 11th, 2004, 08:55 AM
Authorized User
 
Join Date: Aug 2004
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

so what do you suggest Eric to fix this problem??
Thanks,
Marenela

 
Old October 11th, 2004, 05:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You should think about this for a while. You won't learn anything if I give you all the answers :-)

Besides, I didn't want to do that much trouble myself, so I just hard-coded pathnames for deployment in the virtual root. I changed the virtual root on my computer to use c:\ThePhileII, so it works the same on my computer as it does on my hosted site.

Sometimes simple is good ...

Eric





Similar Threads
Thread Thread Starter Forum Replies Last Post
Specified cast is not valid heba ASP.NET 2.0 Professional 1 May 27th, 2007 08:51 PM
Specified cast is not valid. Jose P C# 5 May 11th, 2007 08:04 AM
Specified cast is not valid surajb Crystal Reports 0 January 12th, 2007 02:03 PM
"Specified cast is not valid" help BaBaBooey ASP.NET 1.0 and 1.1 Basics 2 November 23rd, 2004 12:12 PM
Specified cast is not valid TheMole BOOK: ASP.NET Website Programming Problem-Design-Solution 9 November 1st, 2004 05:09 PM





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