//takes user back to home if cancel button is clicked private void Btn_LoginCancel_Click(object sender, EventArgs e) { home = true; Txtb_LoginUsername.ResetText(); Txtb_LoginPassword.ResetText(); Visibility(); }
//logs user in if correct details are entered and displays a greeting at top of app containing username private void Btn_Login_Click(object sender, EventArgs e) { try { using (SqlConnection conn = new SqlConnection(connstring)) { using (SqlCommand cmd = new SqlCommand("select Count(*) from application.dbo.[User] WHERE Username=@A and Password=@B", conn)) { conn.Open(); cmd.Parameters.AddWithValue("@A", Txtb_LoginUsername.Text); cmd.Parameters.AddWithValue("@B", Txtb_LoginPassword.Text); int result = (int)cmd.ExecuteScalar(); if (result > 0) { if (Link_Login.Text == "Login") { MessageBox.Show("Successful login!"); home = true; Load_Profile(); Lbl_LoggedIn.Text = "Hi, " + Txtb_LoginUsername.Text; loggedinas = Txtb_LoginUsername.Text; Lbl_LoggedIn.Visible = true; Visibility(); Link_Register.Visible = false; Link_Login.Text = "Logout"; Txtb_LoginUsername.ResetText(); Txtb_LoginPassword.ResetText(); } } else { MessageBox.Show("Incorrect login details! Please try again!"); Txtb_LoginUsername.ResetText(); Txtb_LoginPassword.ResetText(); } } conn.Close(); conn.Dispose(); } } catch (Exception ex) { MessageBox.Show("Unexpected error: " + ex.Message); } }
//sets visibility of objects when login link is clicked private void Link_Login_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Lbl_PageTitle.Text = "Login"; Txtb_LoginUsername.ResetText(); Txtb_LoginPassword.ResetText(); login = true; reg = false; add = false; inbox = false; home = false; prof = false; //resets profile image stream if (Link_Login.Text == "Logout") { Link_Login.Text = "Login"; Lbl_LoggedIn.Text = ""; Link_Register.Visible = true; loggedinas = ""; PicB_ProfilePhoto.Image = null; } Visibility(); }