Basically this is my first login form. I am using SQL and C# WinForms. I made user roles such as "Admin" and others and the user is taken to a specific WinForms, according to his appointed role (appointed by me manually now).
I have also allowed users to create their new accounts, in which they pick their user name and password but the role still needs to be appointed by me. I'd like to know if what I've done is ok or if it needs improvement, and where.
namespace My_PROGRAM
{
public partial class Login : Form
{
SqlConnection loginCon = new SqlConnection("Data Source=******;Initial Catalog=***;Persist Security Info=True;User ID=*****;Password=**********");
public Login()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
loginCon.Open();
SqlDataAdapter loginAdapter = new SqlDataAdapter("SELECT [Role] FROM [dbo].[LOGIN_Tab] WHERE Name ='"+ userNameTextobx.Text +"' and Password='"+ userPasswordTextbox.Text +"' ", loginCon);
DataTable result = new DataTable();
loginAdapter.Fill(result);
try
{
if (result.Rows.Count == 1)
{
switch(result.Rows[0]["Role"] as string)
{
case "Admin":
{
this.Hide();
AdminMenu aMenu = new AdminMenu();
MessageBox.Show("Login was succesful. Welcome back " + userNameTextobx.Text + " !!");
aMenu.Show();
break;
}
case "Planner":
{
this.Hide();
PlannerMenu pMenu = new PlannerMenu();
MessageBox.Show("Login was succesful. Welcome back " + userNameTextobx.Text + " !!");
pMenu.Show();
break;
}
case "Operator":
this.Hide();
OperatorMenu oMenu = new OperatorMenu();
MessageBox.Show("Login was succesful. Welcome back " + userNameTextobx.Text + " !!");
oMenu.Show();
break;
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
loginCon.Close();
}
}
private void btnNewUser_Click(object sender, EventArgs e)
{
try
{
if (userNameTextobx.Text == "" || userNameTextobx.Text.Length < 4)
{
MessageBox.Show("User name too short, or you didn't write anything !");
userNameTextobx.Clear();
}
if (userPasswordTextbox.Text == "" || userPasswordTextbox.Text.Length < 5)
{
MessageBox.Show("Password too short, or you didn't write anything !");
userPasswordTextbox.Clear();
}
else
{
loginCon.Open();
bool exists = false;
SqlCommand checkCMD = new SqlCommand("SELECT COUNT(*) FROM [dbo].[LOGIN_Tab] WHERE [Name]= @Name", loginCon);
{
checkCMD.Parameters.AddWithValue("[Name]", userNameTextobx.Text);
exists = (int)checkCMD.ExecuteScalar() > 0;
}
if (exists)
{
MessageBox.Show("The name " + userNameTextobx.Text + ", already exists. Pick new name !");
userNameTextobx.Clear(); userPasswordTextbox.Clear();
}
else
{
SqlCommand insert = loginCon.CreateCommand();
insert.CommandType = CommandType.Text;
insert.CommandText = "INSERT INTO [dbo].[LOGIN_Tab] ([Name],[Password]) VALUES ('" + userNameTextobx.Text +
"','" + userPasswordTextbox.Text + "')";
insert.ExecuteNonQuery();
loginCon.Close();
MessageBox.Show("New user '" + userNameTextobx.Text + "' was created !");
userNameTextobx.Clear(); userPasswordTextbox.Clear();
}
}
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
loginCon.Close();
}
}
private void btnQuit_Click(object sender, EventArgs e)
{
//Environment.Exit(0);
Application.Exit();
}
}
}
Any suggestions are more than welcome and appreciated. Also, is there a good way for me after the user makes a new account, to appoint his role?