How to use textbox lost-focus event

13,553

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

Solution 2

This might fix your issue:

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

Share:
13,553
VIPUL PARMAR
Author by

VIPUL PARMAR

ASP.Net Developer

Updated on June 04, 2022

Comments

  • VIPUL PARMAR
    VIPUL PARMAR almost 2 years

    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 PARMAR
    VIPUL PARMAR over 10 years
    Thanks RaZor in page load event what to write?
  • ValarmorghulisHQ
    ValarmorghulisHQ over 10 years
    I'm not really an ASP.NET developer. Give me some time to look it up ;)
  • ValarmorghulisHQ
    ValarmorghulisHQ over 10 years
    Just found a declaration for the event, try this: <asp:TextBox ID="textbox1" Runat="server" Text="" OnLeave="textBox1_OnLeave" /> protected void textBox1_OnLeave(object sender, EventArgs e) { //put the code that I posted earlier here! }
  • VIPUL PARMAR
    VIPUL PARMAR over 10 years
    Thanks RaZor this is very helpful to me
  • VIPUL PARMAR
    VIPUL PARMAR over 10 years
    in aspx page onLeave event is not whown
  • VIPUL PARMAR
    VIPUL PARMAR over 10 years
    in this, i think function should in js
  • ValarmorghulisHQ
    ValarmorghulisHQ over 10 years
    Yes, Here is the link where I found the answer for your question: codeproject.com/Questions/334142/…