Asp.Net中的空对象引用

时间:2013-11-30 16:47:57

标签: c# asp.net .net nullreferenceexception

我的Admin Aspx页面中有一个UserControl,第二个是Aspx页面。我使用Session从Admin Aspx页面的UserControl获取登录用户的Userid,但返回Null我写的是:

User Astrol在Admin Aspx页面:

 protected void btnLogin_Click(object sender, EventArgs e) {
        int flag = 0;
         Entities db = new Entities();
        foreach (var s in db.Users)
        {
            if (tbUserName.Text==s.user_name && tbPassword.Text == s.user_password)
            {

                //  if (Membership.ValidateUser(tbUserName.Text, tbPassword.Text)) {
                if (string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
                {
                    FormsAuthentication.SetAuthCookie(tbUserName.Text, false);
                    Response.Redirect("~/");
                }
                else
                {
                    FormsAuthentication.RedirectFromLoginPage(tbUserName.Text, false);
                }
                flag = 1;
                string sessionUserId = Session["UserId"] as string;

                if (string.IsNullOrEmpty(sessionUserId))
                {

                    Session["UserId"] = Server.HtmlEncode(s.user_id.ToString());
                }
                break;
            }
            else
                flag=0;
        }
          if(flag==0)
           {
                tbUserName.ErrorText = "Invalid user";
                tbUserName.IsValid = false;
            }
    }

////////////////////上述类的设计代码////////////////////// //////

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Light.master" CodeBehind="Login.aspx.cs" Inherits="DXApplication5.Login" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">

  <table border="0">

<tr>
<td>
<div class="accountHeader">
    <h2>
        Log In</h2>
</div>
<dx:ASPxLabel ID="lblUserName" runat="server" AssociatedControlID="tbUserName" Text="User Name:" />
    <div class="form-field">
    <dx:ASPxTextBox ID="tbUserName" runat="server" Width="200px">
        <ValidationSettings ValidationGroup="LoginUserValidationGroup">
            <RequiredField ErrorText="User Name is required." IsRequired="true" />
        </ValidationSettings>
    </dx:ASPxTextBox>
</div>
<dx:ASPxLabel ID="lblPassword" runat="server" AssociatedControlID="tbPassword" Text="Password:" />
<div class="form-field">
    <dx:ASPxTextBox ID="tbPassword" runat="server" Password="true" Width="200px">
        <ValidationSettings ValidationGroup="LoginUserValidationGroup">
            <RequiredField ErrorText="Password is required." IsRequired="true" />
        </ValidationSettings>
    </dx:ASPxTextBox>
</div>
</td>
</tr>
<tr>
<td>
<dx:ASPxButton ID="btnLogin" runat="server" Text="Log In" ValidationGroup="LoginUserValidationGroup"
    OnClick="btnLogin_Click">
</dx:ASPxButton>
</td>
</tr>
</table>

获取会话用户ID的其他Aspx页面:

 protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        string ab = Session["UserId"] as string;**// RETURNS NULL OVER HERE ? WHAT'S SOLUTION?**
        if (string.IsNullOrEmpty(ab))
        {

             ASPxLabel1.Text = "nOtHiNg";               
        }
        else
           ASPxLabel1.Text = ab;            
    }

STACK TRACE

[NullReferenceException: Object reference not set to an instance of an object.]
   DXApplication5.Products.ASPxButton1_Click(Object sender, EventArgs e) in C:\Users\Documents\Visual Studio 2010\Projects\DXApplication5\DXApplication5\Products.aspx.cs:29
   DevExpress.Web.ASPxEditors.ASPxButton.OnClick(EventArgs e) +113
   DevExpress.Web.ASPxEditors.ASPxButton.RaisePostBackEvent(String eventArgument) +626
   DevExpress.Web.ASPxClasses.ASPxWebControl.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

以下对商店会话的引用是: http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.aspx

2 个答案:

答案 0 :(得分:2)

我在aspx文件中看不到ASPxLabel1。如果您在前端更改了名称,有时VS忘记或无法更改它,请尝试在页面的deginer.cs文件中将其重命名为。

答案 1 :(得分:2)

我同意--Sudhakar Tillapudi - 你要回页了。请尝试:

if (string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
                    {
                        string sessionUserId = Session["UserId"] as string;

                        if (string.IsNullOrEmpty(sessionUserId))
                        {

                            Session["UserId"] = Server.HtmlEncode(s.user_id.ToString());
                        } FormsAuthentication.SetAuthCookie(tbUserName.Text, false);
                        Response.Redirect("~/");
                    }
                    else
                    {
                        string sessionUserId = Session["UserId"] as string;

                        if (string.IsNullOrEmpty(sessionUserId))
                        {

                            Session["UserId"] = Server.HtmlEncode(s.user_id.ToString());
                        }
                        FormsAuthentication.RedirectFromLoginPage(tbUserName.Text, false);
                    }