• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Log In Form

hi there , in my App i made a Form that u log into the App whit .
the code is :

SqlCommand cmd1 = new SqlCommand();
                  cmd1.Connection=sqlConnection1;
                  SqlParameter parm = new SqlParameter("@parm",SqlDbType.VarChar,50,"Pass");
                  parm.Direction=ParameterDirection.Output;
                  cmd1.Parameters.Add(parm);
                  cmd1.CommandText = "select @parm = Pass from Bakara";
                  sqlConnection1.Open();
                  cmd1.ExecuteNonQuery();
                  sqlConnection1.Close();
                  string Password = parm.Value.ToString();
                              
                  
                  if(Password == this.editBox1.Text)
                  {
                        this.Close();
                        
                  }
                  else
                  {
                        i++;
                        MessageBox.Show("The Pass Is False try Agian");
                  }
                  if (i==3)
                  {
                        MessageBox.Show("Incoorect 3 time Piss Off");
                        Environment.Exit(1);
                  }
                  
                         
            }

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 ?
0
Tech_Men
Asked:
Tech_Men
4 Solutions
 
dkloeckCommented:
Use the event the app throws when it closes and put the text you need to close the connection on it
0
 
YurichCommented:
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,
yurich
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
            {
                f1.Dispose();
                Environment.Exit(1);                  
            }

            f1.Dispose();
            // 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;
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }

    }
0
 
vinhnlCommented:
Here is my solution:

[STAThread]
 static void Main(string[] args)
 {
               LoginForm login = new LoginForm();
               Application.Run(login);
               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,
VINHNL
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now