How to repeated insert into history by textchanged event problem
Hi guys i have 3 textboxes 1,2,3
textbox1 respresent mobileNo
textbox2 represent email
textbox3 represent roomno
I need to update any textboxes from three based on textchanged event
Suppose i need update room no in textbox3 changed event
room no is 22 i need to changed to 3333 then update in history by insert into statment in textchanged event
it make insert 4 times
first time 3
second time 33
third time 333
four time 3333
how to prevent repeated insert into changes to history
public void UpdateMobileHistory(string ConnectionString,string SerialNo ,string EmployeeNo, string Mobile,string UserID,string DateEdit)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into dbo.HistoryEmployee values(@SerialNo,@EmployeeNo ,'Contact Data',@DateEdit,@UserID,@Mobile,null,null)";
cmd.Parameters.Add("@SerialNo", SqlDbType.NVarChar, 20);
cmd.Parameters.Add("@EmployeeNo", SqlDbType.NVarChar ,20);
cmd.Parameters.Add("@Mobile", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("@UserID", SqlDbType.NVarChar ,20);
cmd.Parameters.Add("@DateEdit", SqlDbType.NVarChar ,50);
cmd.Parameters["@SerialNo"].Value = SerialNo;
cmd.Parameters["@EmployeeNo"].Value = EmployeeNo;
cmd.Parameters["@Mobile"].Value = Mobile;
cmd.Parameters["@UserID"].Value = UserID;
cmd.Parameters["@DateEdit"].Value = DateEdit;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
-----------------
public string MaxHistoryEmployee(string ConnectionString)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select max(SerialNo)+1 from dbo.HistoryEmployee";
con.Open();
string commit = Convert.ToString(cmd.ExecuteScalar());
con.Close();
return commit;
}
-----------------
private void textBox1_TextChanged(object sender, EventArgs e)
{
FleetManagment.Fleet fleetContact1 = new FleetManagment.Fleet();
string ID = fleetContact1.MaxHistoryEmployee("Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4 + "");
fleetContact1.UpdateMobileHistory("Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4 + "",ID ,textBox4.Text, textBox1.Text,label6.Text,label10.Text);
}
this is all my code
Re: How to repeated insert into history by textchanged event problem
Again, please use code tags. Also, when someone takes the time to respond to your posts, please at least acknowledge that you've seen the reply.
Re: How to repeated insert into history by textchanged event problem
Don't do the insert from the text changed event. That event will fire with every change so you can get a lot more inserts than you want. You need to change the way you are thinking and trigger the insert another way.
Re: How to repeated insert into history by textchanged event problem
The TextChanged event is simple the wrong event to handle that problem.
I would use the keydown Event and checking for the 'Enter' key. When you then press the enter key in the end of entering Text to thr box, the whole text cousd be used to do the enty into your History
.Do you get the idea or do you need a code snipped?
Re: How to repeated insert into history by textchanged event problem
Or, in case the user is allowed, but not required, to press enter, you can additionally handle the LostFocus event.