View Single Post
  #1 (permalink)  
Old April 17th, 2012, 08:36 AM
keefa3011 keefa3011 is offline
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hidden form containing user data to be used by other forms

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.

Last edited by keefa3011; April 17th, 2012 at 09:49 AM..