title: C# loading the main form after successful login
body: <p>I have two forms namely Login and Main Form. I want that on successful log in, the log in page should close and the main form should load. But the problem that I was facing was whenever I enter invalid username and password, the main form still shows up.</p>
  
  <p>I am accepting login message(Login Successfull Or Failed) as a string from SQL Server 2008 database </p>
  
  <p>I tried using the following codes </p>
  
  <ol>
  <li><p><strong>Code:</strong></p>
  
  <pre><code>public Form1()
  {
       FrmLogin frm = new FrmLogin();
       frm.ShowDialog();
       InitializeComponent();
  
  }
  </code></pre></li>
  <li><p><strong>Code:</strong></p>
  
  <pre><code>static void Main()
  {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      FrmLogin frm = new FrmLogin();
      if (frm.ShowDialog() == DialogResult.OK)
      {
          Application.Run(new Form1());
      }
      else
      {
          Application.Exit();
      }
  }    
  </code></pre></li>
  </ol>
  
  <p><strong>Login Form Button Code:</strong></p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
      OleDbConnection conn = new OleDbConnection("File Name=E:\\Vivek\\License Manager\\License Manager\\login.udl");
      try
      {
          conn.Open();
          OleDbCommand cmd = new OleDbCommand("checkuser",conn);
          cmd.CommandType = CommandType.StoredProcedure;
          OleDbParameter p1=new OleDbParameter("userid",username.Text);
          OleDbParameter p2 = new OleDbParameter("password",password.Text);
          cmd.Parameters.Add(p1);
          cmd.Parameters.Add(p2);
          var returnParameter = cmd.Parameters.Add("@ReturnVal",OleDbType.VarChar,50);
          returnParameter.Direction = ParameterDirection.ReturnValue;
          cmd.ExecuteNonQuery();
          // Retrieve the return value
          string result = returnParameter.Value.ToString();
          MessageBox.Show(result);
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }     
      conn.Close();
  }
  </code></pre>
  
  <p>Stored Procedure</p>
  
  <pre><code>ALTER proc [dbo].[usercheck]
                     (@userid varchar(20),
                       @password varchar(20))
  
                       as
                       begin
                       if exists(select userid,password from users where userid=@userid and password=@password)
                       return 0
                       else 
                       return 1
                       end
  </code></pre>
  
  <p>But unfortunately none of the above codes seem to work. I found the above code snippets from questions asked by others here but they are not working for me.</p>
  
  <p>Can anyone help me to rectify this?</p>
  
  <p>Thanks</p>