此刻,我正在嘗試保持某個站點(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;
}
}
}
有人可以告訴我這是哪裏出錯了嗎?
代碼寫入的方式看起來總是會有一個有效的ID作爲SQL語句「SELECT ID from ledenlijst」的結果。如果沒有可能導致InvalidCastException的記錄被返回。另一個原因可能是ID是int以外的字段。這段代碼是否有時會工作,有時會失敗? – rsbarro 2011-03-11 11:22:11