0
votes

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
Do not do select count because it will give you the number of records. Get the actual info you need and then store it in the session.CodingYoshi
i used select count * . i am getting the information because i logged in thats why i have to check for the username and password using the database @CodingYoshiWen XIANG 1999
Run the query select count * using management studio or another tool and see what you get. select count * will not work because it Has syntax error. select count(*) will work but that will return a number. You need to get username, email, name etc (whatever you need) so use select username, email...CodingYoshi

1 Answers

0
votes
  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 }