Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the problem in my code? c# winforms
    text
    copied!<p>I have two forms Login and Main form. Initially, the Login form will be displayed and when the user is authenticated, the Main form will be displayed and the Login form will be closed.</p> <p>It's kinda working, but I have to click the btnLogin (a Button in the Login form) twice to close the Login form and show the Main form.</p> <p>Here's my code. </p> <p><strong>Program.cs</strong> (Login form)</p> <pre><code>namespace Login { static class Program { /// &lt;summary&gt; /// The main entry point for the application. /// &lt;/summary&gt; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Login fLogin = new Login(); if (fLogin.ShowDialog() == DialogResult.OK) { Application.Run(new Main()); } } } } </code></pre> <p><strong>Login form</strong></p> <pre><code>namespace Login { public partial class Login : Form { public Login() { InitializeComponent(); } private void Login_Load(object sender, EventArgs e) { } private void btnLogin_Click(object sender, EventArgs e) { // initially btnLogin has a DialogResult property set to None Authenticate(); } private void Authenticate() { SqlCeConnection conn = new SqlCeConnection(@"Data source=d:/BIMS.sdf"); conn.Open(); SqlCeCommand cmd = new SqlCeCommand(Properties.Resources.CheckIfUserExists, conn); cmd.Parameters.Add("username", txtUsername.Text.Trim()); cmd.Parameters.Add("password", txtPassword.Text.Trim()); SqlCeDataReader dr = cmd.ExecuteReader(); bool hasRow = dr.Read(); if (hasRow) { btnLogin.DialogResult = DialogResult.OK; } } } } </code></pre> <p>Where do you think I'm doing it wrong? Thanks....</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload