Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am using C# 2008 with Access 2007. I am using MDI on MDI a added a menu now that Menu have a Login MenuItem which is enable and other menu items are disabled at form load. Now I am try to implement this if login is successful then other menu items will be enabled automatically. I tried lots of technique but I can't able to access Menu Items from Login Form. What I tried here is the code -
namespace EzBuddy
{
    public partial class UserLoginForm : Form
    {
        //public NewUser nuser1;
        private OleDbDataReader aReader;
        private OleDbCommand aCommand;
        private OleDbConnection aConnection;
        public int login_flag = 0;
        public UserLoginForm()
        {
            InitializeComponent();
        }
        public void menuEnable()
        {
           
        }
        public void login_code()
        {
            int x = LoginBox.TextLength;
            if (x < 5 || LoginBox.Text.Equals(""))
            {
                MessageBox.Show("ID is of minimum 5 digits as your Permanent Emplolee code or 9 digits as your Temporary Employee code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                LoginBox.Text = "";
                PasswordBox.Text = "";
                LoginBox.Focus();
            }
            try
            {
                database_Connection();
                aReader = aCommand.ExecuteReader();
               
                while (aReader.Read())
                {
                    if (aReader.GetString(0).Equals(LoginBox.Text) && aReader.GetString(1).Equals(PasswordBox.Text))
                    {
                         login_flag = 1;
                        break;
                    }
                    else
                    {
                        login_flag = 0;
                    }
                }
                if (login_flag == 1)
                {
                     
                    MDIParentForm.loginToolStripMenuItem1.Enabled = false;
                    MDIParentForm.newToolStripMenuItem3.Enabled = true;
                    MDIParentForm.editToolStripMenuItem3.Enabled = true;
                    MDIParentForm.deleteToolStripMenuItem3.Enabled = true;
                    MDIParentForm.
                    MDIParentForm.searchToolStripMenuItem1.Enabled = true;
                    MDIParentForm.reportsToolStripMenuItem1.Enabled = true;
                    MDIParentForm.utilitiesToolStripMenuItem.Enabled =true;
                    this.Dispose();
                }
                else
                {
                    LoginBox.Text = "";
                    PasswordBox.Text = "";
                    LoginBox.Focus();
                    loginMessage.Visible = true;
                    aReader.Close();
                    aConnection.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }
            finally
            {
                aReader.Close();
                aConnection.Close();
            }
        }
        private void database_Connection()
        {
            try
            {
                aConnection.Open();
                Console.WriteLine("This is the returned data from Login table");
            }
            catch (OleDbException e)
            {
                Console.WriteLine("Error: {0}", e.Errors[0].Message);
            }
        }
        private void UserLoginForm_Load(object sender, EventArgs e)
        {
            aConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\KVSoftware\\KVStudent\\KV1StudentInformation.mdb");
            aCommand = new OleDbCommand("select * from Login", aConnection);
        }
        private void LoginButton_Click(object sender, EventArgs e)
        {
            login_code();
        }
    }
}

Help me to solve this problem
Posted
Comments
Indivara 2-Feb-11 0:40am    
Reply to your comment -

That can be done in many ways
- use a property in the login form, access it from the main window after the form closes
- set the DialogResult property (inherited from System.Windows.Form) to a value indicating success or failure
- a callback (event) registered on the login form class by the main window

etc

1 solution

What exactly happens? The menu state is unchanged?

I suppose the login form is modal? (main window cannot be operated while the login form is displayed)

Try this for now:
  • Main window opens login form
  • Login form processes login and returns the result to the main window
  • Main window disables its menu based on the return



What you are doing is bad object oriented design anyway
 
Share this answer
 
Comments
RaviRanjanKr 2-Feb-11 0:03am    
From OP:-
I accept you thoughts but question is how I will return the result to Main MDI Window for enabling or disabeling the menus.
I don't want to enable menu before valid login
Thank you for consideration.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900