Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i write a program with c# and the Data Base for that is Access , now My Access' Data Base is on other computer with a lot of Data (i use Network with Server ) , i Create a textbox for search from Data Base like Search on 'Google.com';
there is a problem when i do Search for example i type "A" then my code go to database (Server) and find all record that have the character "A" next i type "B" now my text Box is "AB" then again my code go to Data Base and find all record that have the character "AB" next i type "c" go Database and Back .... Go and Back again and again , For each time a large number of records Searched it is Bad , now i want to solved this problem but i don't now How ?

see this image and my Code For Search
image : [^]

my Code :
C#
 private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Project par = new Project();
                DataTable tabl = par.SearchProjeh(txtFamilSearch.Text.Trim());
                if (tabl != null && tabl.Rows.Count > 0)
                {
                    dgvSearch.DataSource = tabl;
                    dgvSearch.Visible = true;
                    
                    
                }
                else
                {
                    dgvSearch.DataSource = null; dgvSearch.Visible = false;
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }
*****************************************

 public DataTable SearchProjeh(string Famil)
        {
            DataTable table = new DataTable();
            OleDbCommand command = new OleDbCommand();
            try
            {
                command.Connection = Connect;
                command.CommandText = "SELECT tblProjeh.CodeProj, tblProjeh.fldFamil, tblProjeh.fldName FROM tblProjeh WHERE  fldFamil like '%" + Famil + "%';";
                command.CommandType = CommandType.Text;
                if (command.Connection.State != ConnectionState.Open) command.Connection.Open();
                OleDbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                table.Load(reader);
            }
            catch (Exception)
            {

                throw;
            }
            return table;
        }
Posted

1 solution

Instead of using the TextChanged event try using the textBox1_Validating event. It will fire when the user has finished typing and tries to move away from the text box. That way you will only be searching the database once for the whole string entered

i.e. In the IDE Properties window for the textbox click on the "Lightening bolt" icon to get a list of possible events for the textbox.

Scroll down the list until you find Validating and double-click it.

Move the code currently in your textBox1_TextChanged event into the new event textBox1_Validating event

You should end up with something like this
private void textBox1_Validating(object sender, CancelEventArgs e)
{
    Project par = new Project();
    DataTable tabl = par.SearchProjeh(txtFamilSearch.Text.Trim());
    if (tabl != null && tabl.Rows.Count > 0)
    {
        dgvSearch.DataSource = tabl;
        dgvSearch.Visible = true;


    }
    else
    {
        dgvSearch.DataSource = null; dgvSearch.Visible = false;
    }
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}
NOTE I have removed the Try-catch block ... if you are not going to do something with an exception then don't catch it... it's very easy to "swallow" exceptions or lose valuable information about them.
 
Share this answer
 
v2
Comments
NorouziFar 27-Jan-15 3:50am    
Can you explain more?

Or Give Me a Example with c# and Access
CHill60 27-Jan-15 4:06am    
I've updated my solution

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