如何使用文本框失去焦点事件 [英] How to use textbox lost-focus event

查看:730
本文介绍了如何使用文本框失去焦点事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

这篇关于如何使用文本框失去焦点事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆