Specified cast is not valid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Digiplex
    New Member
    • Feb 2008
    • 8

    Specified cast is not valid

    Hi all, I'm working in C# and the following works on my local box (vista) but when I load it to the server and test I get an Invalid Cast Exception error. (I'm new to C# and yeah I know I'm late to the ballgame) Please help. The stack trace indicates the error is here ("SetUserData(" LakinPortal", Login1.UserName , Login1.Password );") see listing below.

    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using TheMembershipProvider;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    
        protected void OnLoggingIn(object sender, EventArgs e)
        {
            //Set User rights to db
            SetUserData("LakinPortal", Login1.UserName, Login1.Password);
    
        }
    
        public void SetUserData(string AppName, string username, string password)
        {
            DataTable ds = new DataTable();
            string strPwd;
    
            try
            {
                TheMembershipProvider.TheMembershipProvider Mem = new TheMembershipProvider.TheMembershipProvider();
    
                ds = Mem.GetUserSecParams(username, password);
    
    ...
    Last edited by Plater; Feb 20 '08, 03:44 PM. Reason: added code tags
  • beebob
    New Member
    • Sep 2007
    • 6

    #2
    I am also a beginner in C#, but I will try to help you.. :-)

    First of all, I want to know something in your code:
    1. Are you sure that Login1.UserName and Login1.Password are really a string?
    2. What is Login1?
    3. Have you try to use something like this:

    Code:
    ...
    SetUserData("LakinPortal", <write_your_login_name_here>, <write_your_login_password_here>);
    ...
    I mean, put the real login name and login password in that part of your code, not by getting the string from other part / parameter. Try this, and if you get the same error, it means the problem is not at SetUserData.

    Hope it can help.


    Regards,
    Novan Ananda


    Originally posted by Digiplex
    Hi all, I'm working in C# and the following works on my local box (vista) but when I load it to the server and test I get an Invalid Cast Exception error. (I'm new to C# and yeah I know I'm late to the ballgame) Please help. The stack trace indicates the error is here ("SetUserData(" LakinPortal", Login1.UserName , Login1.Password );") see listing below.


    using System;
    using System.Data;
    using System.Configur ation;
    using System.Collecti ons;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;
    using TheMembershipPr ovider;

    public partial class _Default : System.Web.UI.P age
    {
    protected void Page_Load(objec t sender, EventArgs e)
    {
    }

    protected void OnLoggingIn(obj ect sender, EventArgs e)
    {
    //Set User rights to db
    SetUserData("La kinPortal", Login1.UserName , Login1.Password );

    }

    public void SetUserData(str ing AppName, string username, string password)
    {
    DataTable ds = new DataTable();
    string strPwd;

    try
    {
    TheMembershipPr ovider.TheMembe rshipProvider Mem = new TheMembershipPr ovider.TheMembe rshipProvider() ;

    ds = Mem.GetUserSecP arams(username, password);

    ...

    Comment

    • Digiplex
      New Member
      • Feb 2008
      • 8

      #3
      Thanks for the suggestion, but it didn't work. To be more precise on my development environment it works, just as the original code does but on the production box I get the Specified cast is not valid error. I’m using ASP 2.0 on both. My dev box is using Visual Studio 2005. This is perplexing. Hey but keep the ideas coming thanks.

      I’ve change the code a little to pass ref’s to see if that makes a diff, but noooo. Here is the slightly new code:
      Code:
      using System;
      using System.Data;
      using System.Configuration;
      using System.Collections;
      using System.Web;
      using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Web.UI.HtmlControls;
      using TheMembershipProvider;
      
      public partial class _Default : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
          }
      
          protected void OnLoggingIn(object sender, EventArgs e)
          {
              //Set User rights to db
              //SetUserData("LakinPortal", Login1.UserName, Login1.Password);
              SetUserData("LakinPortal", "joe", "joepw"); //just a test
      
          }
      
          public void SetUserData(string AppName, string username, string password)
          {
              DataTable ds = null;
              string strPwd;
              int Roweffected = 0;
      
              try
              {
                  TheMembershipProvider.TheMembershipProvider Mem = new TheMembershipProvider.TheMembershipProvider();
      
                  Roweffected = Mem.GetUserSecParams(username, password, ref ds);
      
                  if (Roweffected > 0)
                  {…
      And here is a piece of the code from TheMembershipPr ovider dll which is custom:
      Code:
      using System;
      using System.Reflection;
      using System.Web;
      using System.Data;
      using System.Configuration;
      using System.Collections;
      using System.Web.Security;
      using System.IO;
      using System.Security.Cryptography;
      using System.Collections.Specialized;
      using System.Data.SqlClient;
      using TheDatabase.Utilities.Database;
      
      namespace TheMembershipProvider
      {
          public class TheMembershipProvider: MembershipProvider
          {
      . . .
      
              public int GetUserSecParams(string username, string password, ref DataTable d)
              {
                  DBConString = GetConnectionString();
      
                  try
                  {
                      SqlParameter[] p = new SqlParameter[2];
      
                      SqlParameter a = new SqlParameter("@userid", SqlDbType.VarChar);
                      a.Value = username;
                      a.Direction = System.Data.ParameterDirection.Input;
                      p[0] = a;
      
                      SqlParameter b = new SqlParameter("@password", SqlDbType.VarChar);
                      b.Value = password;
                      b.Direction = System.Data.ParameterDirection.Input;
                      p[1] = b;
      
      
                      TheDatabase.Utilities.Database.TheDatabase LakinDB = new TheDatabase.Utilities.Database.TheDatabase(DBConString);
                      int RowsAffected = 0;
                      RowsAffected = TheDB.ExecuteQueryStoredProcedure("getdata", ref p, ref d);
      
                      return RowsAffected;
                  }
                  catch (Exception ex)
                  {
                      throw ex;
                  }
              }
      . . .
      Last edited by Plater; Feb 20 '08, 03:44 PM. Reason: added CODE tags

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Vista uses completely different styles for verification/authorization.

        I would guess something you are using is not the same object on other OSs.

        Where you able to get a more percise location from the stacktrace when you used literal string to test the SetUser()?

        Comment

        • Digiplex
          New Member
          • Feb 2008
          • 8

          #5
          Originally posted by Plater
          Vista uses completely different styles for verification/authorization.

          I would guess something you are using is not the same object on other OSs.

          Where you able to get a more percise location from the stacktrace when you used literal string to test the SetUser()?
          No here is the stack trace:

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

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

          Source Error:

          An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

          Stack Trace:


          [InvalidCastExce ption: Specified cast is not valid.]
          _Default.SetUse rData(String AppName, String username, String password) +1194
          _Default.OnLogg ingIn(Object sender, EventArgs e) +23
          System.Web.UI.W ebControls.Logi n.OnLoggingIn(L oginCancelEvent Args e) +105
          System.Web.UI.W ebControls.Logi n.AttemptLogin( ) +67
          System.Web.UI.W ebControls.Logi n.OnBubbleEvent (Object source, EventArgs e) +99
          System.Web.UI.C ontrol.RaiseBub bleEvent(Object source, EventArgs args) +35
          System.Web.UI.W ebControls.Butt on.OnCommand(Co mmandEventArgs e) +115
          System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String eventArgument) +163
          System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument) +7
          System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler sourceControl, String eventArgument) +11
          System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +33
          System.Web.UI.P age.ProcessRequ estMain(Boolean includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +1746




          --------------------------------------------------------------------------------
          Version Information: Microsoft .NET Framework Version:2.0.507 27.1433; ASP.NET Version:2.0.507 27.1433

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            That is very strange.
            Are you able to run a debug session on the computer that it fails on at all?

            Comment

            • Digiplex
              New Member
              • Feb 2008
              • 8

              #7
              Thanks Plater and Beebob. I figured out the problem, it's in the piece of code I didn't give you. Or, rather the database. Someone else is handling the database portion and stored procedures. For me this is a black box they are suppose to tell me the output types. Well, I'm getting incomplete info on the types which is causing my cast error. Sorry about that. Thanks again.

              E.

              Comment

              Working...