如何使用文本框失去焦点事件

问题描述:

我有一个文本框,我想从数据库中重复的记录检查数据,当我失去了从文本光标。

I have a textbox and I want to check the data from database for duplicate record when I lost the cursor from textbox.

所以,请帮助我如何解决这个问题。

So please help me how to solve this.

VIPUL,

我为您创建了下面的例子。这可以帮助你从与数据库中的数据文本框中检查数据。

I created the following example for you. This can help you to check the data from the textbox with the data in the Database.

private void textBox1_Leave(object sender, EventArgs e)
    {
        //Put the value to be checked with the Database in a Variable.
        var valueToCheck = textBox1.Text;

        //Create connection with the database.
        var sqlConn = new SqlConnection("Connection String to Database");

        //Create dataset instance to fill with the return results from the Database.
        var ds = new DataSet();
        //Create SqlCommand to be execute on the database.
        var cmd = new SqlCommand("SELECT * FROM TABLE WHERE 'field to be checked' = " + valueToCheck, sqlConn);
        //Create SqlDataAdapter
        var da = new SqlDataAdapter(cmd);
        ds.Clear();
        try
        {
            da.Fill(ds);
        }
        catch (Exception ex)
        {
        }


        foreach (DataRow row in ds.Tables[0].Rows)
        {
            //do you stuff here.
        }
    }

我希望这有助于!

I hope this helps!