4
Answers

Stuck in Loop for timer, will not exit method.

Marcus Dutton

Marcus Dutton

8y
401
1
So I'm working on a login form, this is opened from another form to un-hide certain objects.
 
on my login form I have a button for the Login statement along with a timer for a status bar message.
 
When I run my debug and start walking through this.  my app keeps looping at the timer for some reason.
 
I think I have found my issue but really don't know how to correct it.  It seems that if a false on the username and password combination are issued.  It's keeping the data from the previous select statement, even after I close the connection.  It is not going and grabbing the new values in the text boxes.
 
See Code Below
 
 
 private void button1_Click(object sender, EventArgs e)
{
Login userlogin = new Login();
userlogin.Username = tbxUserName.Text;
userlogin.Password = tbxPassword.Text;
bool result = dbconnect.ck_login(userlogin);
if (result == true)
{
Answer a = new Answer();
a.AnsYes = true;
this.Close();
}
else
{
tbxPassword.Clear();
tbxUserName.Clear();
tbxUserName.Focus();
Timer timer1 = new Timer();
timer1.Interval = 5000;
tsLoginLabel.Text = ("Login Failed! Please try again");
timer1.Start();
timer1.Tick += new EventHandler(timer1_Tick);
return;
}

}
private void timer1_Tick(object sender, EventArgs e)
{
tsLoginLabel.Text = ("Ready");
timer1.Stop();
return;
}
}
 
I have the following Classes:  MyConnection, Login, Answer
See Code Below
public bool ck_login(Login ckLogin)
{
bool result = false;
try
{
OleDbDataAdapter dbadapter = new OleDbDataAdapter();
DataTable logdt = new DataTable();
dbconnect.Open();
dbcommand.CommandText = "SELECT * FROM tblUsers WHERE uName=@userName and uPassword=@userPassword";
dbcommand.Parameters.AddWithValue("@userName", ckLogin.Username);
dbcommand.Parameters.AddWithValue("@userPassword", ckLogin.Password);
dbcommand.CommandType = System.Data.CommandType.Text;
dbadapter.SelectCommand = dbcommand;
dbadapter.Fill(logdt);
if (logdt.Rows.Count <= 0)
{
result = false;
logdt.Clear();
}
else
{
result = true;
logdt.Clear();
logdt.Dispose();
dbconnect.Close();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
dbconnect.Close();
}
return result;


 class Login
{
string username;
public string Username
{
get { return username; }
set { username = value; }
}
string password;
public string Password
{
get { return password; }
set { password = value; }
}
}



class Answer
{
bool ansYes;
public bool AnsYes
{
get { return ansYes; }
set { ansYes = value; }
}
}
}




Answers (4)