简体   繁体   中英

how to store into a session and display current users information

public class UserDA { private static string _connStr = ConfigurationManager.ConnectionStrings["airmazin"].ConnectionString;

//update,delete,insert,getuser

public static void UserInsert(User user)
{

    string queryStr = "INSERT INTO Account (username, gender, email, password) values (@username, @gender, @email, @password);";

    SqlConnection conn = new SqlConnection(_connStr);
    SqlCommand cmd = new SqlCommand(queryStr, conn);
    cmd.Parameters.AddWithValue("@username", user.userName);
    cmd.Parameters.AddWithValue("@gender", user.gender);
    cmd.Parameters.AddWithValue("@email", user.email);
    cmd.Parameters.AddWithValue("@password", user.password);

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();

}

protected void btn_Submit_Click(object sender, EventArgs e) {

    string userName = tb_userName.Text;
    string gender = rbl_gender.SelectedValue.ToString();
    string email = tb_email.Text;
    string password = tb_password.Text;
    User user = new User(userName, gender, email, password);
    UserDA.UserInsert(user);

}

protected void Page_Load(object sender, EventArgs e) {

}

protected void btn_Login_Click(object sender, EventArgs e)
{
    //if (txt_userEmail == null) { }
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["airmazin"].ToString());
    con.Open(); 
    string query = "select count   from Account where email='" + txt_userEmail.Text+ "' and password='" + txt_userPass.Text +"'" ;

   SqlCommand cmd = new SqlCommand(query, con);
    string output = cmd.ExecuteScalar().ToString();
    con.Close();
    if (output == "1")
    {
        // Create a session
        Session["Account"] = txt_userEmail.Text;


        Response.Redirect("User_Profile.aspx");

   }

   else
    {
        Response.Write("<script>alert('Login NOT successful');</script>");
    }
}

protected void btn_loginSignup_Click(object sender, EventArgs e)
{
    Response.Redirect("SignUp.aspx");
}

}

I have inserted data into the data base and how do I store it into a session and display the current users information(Eg: Username, email, gender etc) on my profile page.

  1. You can access the session value in aspx by <%=Session["Account"]%>
  2. You should wrap the SqlConnection in a using statement, otherwise, you connection is not released when exception.

    using (SqlConnection connection = new SqlConnection(connectionString)) { //your code }

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