How to use textbox lost-focus event [duplicate]

こ雲淡風輕ζ 提交于 2019-12-24 15:26:37

问题


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.


回答1:


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!




回答2:


This might fix your issue:

<asp:TextBox ID="textBox1" runat="server" onblur="Your Function"></asp:TextBox>



来源:https://stackoverflow.com/questions/20187737/how-to-use-textbox-lost-focus-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!