p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# 2005 (http://p2p.wrox.com/forumdisplay.php?f=138)
-   -   Hidden form containing user data to be used by other forms (http://p2p.wrox.com/showthread.php?t=87288)

keefa3011 April 17th, 2012 08:36 AM

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.

keefa3011 April 27th, 2012 06:34 AM

In the end I wrote a new public class to take the values from the correct database table row and store them as public strings for use at later points in the program.

Just in case anyone else has a similar issue in the future :)


All times are GMT -4. The time now is 10:27 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.