Amrul Hakim

Amrul Hakim

  • NA
  • 19
  • 3.9k

How to Make Autocomplete from database sql?

May 18 2017 2:36 AM
This my code in class Fungsi
  1. public void autocompele1(TextBox autotext)  
  2.         {  
  3.             conn = Koneksi.Conn;  
  4.             conn.Open();  
  5.             cmd = new SqlCommand("SELECT Nama_Barang FROM Tb_Barang ORDER BY Nama_Barang", conn);  
  6.             try  
  7.             {  
  8.   
  9.                 Reader = cmd.ExecuteReader();  
  10.                 if(Reader.HasRows)  
  11.                 {  
  12.                     while (Reader.Read())  
  13.                     {  
  14.                         autotext.AutoCompleteCustomSource.Add(Reader["Nama_Barang"].ToString());  
  15.                     }  
  16.                 }  
  17.                 else  
  18.                 {  
  19.                     MessageBox.Show("Data Not Found");  
  20.                 }  
  21.             }  
  22.             catch(Exception ex)  
  23.             {  
  24.                 MessageBox.Show("erorr" + ex.Message);  
  25.             }  
  26.   
  27.             finally  
  28.             {  
  29.                 conn.Close();  
  30.                 Reader.Close();  
  31.             }  
  32.   
  33.         }  
 and i called in form like this 
 
  1. private void Form_Transaksi_Load(object sender, EventArgs e)  
  2.         {  
  3.             Fungsi auto = new Fungsi();  
  4.             auto.autocompele1(txt_NamaBarang);  
  5.         }  

Answers (4)