473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using TheMembershipProvider;
  12.  
  13. public partial class _Default : System.Web.UI.Page
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.     }
  18.  
  19.     protected void OnLoggingIn(object sender, EventArgs e)
  20.     {
  21.         //Set User rights to db
  22.         SetUserData("LakinPortal", Login1.UserName, Login1.Password);
  23.  
  24.     }
  25.  
  26.     public void SetUserData(string AppName, string username, string password)
  27.     {
  28.         DataTable ds = new DataTable();
  29.         string strPwd;
  30.  
  31.         try
  32.         {
  33.             TheMembershipProvider.TheMembershipProvider Mem = new TheMembershipProvider.TheMembershipProvider();
  34.  
  35.             ds = Mem.GetUserSecParams(username, password);
  36.  
  37. ...
  38.  
Feb 19 '08 #1
6 1377
beebob
6
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:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. SetUserData("LakinPortal", <write_your_login_name_here>, <write_your_login_password_here>);
  3. ...
  4.  
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


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.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);

...
Feb 20 '08 #2
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:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using TheMembershipProvider;
  12.  
  13. public partial class _Default : System.Web.UI.Page
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.     }
  18.  
  19.     protected void OnLoggingIn(object sender, EventArgs e)
  20.     {
  21.         //Set User rights to db
  22.         //SetUserData("LakinPortal", Login1.UserName, Login1.Password);
  23.         SetUserData("LakinPortal", "joe", "joepw"); //just a test
  24.  
  25.     }
  26.  
  27.     public void SetUserData(string AppName, string username, string password)
  28.     {
  29.         DataTable ds = null;
  30.         string strPwd;
  31.         int Roweffected = 0;
  32.  
  33.         try
  34.         {
  35.             TheMembershipProvider.TheMembershipProvider Mem = new TheMembershipProvider.TheMembershipProvider();
  36.  
  37.             Roweffected = Mem.GetUserSecParams(username, password, ref ds);
  38.  
  39.             if (Roweffected > 0)
  40.             {…
  41.  
And here is a piece of the code from TheMembershipProvider dll which is custom:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Reflection;
  3. using System.Web;
  4. using System.Data;
  5. using System.Configuration;
  6. using System.Collections;
  7. using System.Web.Security;
  8. using System.IO;
  9. using System.Security.Cryptography;
  10. using System.Collections.Specialized;
  11. using System.Data.SqlClient;
  12. using TheDatabase.Utilities.Database;
  13.  
  14. namespace TheMembershipProvider
  15. {
  16.     public class TheMembershipProvider: MembershipProvider
  17.     {
  18. . . .
  19.  
  20.         public int GetUserSecParams(string username, string password, ref DataTable d)
  21.         {
  22.             DBConString = GetConnectionString();
  23.  
  24.             try
  25.             {
  26.                 SqlParameter[] p = new SqlParameter[2];
  27.  
  28.                 SqlParameter a = new SqlParameter("@userid", SqlDbType.VarChar);
  29.                 a.Value = username;
  30.                 a.Direction = System.Data.ParameterDirection.Input;
  31.                 p[0] = a;
  32.  
  33.                 SqlParameter b = new SqlParameter("@password", SqlDbType.VarChar);
  34.                 b.Value = password;
  35.                 b.Direction = System.Data.ParameterDirection.Input;
  36.                 p[1] = b;
  37.  
  38.  
  39.                 TheDatabase.Utilities.Database.TheDatabase LakinDB = new TheDatabase.Utilities.Database.TheDatabase(DBConString);
  40.                 int RowsAffected = 0;
  41.                 RowsAffected = TheDB.ExecuteQueryStoredProcedure("getdata", ref p, ref d);
  42.  
  43.                 return RowsAffected;
  44.             }
  45.             catch (Exception ex)
  46.             {
  47.                 throw ex;
  48.             }
  49.         }
  50. . . .
  51.  
Feb 20 '08 #3
Plater
7,872 Expert 4TB
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()?
Feb 20 '08 #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()?
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.InvalidCastException: 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:


[InvalidCastException: Specified cast is not valid.]
_Default.SetUserData(String AppName, String username, String password) +1194
_Default.OnLoggingIn(Object sender, EventArgs e) +23
System.Web.UI.WebControls.Login.OnLoggingIn(LoginC ancelEventArgs e) +105
System.Web.UI.WebControls.Login.AttemptLogin() +67
System.Web.UI.WebControls.Login.OnBubbleEvent(Obje ct source, EventArgs e) +99
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.Button.OnCommand(Command EventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +163
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433
Feb 20 '08 #5
Plater
7,872 Expert 4TB
That is very strange.
Are you able to run a debug session on the computer that it fails on at all?
Feb 20 '08 #6
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.
Feb 20 '08 #7

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

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
4
by: Tyro | last post by:
Can someone shed some light on my error here? Thanks! Specified cast is not valid. Exception Details: System.InvalidCastException: Specified cast is not valid. Source Error: Stack Trace:
3
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
3
by: VB Programmer | last post by:
I am setting up forms authentication. In my code I keep getting this error. Any ideas? Error.... Server Error in '/LandOLots' Application....
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
0
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No cross-posting intended. Thanks....
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
2
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { Microsoft.Web.UI.WebControls.TreeNode menuNode = null; ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.