I have two forms: Login and Main form. First, 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.
This works, but I have to double-click btnLogin (the button in the login form) to close the login form and show the main form.
Here is my code.
Program.cs (login form)
namespace Login
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Login fLogin = new Login();
if (fLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new Main());
}
}
}
}
Login form
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)
{
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;
}
}
}
}
What do you think I'm doing it wrong? Thank....
source
share