We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Log In Form

Tech_Men asked
Medium Priority
Last Modified: 2010-04-16
hi there , in my App i made a Form that u log into the App whit .
the code is :

SqlCommand cmd1 = new SqlCommand();
                  SqlParameter parm = new SqlParameter("@parm",SqlDbType.VarChar,50,"Pass");
                  cmd1.CommandText = "select @parm = Pass from Bakara";
                  string Password = parm.Value.ToString();
                  if(Password == this.editBox1.Text)
                        MessageBox.Show("The Pass Is False try Agian");
                  if (i==3)
                        MessageBox.Show("Incoorect 3 time Piss Off");

now if the user close the form the app is still working whitout the login was verified to be true .
what can i do to overpower this problem ?
Watch Question

Use the event the app throws when it closes and put the text you need to close the connection on it

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
there're really several approaches, some of them:

your login form could be the application main form (my preference) - in other words, that's the one with the Main() function in it, in which case if you close this form, the whole application is closed.

another way is you can just add OnClosing event handler for your login form and from there you can call some public function from your current main form which would call this.Close(); of your main form - it will close your main application

if you WANT your user be able to see your application without logging but not be able to do anything, you can set a parameter parameter of your main application from your login form to true or false depending on the login situation and check for this parameter in your application form (in the form load method), then you can diable controls for example and show big message across your form - log in first please.

good luck,
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Set the DialogResult property of the login form before you exit.  Then you can check this back in the calling form to test whether or not login was successful and exit the application if appropriate.

(When the close button is clicked on a modal form the DialogResult is set to Cancel for you)

Simplified example:

            Form1 f1 = new Form1();
            if (f1.ShowDialog() != DialogResult.OK)

            // continue with your app...

    public partial class Form1 : Form

        private void button1_Click_1(object sender, EventArgs e)
            // set the DialogResult based on the results
            if (true)
                this.DialogResult = DialogResult.OK;
                this.DialogResult = DialogResult.Cancel;

Here is my solution:

 static void Main(string[] args)
               LoginForm login = new LoginForm();
               if( login.IsOK == true )
                    Application.Run(new MainForm());

IsOK is a property of LoginForm (or public variable). You must set login.IsOK == true if user and password are correct

Good luck,
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.