locked
Strange error Guid currentUserId = (Guid)currentUser.ProviderUserKey; RRS feed

  • Question

  • User1557098506 posted

    Hi,

    I am trying to retrieve the user profile from the database.

    I have strange problem that is If I login with this login page which I have to use because I am using LOGINVIEW controls :

    using System;
    
    using System.Collections;
    
    using System.Configuration;
    
    using System.Data;
    
    using System.Linq;
    
    using System.Web;
    
    using System.Web.Security;
    
    using System.Web.UI;
    
    using System.Web.UI.HtmlControls;
    
    using System.Web.UI.WebControls;
    
    using System.Web.UI.WebControls.WebParts;
    
    using System.Xml.Linq;
    
    using System.Data.SqlClient;
    
    
    public partial class userpanel : System.Web.UI.MasterPage
    {
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            
           
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Session.Clear();
            Session.Abandon();
            Response.Redirect("~/index.aspx");
        }
        protected void btn_login_Click(object sender, EventArgs e)
        {
            TextBox user = LoginView1.FindControl("TextBox_user_name") as TextBox;
    TextBox password = LoginView1.FindControl("TextBox_password") as TextBox;
    
    if (user != null && password != null)
    {
        if (Membership.ValidateUser(user.Text, password.Text))
        {
    
                 FormsAuthentication.SetAuthCookie(user.Text, true);
            FormsAuthentication.RedirectFromLoginPage(password.Text.ToString(), false);
            Response.Redirect("/Details.aspx");
    
            // etc... inclusing using cb.Checked where needed.
    
    
    
    
        }
    
        else
        {
    
            Label lb1 = (Label)LoginView1.FindControl("lb1");
    
            lb1.Text = "username or password is incorrect";  
        }
    }
        }
    
    
      
    }
    
    
    

    I get this error on profile page :

     Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error:
    
    
    Line 46: 
    Line 47:             // Determine the currently logged on user's UserId value
    Line 48:             Guid currentUserId = (Guid)currentUser.ProviderUserKey;
    Line 49: 
    Line 50:             // Assign the currently logged on user's UserId to the @UserId parameter
    
    

    But If I use another Login page with this code:

    using System.Configuration;
    
    using System.Data;
    
    using System.Linq;
    
    using System.Web;
    
    using System.Web.Security;
    
    using System.Web.UI;
    
    using System.Web.UI.HtmlControls;
    
    using System.Web.UI.WebControls;
    
    using System.Web.UI.WebControls.WebParts;
    
    using System.Xml.Linq;
    
    using System.Data.SqlClient;
    
    namespace Display_image_username_after_login
    {
    
        public partial class Login : System.Web.UI.Page
        {
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
    
        protected void  btn_login_Click(object sender, EventArgs e)
    {
    
        if (Membership.ValidateUser(TextBox_user_name.Text.ToString(), TextBox_password.Text.ToString()))  
            {
                FormsAuthentication.RedirectFromLoginPage(TextBox_user_name.Text.ToString(), false);
                  
        }  
            else {
                lb1.Text= "username or password is incorrect";  
            }  
    }
    }
    
         
    }

    I don't received any error and everythings work fine.

    But I have to use login page with control view. How to make it work?

    This is my Profile page :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Security;
    
    public partial class test_Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void UserProfileDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
    
    
            // Get a reference to the currently logged on user
            MembershipUser currentUser = Membership.GetUser();
    
            // Determine the currently logged on user's UserId value
            Guid currentUserId = (Guid)currentUser.ProviderUserKey;
    
            // Assign the currently logged on user's UserId to the @UserId parameter
            e.Command.Parameters["@UserId"].Value = currentUserId;
        }
    
        protected void UserProfile_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            SettingsUpdatedMessage.Visible = true;
        }
    }

    Any suggestion please?

    Saturday, June 28, 2014 2:35 PM

Answers

  • User697462465 posted

    Hi new2world,

    From your error msg:Object reference not set to an instance of an object.

    You need to confirm the user have loged successfully, if not, this error will be arised.

    So, please set a checkpoint in following code, to check the ValidateUser have return true:

    if (Membership.ValidateUser(user.Text, password.Text))
        {
    
                 FormsAuthentication.SetAuthCookie(user.Text, true);
            FormsAuthentication.RedirectFromLoginPage(password.Text.ToString(), false);
            Response.Redirect("/Details.aspx");
    
            // etc... inclusing using cb.Checked where needed.
    
    
    
    
        }
    

    Best Regards,
    Terry Guo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 30, 2014 3:02 AM