locked
User profile ID on all web forms RRS feed

  • Question

  • User-1994446809 posted

    Hello,

    I am trying to make the user profile ID appear in all my web forms, and on the navigation bar. I already have a web form where the user profile ID displays on the navbar, after the user login.

    From the .cs login code behind, when a user logs in and it redirects the user to the next page, it displays the user profile ID in that redirected page

    Login.aspx.cs

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Username.Text != "" & Password.Text != "")
                {
                string check = "select count(*) from [Login] where user_name = '" + Username.Text + "' and pass = '" + Password.Text + "' ";
                    SqlCommand com = new SqlCommand(check, con);
                    con.Open();
                    int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
                    con.Close();
    
                    if (temp == 1)
                   {
    
                    string user = Username.Text.Trim();
                    com.CommandText = "select * from login where user_name='" + Username.Text + "'";
                    com.Connection = con;
                    sda.SelectCommand = com;
                    sda.Fill(ds, "detail");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Session["user"] = user;
                        Username.Text = "";
                        Password.Text = "";
                        Response.Redirect("Home.aspx");
                    }
                       
                    }
    
                    else
                   {
                        Label7.ForeColor = System.Drawing.Color.Red;
                        Label7.Text = "Invalid Username or Password";
                       Username.Text = "";
                        Password.Text = "";
                    }
                }
            else
            {
                Label7.ForeColor = System.Drawing.Color.Red;
              Label7.Text = "Empty Data";
     }
        }

    From the Home.aspx web form, the label used in displaying the user ID after login is shown in the yellow highlighted area in the code below, and the Home.aspx.cs code behind binds the user profile ID to the label.

    Home.aspx

                <div class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation" style="background-color: #00003D; font-family: Nunito;">
                    <div class="container">
                        <div class="navbar-header">
                            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse" style="background-color: #00003D; border-color:white; border-width:1px; color: white">
                                <span class="sr-only">Toggle Navigation</span>
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                            </button>
                            <a class="navbar-brand" style="color: white; margin-top:-3%; font-family:Goudy Old Style;"><span><img alt="LOGO22" src="images/LOGO22.png" height="40"/>&nbsp;</span>JOSCHECK</a>
                            
                        </div>
                        <div class="navbar-collapse collapse">
                            <ul class="nav navbar-nav navbar-right">
                                <li><a href="Home.aspx" style="color: #FFFFFF">| Home</a></li>
                                <li><a href="AcustomID.aspx" style="color: #FFFFFF">| About</a></li>
                                <li class="dropdwon">
                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" style="color: #FFFFFF">| Car<b class="caret"></b></a>
                                    <ul class="dropdown-menu" style="background-color: #00003D;">
                                        <li class="dropdown-header"><a href="VehicleRegistration.aspx" style="color: #FFFFFF; background-color: #00003D;">Register Vehicle</a></li>
                                        <li class="dropdown-header"><a href="Report.aspx" style="color: #FFFFFF; background-color: #00003D;">Report</a></li>
                                        <li class="dropdown-header"><a href="Verification.aspx" style="color: #FFFFFF; background-color: #00003D;">Verify Vehicle</a></li>
                                    </ul>
                                </li>
                                <li class="dropdwon">
                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" style="color: #FFFFFF">| Phone<b class="caret"></b></a>
                                    <ul class="dropdown-menu" style="background-color: #00003D;">
                                        <li class="dropdown-header"><a href="PhoneRegistration.aspx" style="color: #FFFFFF; background-color: #00003D;">Register Phone</a></li>
                                        <li class="dropdown-header"><a href="PhoneReport.aspx" style="color: #FFFFFF; background-color: #00003D;">Report</a></li>
                                        <li class="dropdown-header"><a href="PhoneVerification.aspx" style="color: #FFFFFF; background-color: #00003D;">Phone Check</a></li>
                                    </ul>
                                </li>
                                <li class="dropdwon">
                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" style="color: #FFFFFF">| QR Code <b class="caret"></b></a>
                                    <ul class="dropdown-menu" style="background-color: #00003D;">
                                        <li class="dropdown-header"><a href="UploadDesign.aspx" style="color: #FFFFFF; background-color: #00003D;">Generate QR Code</a></li>
                                        <li class="dropdown-header"><a href="QRScan.aspx" style="color: #FFFFFF; background-color: #00003D;">Check QR Code</a></li>
                                    </ul>
                                </li>
                               <li>
                                  <a><asp:Label ID="user" runat="server" Font-Size="Large" ForeColor="White">User</asp:Label></a>
                               </li>
                                <li style="margin-right:10px;">
                                    <asp:Button ID="btnSignOut" runat="server" class="btn btn-primary navbar-btn" Text="Sign out" OnClick="btnSignOut_Click"/>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>

    Home.aspx.cs

    public partial class Admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            showdata();
        }
    
        public void showdata()
        {
            SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from login where user_name='" + Session["user"] + "'";
            cmd.Connection = con;
            SqlDataAdapter sda = new SqlDataAdapter();
            DataSet ds = new DataSet();
            sda.SelectCommand = cmd;
            sda.Fill(ds);
            user.Text = ds.Tables[0].Rows[0]["user_name"].ToString();
        }
    }

    Where I have an issue is that after all this has been done, the user decides to move on to another web page, there user profile ID will not be displayed on these subsequent web forms; it only displays on the Homepage. I want the user profile ID to be tied to all other forms when a user login or to display the profile ID on all web forms upon user login. Please how do I go about this? I will be grateful if I get help on this.

    Thank you,

    George 

    Sunday, May 17, 2020 8:46 PM

Answers

  • User-719153870 posted

    Hi George A,

    does it mean that I have to attach the same line of code to all other forms to display the user ID as done in the Home form?

    Yes, as long as the session has not expired, below code can always show your user_name:

    user.Text = Session["user"].ToString();

    Check Exploring Session in ASP.NET for deeper understanding of Session in ASP.NET if you are interested.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2020 7:15 AM

All replies

  • User-719153870 posted

    Hi,  georgeakpan233

    I found that after you log in, the user ID is stored in the session. So as long as the session has not expired, all your pages can use it directly, without the need to query again.

    For example:

     protected void Page_Load(object sender, EventArgs e)
            { 
    if (!IsPostBack) {
                    user.Text = Session["user"].ToString();
                }
           }
    

    Or you can put it in the master page, so you only need to process in the code behind of the master page to achieve your needs.

    If you have not used the master page, then I suggest you can refer to this:

    https://www.c-sharpcorner.com/article/how-to-create-master-page-in-asp-net/

    Hope this can help you.

    In addition, may i ask why you need to query again in Home.aspx.cs when you already got the user_name from session?

    Best Regard,

    Yang Shen

    Monday, May 18, 2020 3:07 AM
  • User-1994446809 posted
    Hi Yang Shen,
    I am not using Master page, I designed all the forms separately. Since you mentioned that all the pages will still be using the logged in user ID as long as the session has not expired, does it mean that I have to attach the same line of code to all other forms to display the user ID as done in the Home form? Or what should I do?

    George A.
    Monday, May 18, 2020 6:28 AM
  • User-719153870 posted

    Hi George A,

    does it mean that I have to attach the same line of code to all other forms to display the user ID as done in the Home form?

    Yes, as long as the session has not expired, below code can always show your user_name:

    user.Text = Session["user"].ToString();

    Check Exploring Session in ASP.NET for deeper understanding of Session in ASP.NET if you are interested.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2020 7:15 AM
  • User-1994446809 posted
    Thanks a lot. I really appreciate.
    George
    Monday, May 18, 2020 7:19 AM