指定演员表无效

时间:2011-03-11 11:15:51

标签: c# .net asp.net

目前我正在努力让某个网站(asp.net)保持足够长的时间,以便在PHP中构建它。但是我对.net或asp.net不太了解。

我们收到此错误:

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

看起来在从数据库或其他东西投射结果时出现问题。使用的代码:

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;
        }
    }
}

有谁可以告诉我哪里出错了?

3 个答案:

答案 0 :(得分:2)

很可能ID列的类型已更改,并且该值不适合int。或者它总是比int更大的类型,但只是变得太大而不适合int。

如果数据库中的ID仍然是数字,但是太大了,那么碰撞属性并从int转换为更大的东西(如很长)应该可以解决问题。

答案 1 :(得分:1)

SQL查询返回一个无法转换为int的ID(例如,包含非数字字符的字符串,或者根本没有)。

 iD = (int)SqlHelper.ExecuteScalar(
                        conn,
                        CommandType.Text,
                        "SELECT ID From ledenlijst WHERE email = '" + email + "'");

查看Int32.TryParse()方法,该方法尝试将字符串解析为整数,但如果不能则不会抛出异常。

答案 2 :(得分:1)

ExecuteScalar返回的内容不是整数。调试应用程序并通过SqlHelper.ExecuteScalar方法检查第23行返回的内容。

修改

如果返回的值是int之外的其他类型,则必须确保对该类型使用正确的转换/强制转换。如果值为null,则应用程序数据/逻辑中可能存在一些错误,导致sql语句不返回任何行。您必须确保数据一致或更改逻辑以考虑空案例。

如果您仍然无法弄清楚什么是错误的,请尝试手动向DB执行查询并检查返回的内容。