Hamed Parvini
Hamed Parvini

Reputation: 21

Event isn't triggered

I have created an event called textBox1_Leave; But when I run my program, and I move the focus from txtBox1 the event isn't triggered.

I would like this event to be triggered, so I can check if the Name value that the user enters in txtBox1 exists in my database. If it does, I want to enable button1 by setting button1.Enable = true and otherwise to false.

My C# Code:

private void textBox1_Leave(object sender, EventArgs e)
{
    OleDbConnection A=new OleDbConnection();
    A.ConnectionString=Program.DBPATH;
    A.Open();

    OleDbCommand BB=new OleDbCommand();
    BB.Connection=A;
    BB.CommandText="SELECT username FROM Users WHERE (username = '" + textBox1.Text + "')";
    OleDbDataReader CC = BB.ExecuteReader();

    if (CC.Read())
    {
        button1.Enabled=true;
    }
    else
    {
        button1.Enabled=false;
    }
}

Upvotes: 0

Views: 648

Answers (2)

Pushpendra
Pushpendra

Reputation: 538

first check the event is wired up in constructor of the page

textBox1.Leave += textBox1_Leave;


and then

1) Debug the program and check whether break point is hitting.

Upvotes: 2

Pushpendra
Pushpendra

Reputation: 810

I believe you are developing windows application. I would suggest you to go through following steps:

1) Debug the program and check whether break point is hitting.

2) On your form you should have some control other than the textbox , then only the Leave event will fire. If you are having only textbox on the form the Leave event of textbox would not fire.

3) use try catch block else your application will crash if there is runtime error.

Upvotes: 0

Related Questions