0

i made program to populate employe name in checkedlist box. now i need a search box for searching items from the combo box.when i typing the matching items must list out in the checked list box.for that how i code in vb.net ?. in key press even driven section i give the code but its not working properly.Please provide sufficient information. my code is given below

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    Dim conobj As Class1
    conobj = New Class1
    conobj.connection()

    Dim str1 = "select distinct e.vc_empname xxxxxxx e.vc_empname like '" & Me.TextBox1.Text & "%' order by e.vc_empname"

    conobj.readdata1(str1)
    CheckedListBox1.DataSource = conobj.ds.Tables(0)
    CheckedListBox1.DisplayMember = "vc_empname"
    CheckedListBox1.ValueMember = "vc_empname"

End Sub
achu
  • 17
  • 2
  • 8

1 Answers1

0

Even if "but its not working properly" is very vague i'll give it a try. Use the Asclause to specify the alias. Also, use Where to filter records:

Dim str1 = "select distinct e.vc_empname As xxxxxxx e.vc_empname Where vc_empname  like '%" & Me.TextBox1.Text & "%' order by e.vc_empname"

Note that you should always use sql-parameters to prevent sql-injection and other issues. I also strongly discourage from global connection classes like your Class1 which also holds the connection. All the more if it is Shared/static. This can cause issues with connection-pooling which is enabled by default. It's also more difficult to use the Using-statement (which you should use) if you don't expose the connection and other ADO.NET objects. It's also difficult to pass the sql-parameters if the method readdata1 doesn't accepts them.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939