// Login button private void Button_Login_Click(object sender, EventArgs e) { ManageContacts_DB db = new ManageContacts_DB(); DataTable table = new DataTable(); using (SqlCommand command = new SqlCommand("uspLogin", db.GetConnection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@Username", SqlDbType.VarChar, 100)); command.Parameters["@Username"].Value = this.textBoxUsername.Text; command.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar, 20)); command.Parameters["@Password"].Value = this.textBoxPassword.Text; using (SqlDataAdapter adapter = new SqlDataAdapter()) { try { db.OpenConnection(); adapter.SelectCommand = command; adapter.Fill(table); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { db.CloseConnection(); } } } // Check for empty fields if (VerifyFields("login")) { // Check if this user exists if (table.Rows.Count > 0) { // Get user Id int userId = Convert.ToInt32(table.Rows[0][0].ToString()); Globals.SetGlobalUserId(userId); // Show the Main App From this.DialogResult = DialogResult.OK; } else { // Show an Error Message this.DialogResult = MessageBox.Show("Invalid Username or Password", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error); this.DialogResult = DialogResult.None; } } else { // Show an Error Message this.DialogResult = MessageBox.Show("Empty Username or Password", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error); this.DialogResult = DialogResult.None; } }