I'd like to see and hear some reviews on my current WinForms Login and Dashboard code.
It's a desktop application to be used only internally and not over the web.
If you have any code suggestions - I'd be more than happy to see it and see where I can improve.
LoginScreen.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Technical_Application.Classes;
using Technical_Application.Forms;
namespace Technical_Application
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
if (Properties.Settings.Default.username != string.Empty)
{
usernameTextBox.Text = Properties.Settings.Default.username;
}
}
//Login Button
private void loginBtn_Click_1(object sender, EventArgs e)
{
//MySQL connection to retrive user details into a class on succesfull log in
using (var conn = new MySqlConnection(ConnectionString.ConnString))
{
conn.Open();
//Get count, username, id, and their access id
using (var cmd = new MySqlCommand("select count(*), username, id, access from users where username = @username and password = MD5(@password)", conn))
{
cmd.Parameters.AddWithValue("@username", usernameTextBox.Text);
cmd.Parameters.AddWithValue("password", passwordTextBox.Text);
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
//Check whether user exists
if (dt.Rows[0][0].ToString() == "1")
{
//If the user exist - allow to log in
//Store the infrmation from the query to UserDetails class to be used in other forms
UserDetails.Username = dt.Rows[0][1].ToString();
UserDetails.userId = (int)dt.Rows[0][2];
UserDetails.accessId = (int)dt.Rows[0][3];
//Save the username details - for future logins
Properties.Settings.Default.username = dt.Rows[0][1].ToString();
Properties.Settings.Default.Save();
//Hide this form and open the main Dashboard Form
this.Hide();
var dashboard = new Dashboard();
dashboard.Closed += (s, args) => this.Close();
dashboard.Show();
}
//If failed login - show message
else
{
MessageBox.Show("Login failed", "Technical - Login Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
}
}
UserDetails.cs
namespace Technical_Application.Classes
{
public class UserDetails
{
private static string _username;
private static int _userId;
private static int _accessId;
public static string Username
{
get
{
return _username;
}
set
{
_username = value;
}
}
public static int userId
{
get
{
return _userId;
}
set
{
_userId = value;
}
}
public static int accessId
{
get
{
return _accessId;
}
set
{
_accessId = value;
}
}
}
}
Dashboard.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Technical_Application.Classes;
using Technical_Application.Forms.Settings.Admin;
using Tulpep.NotificationWindow;
using MySql.Data.MySqlClient;
using Technical_Application.Forms.SubForm.FSQM;
namespace Technical_Application.Forms
{
public partial class Dashboard : Form
{
public Dashboard()
{
InitializeComponent();
//Show welcome message
welcomeText.Text = $"Hello {UserDetails.Username}!";
//Depending on the users accessID load specific content only
if (UserDetails.accessId == 1)
{
}
if (UserDetails.accessId == 2)
{
viewRequestedDocumentsToolStripMenuItem.Visible = false;
adminControlToolStripMenuItem.Visible = false;
}
if (UserDetails.accessId == 3)
{
requestDocumentAmendmentToolStripMenuItem.Visible = false;
viewRequestedDocumentsToolStripMenuItem.Visible = false;
labelRoom.Visible = false;
adminControlToolStripMenuItem.Visible = false;
}
if (UserDetails.accessId == 4)
{
technical.Visible = false;
labelRoom.Visible = false;
production.Visible = false;
settings.Visible = false;
welcomeText.Text = $"Hello {UserDetails.Username}! \n Your account is set as inactive.";
}
}
//Check if form is already open
private Form findForm(string text)
{
var found = Application.OpenForms.Cast<Form>().FirstOrDefault(f => f.Text == text);
if (!(found is null))
{
found.BringToFront();
if (found.WindowState == FormWindowState.Minimized)
{
found.WindowState = FormWindowState.Normal;
}
}
return found;
}
private void requestDocumentAmendmentToolStripMenuItem_Click(object sender, EventArgs e)
{
var found = findForm("Request Document Amendment");
if (found is null)
{
var newDoc = new Request_Document_Amendment();
newDoc.Show();
}
}
private void viewRequestedDocumentsToolStripMenuItem_Click(object sender, EventArgs e)
{
var found = findForm("View Requested Documents");
if (found is null)
{
var viewDoc = new ViewRequestedDocuments();
viewDoc.Show();
}
}
private void labelFormToolStripMenuItem_Click(object sender, EventArgs e)
{
var found = findForm("Label Room");
if (found is null)
{
var labelForm = new Technical_Application.Forms.Label_Form___Label_Room.Main();
labelForm.Show();
}
}
private void requestLabelSignOffToolStripMenuItem_Click(object sender, EventArgs e)
{
var found = findForm("Request Label Sign Off");
if (found is null)
{
var request_Label = new Request_Label_Sign_Off();
request_Label.Show();
}
}
private void labelSignOffSheetsToolStripMenuItem_Click(object sender, EventArgs e)
{
var found = findForm("Sign Off");
if (found is null)
{
var labelSignOff = new LabelSignOff();
labelSignOff.Show();
}
}
private void requestLabelsToolStripMenuItem_Click(object sender, EventArgs e)
{
var found = findForm("Production");
if (found is null)
{
var request_labels = new Technical_Application.Forms.Label_Form___Production.Main();
request_labels.Show();
}
}
private void adminControlToolStripMenuItem_Click(object sender, EventArgs e)
{
var found = findForm("Admin Control");
if (found is null)
{
var adminControl = new Admin_Control();
adminControl.Show();
}
}
private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
{
var found = findForm("Change Password - User");
if (found is null)
{
var changePassword = new Technical_Application.Forms.Settings.User.changePassword();
changePassword.Show();
}
}
private void FoodSafetyQM_Click(object sender, EventArgs e)
{
var found = findForm("FSQM");
if (found is null)
{
var FSQM = new FSQMMain();
FSQM.Show();
}
}
}
}