1

There is one UserControl in my Admin Aspx Page and Second is Aspx Page.I used Session to get Userid of login user from UserControl in Admin Aspx Page, but that Returns Null What i am writing is :

UserControl In Admin Aspx Page:

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

//////////////////// Design Code of above Class ////////////////////////////

<%@ 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>

Other Aspx Page to Get Session User id:

 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

Following Reference to Store Session Is: http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.aspx

17
  • so ab is null or what else? Commented Nov 30, 2013 at 16:55
  • yes @Grundy it's null :( Commented Nov 30, 2013 at 16:59
  • 2
    @user2835256: from the code in Admin.aspx you are returning from the current page in either if or else right? so further code i mean after if-else block code is getting executed? Commented Nov 30, 2013 at 17:11
  • 1
    @user2835256 : but if that is the case then there is no way of getting the null reference Exception right? Commented Nov 30, 2013 at 17:20
  • 1
    @user2835256 : it is because - you are returning from the current page either from the if or else block , please check Tameen Mailk has given you answer, please check it. Commented Nov 30, 2013 at 17:34

2 Answers 2

2

I agree with – Sudhakar Tillapudi - you are returning your page. Please try:

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);
                    }
Sign up to request clarification or add additional context in comments.

Comments

2

I cannot see ASPxLabel1 in the aspx file. if you have changed the name in the front end sometimes VS forget or cannot change it try to rename it what you want in the deginer.cs file of your page.

1 Comment

No. That's Code of class defined above it/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.