Hi, this is my first post here, and I am new to C# so it may be a stupid question. After a quick search I don't think it has been covered before, so apologies if it has.
I am writing a program that users will need to log in to, and I would like their userID and permissions code to be available for use in other forms.
At the moment I have this for when people try to log in:
Code:
private void loginUser()
{
SqlConnection con = new SqlConnection("Data Source=DOMAINSERVER;Initial Catalog=RightSortFulfilment;Integrated Security=True");
con.Open();
string checkUser = "SELECT * FROM Humanresources.EmployeeLogins WHERE UserName ='" + txtUser.Text + "'";
SqlCommand cmd = new SqlCommand(checkUser, con);
SqlDataReader drd = cmd.ExecuteReader();
if (drd.HasRows)
{
while (drd.Read())
{
if (drd["Password"].ToString() == txtPass.Text)
{
txtPermissions.Text = drd["Admin"].ToString();
txtUserID.Text = drd["EmployeeID"].ToString();
MainCompanyScreen companyScreen = new MainCompanyScreen();
companyScreen.Show();
/*Application.OpenForms[0].Show();
this.Hide();*/
}
else
{
MessageBox.Show("Password incorrect. Please try again.", "RightSort Fulfilment");
txtPass.ResetText();
txtPass.Focus();
}
}
}
else
{
MessageBox.Show("Username does not exist. Please try again.", "RightSort Fulfilment");
txtUser.ResetText();
txtPass.ResetText();
txtUser.Focus();
}
drd.Close();
con.Close();
}
and then I have this code on the next form to give users with Admin rights access to certain functions that normal users won't have:
Code:
private void MainCompanyScreen_Load(object sender, EventArgs e)
{
LoginScreen loginForm = new LoginScreen();
if (loginForm.txtPermissions.ToString() == "1")
{
staffToolStripMenuItem.Visible = true;
deleteCompanyToolStripMenuItem.Enabled = true;
amendCompanyToolStripMenuItem.Enabled = true;
newToolStripMenuItem.Enabled = true;
}
else
{
staffToolStripMenuItem.Visible = false;
deleteCompanyToolStripMenuItem.Enabled = false;
amendCompanyToolStripMenuItem.Enabled = false;
newToolStripMenuItem.Enabled = false;
}
}
This doesn't seem to want to work (this is similar to how I would have done this type of thing in Access/VBA in the past), so I was wondering if I was missing something very obvious or if I need to use a class or method instead.
Many thanks in advance for any help you can send my way!
EDIT: Just thought I should mention that the data filling the txtUserID and txtPermissions text boxes are set as integers in sql server, and that when I run the program and click to login it runs okay, but regardless of whether the Permissions number is 0 or 1 it still only runs the 'else' statement.