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

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
user2835256
  • 407
  • 2
  • 13
  • 27

2 Answers2

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.

Onur Topal
  • 3,042
  • 1
  • 24
  • 41
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);
                    }
Tameen Malik
  • 1,258
  • 6
  • 17
  • 45