Search code examples
c#asp.nettextboxlost-focus

How to use textbox lost-focus event


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.


Solution

  • 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!