问题
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