Be Sociable, Share!

Note : Here we will provide only search as you type if you want search as you type application which is already developed then you need to comment you will receive application in your mail.

  • There are two type of search 1st is search as type this means when you will write a keyword in your text box all data from the database is extracted and showed into the list view without pressing any button.
  • 2nd type of Simple Search from Database  you write a keyword and press search button if your keyword is found in database then it will display results in text boxes if you want to do this kind then visit Simple Search.

Search As You Type :

  • Below there’s a screen shot of search application here i am searching consumable and non-consumable items from my tables.
  • You can see i have write an alphabet “c” in text box for consumable items and in left listView it shows all data which contains alphabet “c” .
  • Now in the non-consumable item same alphabet “c” is written and in them same way it shows all data which contain alphabet “c” in right listView.
search as you type

Search As You Type

Code Explanation :

    •  Now here comes the code what you have to do is place the this code in the text change event. How to create text change event ?
    • First three line of code shows when you write any key word then if you clear an alphabet then it should clear the old search from the listbox and extract new search from database and place that result into the listbox.
    • After then you have to create query according to your specifications i have made query according to my specification if you find any error while making queries or any other problem just comment below to solve your problem.
    • And then SQL command , Connection and Reader.
    • Don’t forget to include this header file  using System.Data.SqlClient;
    • Now make a while loop and read until the data is found and place this data in a string type variable.
    • Now add this string to listbox.
    • Now close the connection.

 

if (textBox2.Text == “”)
listBox2.Items.Clear();
listBox2.Items.Clear();
string query = “SELECT ComponentName,Quantity FROM noncons where ComponentName like ‘%” + textBox2.Text + “%’”;

SqlConnection conn = new SqlConnection(connection);

SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader rdr;
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string temp = rdr["ComponentName"] + “\t” + rdr["Quantity"];
listBox2.Items.Add(temp);

}
conn.Close();

 

Be Sociable, Share!