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>
Comments
-
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 over 10 yearsThanks RaZor in page load event what to write?
-
ValarmorghulisHQ over 10 yearsI'm not really an ASP.NET developer. Give me some time to look it up ;)
-
ValarmorghulisHQ over 10 yearsJust 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 over 10 yearsThanks RaZor this is very helpful to me
-
VIPUL PARMAR over 10 yearsin aspx page onLeave event is not whown
-
VIPUL PARMAR over 10 yearsin this, i think function should in js
-
ValarmorghulisHQ over 10 yearsYes, Here is the link where I found the answer for your question: codeproject.com/Questions/334142/…