一些我的aspx頁面的我檢查會話這樣爲什麼會話對象拋出一個空引用異常?
if (bool.Parse(Session["YourAssessment"].ToString()) == false
&& bool.Parse(Session["MyAssessment"].ToString()) == true)
{
Response.Redirect("~/myAssessment.aspx");
}
如果我繼續與頻繁的頁面打它工作正常,但如果我不爲5分鐘,頁面做任何事至少連,運行頁面引發錯誤
Object reference not set to an instance of an object.
以下是堆棧這
[NullReferenceException: Object reference not set to an instance of an object.]
yourAssessment.Page_Load(Object sender, EventArgs e) in d:\Projects\NexLev\yourAssessment.aspx.cs:27
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
可能身體有些解釋我這個怪異的行爲?
而且我們知道默認情況下會話的最後時間是20分鐘。
EDITED
見我有一個頁面默認的ASPX,它已經有一個按鈕,它修正了一些基礎上重定向到哪裏 在默認頁檢查它這樣
protected void Page_Load(object sender, EventArgs e)
{
if (!HttpContext.Current.Request.IsAuthenticated)
{
Response.Redirect("~/login.aspx");
}
else
{
Session["YourAssessment"] = false;
Session["MyAssessment"] = false;
}
}
按鈕點擊有
protected void imgClientFreeEval_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
if (HttpContext.Current.Request.IsAuthenticated)
{
string sqlQuery = "SELECT count(*) FROM SurveyClient WHERE UserID='"
+ cWebUtil.GetCurrentUserID().ToString() + "'";
SqlParameter[] arrParams = new SqlParameter[0];
int countSurvey = int.Parse(
Data.GetSQLScalerVarQueryResults(sqlQuery).ToString());
if (countSurvey > 0)
{
Session["YourAssessment"] = true;
Session["MyAssessment"] = false;
}
Response.Redirect((countSurvey > 0)
? "~/yourAssessment.aspx"
: "~/myAssessment.aspx");
}
else
{
Response.Redirect("~/login.aspx");
}
和myAssessment網頁上檢查這樣
protected void Page_Load(object sender, EventArgs e)
{
if (!HttpContext.Current.Request.IsAuthenticated)
{
Response.Redirect("~/login.aspx");
}
else
{
if (Session["YourAssessment"] != null
&& Session["MyAssessment"] != null
&& bool.Parse(Session["YourAssessment"].ToString())
&& !bool.Parse(Session["myAssessment"].ToString()))
{
Response.Redirect("~/yourAssessment.aspx");
}
}
}
和yourAssessmtn檢查它這樣
protected void Page_Load(object sender, EventArgs e)
{
if (!HttpContext.Current.Request.IsAuthenticated)
{
Response.Redirect("~/login.aspx");
}
else
{
if (Session["YourAssessment"] != null
&& Session["MyAssessment"] != null
&& !bool.Parse(Session["YourAssessment"].ToString())
&& bool.Parse(Session["MyAssessment"].ToString()))
{
Response.Redirect("~/myAssessment.aspx");
}
PopulateAllSurveyByUser();
if (ViewState["surveyClientID"] != null)
{
grdSurveyDetail.Visible = true;
PopulateSurveyDetails(
int.Parse(ViewState["surveyClientID"].ToString()));
}
else
{
grdSurveyDetail.Visible = false;
}
}
}
什麼是錯的,請解釋一下嗎?
因爲沒有「YourAssessment」或會話集合中的「MyAssessment」鍵 –
確定我的會話變爲空,但是如果它變爲null,我該做什麼操作? – NoviceToDotNet
以及你可以做的最好的事情是把價值在會話;)只是開玩笑,你在哪裏設置會話變量,顯示你的代碼 –