简体   繁体   中英

How to get/display the user-information after logging in. ASP.net C#

I am implementing an online voting system for my school-project.

After the voter's log-in, i want to display their name, and ID in the label control at the content body. I try to use SESSION to store the voter's username in the log-in page but I'm not sure of my syntax because nothings happen.

I want to know the other way of retrieving a data from database! Please teach me.

public void GetInformation()
{

    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = connection;
    cmd.CommandText = "SELECT * FROM tblUsers WHERE voter_name = '" + Session["VotersID"] + "'";
    OleDbDataReader reader = cmd.ExecuteReader();
    if(reader.Read())
    {
        lblVoterName.Text = reader["usr_FirstN"].ToString() + " " + reader["usr_LastN"].ToString();
    }

}

Please Help Me. Thanks! -

You can use LogonUserIdentity as follow

if (Request.LogonUserIdentity.IsAuthenticated)
   lblName.Text = Request.LogonUserIdentity.Name;

just add this namespace:

using Microsoft.AspNet.Identity;

then you can get LoggedInUserId by:

User.Identity.GetUserId();

Or

HttpContext.Current.User.Identity.GetUserId();

So you don't need to use session to keep UserId.

Also you can create Custom Identity and instead of save Username in Name property, storing custom string Store User Data in ASP.NET Identity

get session data and send to one page(register.aspx) to another page(user_home.aspx)

        Session["remail2"] = txtemailsignin.Text;
        Server.Transfer("user_home.aspx", true); 

display the user-information after logging

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["socailweb"].ConnectionString);
        string sql = "select * from tblUsers  where remail='" + Session["remail2"] + "'";
        con.Open();
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sqldr = cmd.ExecuteReader();
        if (sqldr.Read() == true)
        {

            lblVotersID.Text = sqldr.GetValue(2).ToString();
            lblVoterName.Text = sqldr.GetValue(3).ToString();


        }

        sqldr.Close();
        con.Close();

@Honey Maglangit , what you use is PARAMETER not SESSION.

Response.Redirect("VoterPage.aspx?VotersID="+VoterUsername.Text);

So, you should get your VotersID by this way:

public void GetInformation()
{
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = connection;
    cmd.CommandText = "SELECT * FROM tblUsers WHERE voter_name = '" + Request.QueryString["VotersID"].ToString() + "'";
    OleDbDataReader reader = cmd.ExecuteReader();
    if(reader.Read())
    {
        lblVoterName.Text = reader["usr_FirstN"].ToString() + " " + reader["usr_LastN"].ToString();
    }
}

Try it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM