Reputation: 2062
At the moment I'm trying to keep a certain site (asp.net) alive long enough for its replacement to be build in php. However I don't know much about .net or asp.net.
We are getting this error:
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:
Line 21: string conn = ConfigurationSettings.AppSettings["DSN"];
Line 22: string email = (Membership.GetUser(HttpContext.Current.User.Identity.Name)).Email;
Line 23: iD = (int)SqlHelper.ExecuteScalar(
Line 24: conn,
Line 25: CommandType.Text,
Source File: e:\PKwebSX\app_code\PKMembershipProvider.cs Line: 23
Stack Trace:
[InvalidCastException: Specified cast is not valid.]
PKMembershipProvider.get_ID() in e:\PKwebSX\app_code\PKMembershipProvider.cs:23
ASP.mijnpk_ascx.Page_Load(Object sender, EventArgs e) in e:\PKwebSX\mijnpk.ascx:20
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +36
System.Web.UI.Control.OnLoad(EventArgs e) +102
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Control.LoadRecursive() +131
System.Web.UI.Control.LoadRecursive() +131
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1064
Version Information: Microsoft .NET Framework Version:2.0.50215.44; ASP.NET Version:2.0.50215.44
It looks like there's a problem casting a result from the database or something. The code used:
public sealed class PKMembershipProvider : SqlMembershipProvider
{
public int ID
{
get
{
int iD = 0;
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
string conn = ConfigurationSettings.AppSettings["DSN"];
string email = (Membership.GetUser(HttpContext.Current.User.Identity.Name)).Email;
iD = (int)SqlHelper.ExecuteScalar(
conn,
CommandType.Text,
"SELECT ID From ledenlijst WHERE email = '" + email + "'");
}
return iD;
}
}
}
Can anybody tell me where this is going wrong?
Upvotes: 0
Views: 18716
Reputation: 29851
The ExecuteScalar
is returning something that's not an integer. Debug the application and check what is returned at line 23 by the SqlHelper.ExecuteScalar
method.
Edit:
If the value returned is of another type than int you have to make sure you use the correct conversion/cast for that type. If the value is null, you probably have some error in the application data/logic causing no lines to be returned by the sql statement. You have to make sure the data is consistent or change the logic to take the null case into consideration.
If you still can't figure out what's wrong try executing the query manually towards the DB and check what is returned.
Upvotes: 1
Reputation: 4433
Most likely the type of the ID column has changed and the value doesn't fit into an int. Or it was always of a type bigger than an int, but has only just become too large to fit into an int.
If the ID in the database is still numeric, but is just too big, bumping the property and the cast up from an int to something bigger (like a long) should fix the issue.
Upvotes: 2
Reputation: 12630
The SQL query is returning an ID which cannot be cast to an int (e.g. an string with non-numeric characters in, or nothing at all).
iD = (int)SqlHelper.ExecuteScalar(
conn,
CommandType.Text,
"SELECT ID From ledenlijst WHERE email = '" + email + "'");
Have a look at the Int32.TryParse() method which attempts to parse a string as an integer but will not throw an exception if it cannot.
Upvotes: 1