Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have text box that have textbox control ontextchange, when i press single character it will insert into database, but i want to insert full word to be inserted, but not using any button, here is my code please provide solution for this


private void barcodetxt_TextChanged(object sender, EventArgs e)
{

OleDbConnection cnon = new OleDbConnection();
cnon.ConnectionString = @"Provider= Microsoft.ACE.OLEDB.12.0; Data Source=D:\WindowsFormsApplication1\barcode.accdb";
OleDbCommand command = new OleDbCommand();
string strqur = "select barcode from tblbarcode";

OleDbCommand cmd = new OleDbCommand(strqur,cnon);
OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);

string strcode;
string strcodetxt;

strcodetxt = barcodetxt.Text;
if(ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
strcode = Convert.ToString(ds.Tables[0].Rows[i]["barcode"].ToString());
if (strcodetxt == strcode)
{
MessageBox.Show("Name already exists..! please enter new name");
}
else
{
command.CommandText = "INSERT INTO tblbarcode (BARCODE) VALUES('" + barcodetxt.Text + "')";
}
}
}

cnon.Open();
command.Connection = cnon;
command.ExecuteNonQuery();
cnon.Close();
}
Posted

I think if the focus is out of textbox textchange event occurs whether it is a single letter,a word or a sentence its all the same.If we want to insert a word make validation like textbox text length should greater than some number you want as a condition.Hope it helps.
 
Share this answer
 
v2
You can write the insert code in the textbox leave event hope it helps you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900