Search Textbox with dropdown in vb.net

you can create a drop-down textbox somthing like Auto suggest Google search. when will you type a letter to this textbox then it will show the words starting with letter in drop down.

Let’s suppose if you type ‘a’ into textbox then textbox will suggest all words starting with letter ‘a’ in drop down.

drop-down textbox

You can do this with the help of AutoCompleteMode property in Textbox control. This property automatically matches the input string. This property is very useful for frequently searching strings. If there is duplication occurs in source data then the AutoCompleteMode property omits the duplication and display only once. We need to use AutoCompleteMode and AutoCompleteSource property must be used together.

Example

In the following example textbox control named TextBox1 suggest us employee names from the database according to input string(like picture)

  1. Private Sub FillSearchResult()
  2.         Dim AutoComp As New AutoCompleteStringCollection()
  3.         Dim dsSerch As New DataSet
  4.  
  5.         Dim ConStr As String = "user id=sa;password=aeiouy;Initial Catalog=……."
  6.         Dim sqlCon As New SqlClient.SqlConnection(ConStr)
  7.         sqlCon.Open()
  8.         Dim Str As String = "select empname from tbl_Employee"
  9.         Dim SqlCom As New SqlClient.SqlCommand(Str, sqlCon)
  10.         Dim sqlAdap As New SqlClient.SqlDataAdapter(SqlCom)
  11.         sqlAdap.Fill(dsSerch)
  12.         For i As Integer = 0 To dsSerch.Tables(0).Rows.Count - 1
  13.             AutoComp.Add(dsSerch.Tables(0).Rows(i)(0).ToString())
  14.         Next
  15.  
  16.         TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
  17.         TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
  18.         TextBox1.AutoCompleteCustomSource = AutoComp
  19. End Sub

Published by

Hirendra Sisodiya

Programmer at PSPL. Can be found on Google Plus.

  • NOORULLA KHAN

    Hello! Hirendra I’m back with some problem, As of now you have helped me a lot, so this time I have came with up asp.net google like text box, I have tried and searched but didn’t get a right logic to implement some how I have tried with only asp.net page it works but when I’m trying to do with Asp.net master page there I have big problem,please Hirendra help me out and waiting for your reply.

    • http://www.authorcode.com/ Hirendra Sisodiya

      The above example is only for the Windows application. In asp.net you can use the autocomplete plugin using jQueryUI.

      try that let me know.

  • NOORULLA KHAN

    Well, Bro thank you so much for your quick response,And yeah I’m trying this logic using Javascript,

    Ok, let me explain my code.

    I have a two different master pages called, and now there are two content place holder in my page with Master page2. and now I have textbox namely “txtsearch”. and here goes my content placeholder1 code.

    $(document).ready(function () {

    SearchText();

    });

    function SearchText() {

    $(".autosuggest").autocomplete({

    source: function (request, response) {

    $.ajax({

    type: "POST",

    contentType: "application/json; charset=utf-8",

    url: "Inward entry.aspx/GetAutoCompleteData",

    data: "{'To':'" + document.getElementById('<txtSearch').value + "'}",

    dataType: "json",

    success: function (data) {

    response(data.d);

    },

    error: function (result) {

    alert("Error");

    }

    });

    }

    });

    }


    Inward entry.aspx.vb code:

    _

    Public Shared Function GetAutoCompleteData(ByVal username As String) As List(Of String)

    Dim result As New List(Of String)()

    Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= |DataDirectory|BioFP1.accdb")

    Using cmd As New OleDbCommand("select name1 from tblInward where name1 LIKE '%'+@SearchText+'%'", con)

    con.Open()

    cmd.Parameters.AddWithValue("@SearchText", username)

    Dim dr As OleDbDataReader = cmd.ExecuteReader()

    While dr.Read()

    result.Add(dr("name1").ToString())

    End While

    Return result

    End Using

    End Using

    End Function

    ………………………………………………………………………………………………………………
    And dunno how to properly set the CSS and Javascript in the content placeholder for the asp.net controls. And bro your help would be much more appreciated, Kindly help me with valuable and right answer, because I’m having a very little time to finish my project.

    • http://www.authorcode.com/ Hirendra Sisodiya

      take a just html input type=”text” element inplace of the asp.net textbox control.

      write the css and javascript in the contentplaceholder1 and html in the contentplaceholder2.

      • NOORULLA KHAN

        Hirendra, As you said I did that and yeah it works really fine only
        when, I take html input type=”text” but my page is in asp.net, and I’m
        adding some other data along with html textbox into database but when I
        declared html input type=”text”, since asp.net page is not recognizing
        the input type, well how ever I have to input type=”text”
        runat=”server”. Then again I’m getting the same problem. Bro waiting for
        your positive response.

  • NOORULLA KHAN

    Hirendra where is your foot bro, I need to take “Ashirwaad” man, You’re too,your are just awesome man good.Thank you very much It’s working, But could you please recommend me about how to add html controls in asp.net page and how to deal with business logic layer?

  • NOORULLA KHAN

    Bro I dunno my comments are posting into your forum or not, once again I’m trying because here I’ve internet fluctuation, so how to do with asp.net, because your suggestion was right only when we use html input type, but my page is in aspx. so help me out how to do that.