ASP.NET中一個簡單的關於Session的問題的解決

來源:互聯網
上載者:User

用習慣了asp,總是把session沒有定義直接拿來使用.

今天做一個小小的東西卻出了問題,簡化地說:

1.做一個 設定session的頁面, 然後自動redirect 到顯示 session的頁面,這樣是沒有問題的;對,以前就這麼做的;

2. 今天一個老哥,他先訪問了顯示session的頁面,問題出來了,

說明: 執行當前 Web 請求期間,出現未處理的異常。請檢查堆疊追蹤資訊,以瞭解有關該錯誤以及代碼中導致錯誤的出處的詳細資料。

異常詳細資料: System.NullReferenceException: 未將對象引用設定到對象的執行個體。

源錯誤:

“/WebSite1”應用程式中的伺服器錯誤。
未將對象引用設定到對象的執行個體。

 

行 19:       //  }行 20: 行 21:         this.Label1.Text = Session["name"].ToString();行 22:     }行 23:     protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)

源檔案: d:/My Documents/Visual Studio 2005/WebSites/WebSite1/Default2.aspx.cs    行: 21

堆疊追蹤:

[NullReferenceException: 未將對象引用設定到對象的執行個體。]   Default2.Page_Load(Object sender, EventArgs e) in d:/My Documents/Visual Studio 2005/WebSites/WebSite1/Default2.aspx.cs:21   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +31   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +68   System.Web.UI.Control.OnLoad(EventArgs e) +88   System.Web.UI.Control.LoadRecursive() +74   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3034

解決辦法: 在顯示session的語句前面加上:

 if (Object.Equals(Session["name"],null) )
        {
            Session["name"]="沒有值";
       }

 

OK,問題解決.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.