Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Progressive search using ComboBox
 

Progressive search using ComboBox

Hi all,

i am working on a project whereby i am required to insert, update and delete of customer details in a database. I want to make use of a ComboBox to do a progressive search on the handphone numbers inside the database whereby if the handphone number exists inside the database, it will display the customer details in the textboxes i have created. Handphone number is a column in my database table. This is some of the codes i have done :

Any help is greatly appreciated.
Thanks,
Charles

Imports System.Data
Imports System.Data.SqlClient
Imports System.Threading
Imports System.Text.RegularExpressions
Imports System.Text

Public Class Form1

    Dim da As SqlDataAdapter
    Dim conn As SqlConnection
    'Dim conn1 As SqlConnection
    Dim ds As DataSet
    Dim sql As String
    Dim MaxRows As Integer


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim myCommand As New SqlCommand(sql, conn)

        Dim connectionString As String = "Data Source=itcu-23\sqlexpress;Initial Catalog=compare;" & "Integrated Security=SSPI;"
        conn = New SqlConnection(connectionString)

        sql = "SELECT * FROM  CustDetail"


        da = New SqlDataAdapter(sql, conn)
        ds = New DataSet()
        Dim commandBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)

        da.FillSchema(ds, SchemaType.Mapped)
        da.Fill(ds, "CustDetail")


        MaxRows = ds.Tables("CustDetail").Rows.Count
        MaxRows = MaxRows - 1


       'Dim Number As New List(Of String)
        Dim Words() As String = New String() _
      { ds.Tables("CustDetail").Columns(3)}
        Number.AddRange(Words)
        Number.Sort()
        ComboBox1.Items.AddRange(Number.ToArray)
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
        ComboBox1.SelectedIndex = 0

    End Sub
End Class



Charles Mohd  Tuesday, August 18, 2009 6:35 AM
I assume since you posted the code it is somehow not working for you? Are you getting an error? Or just not getting expected results? Can you provide more detail in the problem you are having?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
  • Unmarked As Answer byCharles Mohd Wednesday, August 19, 2009 1:23 AM
  • Marked As Answer byCharles Mohd Wednesday, August 19, 2009 1:23 AM
  •  
DeborahK  Tuesday, August 18, 2009 5:21 PM
Hi Deborahk,

Yes, the codes are not working because i don't know how to input the values of the ComboBox as a specific column of my database table.
Charles Mohd  Wednesday, August 19, 2009 1:24 AM

Hi Charles,

You said: I don't know how to input the values of the ComboBox as a specific column of my database table. Do you mean the value and the text of the ComboBox should be mapped to different columns of the DataTable? If so, You can bind the DataTable to the ComboBox and set the DsiplayMember and ValueMember properties of the ComboBox to different column names. This is a code snippet:
ComboBox1.DataSource = ds.Tables("CustDetail")

ComboBox1.DisplayMember = "displayColumnName"

ComboBox1.ValueMember = "valueColumnName"

You can get more information about ComboBox binding from:
http://msdn.microsoft.com/en-us/library/w67sdsex.aspx.

Let me know if this helps.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, August 20, 2009 5:21 AM
Hi again Aland,

The codes are working but may i enquire you on how to do a progressive search using a textbox whereby it searches for only a column of a table.
P.S. Something similar to google.com progressive search but it only but it only searches a column of a table.

Thanks a million,
Charles
Charles Mohd  Thursday, August 20, 2009 8:00 AM

Hi Charles,

Based on my understanding, this is what you want: when we type some text in a TextBox, we would use the typed text to search in a column of the table to see if the column has a same value. Am I right?

Regards,

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, August 20, 2009 8:07 AM
i think it is, its like google.com search whereby when i type for e.g. "i" den it will show alot of searches available like "iran", "iras","imeem","ipod" etc.. but the available values for the search, is a specific column in my table.
Charles Mohd  Thursday, August 20, 2009 8:25 AM

Hi Charles,

We need to enable the auto complete mode to get a same effect. This is the code snippet:

//Set the auto complete mode to enable auto complete.
this.textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
//The data source is filled programmatically.
this.textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
//Create the custom source.
this.textBox1.AutoCompleteCustomSource = new AutoCompleteStringCollection();
//Get the DataTable.
DataTable dt = GetData();
//The column we want to help the input.
const int suggestColumnIndex = 0;
//Traverse the rows and and the suggest values.
foreach (DataRow row in dt.Rows)
{
    this.textBox1.AutoCompleteCustomSource.Add(row[suggestColumnIndex].ToString());
}



Regards,

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, August 20, 2009 9:03 AM
Hi Aland,

i am sorry but i am using vb.net as my programming language, i have edited ur codes to vb.net but its not working though without errors.

Here is the codes:

        '//Set the auto complete mode to enable auto complete.
        Me.TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
        '//The data source is filled programmatically.
        Me.TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
        '//Create the custom source.
        Me.TextBox1.AutoCompleteCustomSource = New AutoCompleteStringCollection()

        MaxRows = ds.Tables("CustDetail").Rows.Count
        MaxRows = MaxRows - 1
        Dim numbers() As Integer
        Dim EndAt As Integer
        inc = 0
        MsgBox(MaxRows)
        EndAt = MaxRows
        ReDim numbers(EndAt)

        For i = inc To EndAt

            Me.TextBox1.AutoCompleteCustomSource.Add(ds.Tables("CustDetail").Columns(3).ToString())


        Next i

Please take a look and tell me what is wrong, thanks.
Charles Mohd  Thursday, August 20, 2009 9:28 AM
Hi Charles,

Could you please tell me what errors you have met? This is the converted VB code:
'Set the auto complete mode to enable auto complete. 
Me.textBox1.AutoCompleteMode = AutoCompleteMode.Suggest 
'The data source is filled programmatically. 
Me.textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource 
'Create the custom source. 
Me.textBox1.AutoCompleteCustomSource = New AutoCompleteStringCollection() 
'Get the DataTable. 
Dim dt As DataTable = GetData() 
'The column we want to help the input. 
Const suggestColumnIndex As Integer = 0 
'Traverse the rows and and the suggest values. 
For Each row As DataRow In dt.Rows 
    Me.textBox1.AutoCompleteCustomSource.Add(row(suggestColumnIndex).ToString()) 
Next 



Regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, August 20, 2009 9:31 AM
Hi Aland,

may i enquire you on how to setan existingdatabase tableinto the datatable?
Charles Mohd  Thursday, August 20, 2009 4:55 PM
Hi Charles,

Do you mean ds.Tables("CustDetail") ?

Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 21, 2009 1:56 AM
Hi Aland,

yes, thanks, but is this the only type of progressive search? Because its not functioning like how google search does.

Charles
Charles Mohd  Friday, August 21, 2009 7:27 AM

Hi Charles,

You said: Because it’s not functioning like how google search does.

Could you please tell me which function is not supported?

Regards,

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 21, 2009 7:36 AM
i mean like when i type a "+" for e.g. it will search for all values with a "+" and as u type more stuffs into the textbox, it narrows down the search. I am sorry but theres another problem, when i delete a row in the database, the deleted value still appears in the progressive search.

Charles
Charles Mohd  Friday, August 21, 2009 8:00 AM
Hi Charles,

Have you tested my code snippet posted 23 hours 53 minutes ago. The code can ahieve the first goal.
When you delete a row from database, you need to remove the related item in the AutoCompleteCustomSource of the TextBox.

Regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 21, 2009 9:29 AM
Yes i have tested it and its working, can u teach me how to remove the related item in the AutoCompleteCustomSource of the TextBox?

Thanks alot,
Charles
Charles Mohd  Friday, August 21, 2009 7:43 PM

Hi Charles,

Sorry for the late reply. You can call the Remove method of the AutoCompleteCustomSource to remove a item. You can also call the Clear method to empty the source and add items from the database again.

Regards,

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 24, 2009 2:15 AM
Sorry Aland i am not sure of how to do it but i have attempted the clear method and add items from the database way but its not working, i posted some of the codes, could you kindly help me check what is missing? Thanks a million.

Charles


                    Dim nameCommand As SqlCommand = conn.CreateCommand()
                    nameCommand.CommandText = _
                   "DELETE FROM CustDetail WHERE RmuNum = '" & txtSearch.Text & "'"

                    nameCommand.ExecuteNonQuery()

                    da.Update(ds, "CustDetail")

                    Me.txtSearch.AutoCompleteCustomSource.Clear()

                    Me.txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest
                    'The data source is filled programmatically. 
                    Me.txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource
                    'Create the custom source. 
                    Me.txtSearch.AutoCompleteCustomSource = New AutoCompleteStringCollection()
                    'Get the DataTable. 
                    dt = ds.Tables("CustDetail")
                    'The column we want to help the input. 
                    Const suggestColumnIndex As Integer = 3
                    'Traverse the rows and and the suggest values. 
                    For Each row As DataRow In dt.Rows
                        Me.txtSearch.AutoCompleteCustomSource.Add(row(suggestColumnIndex).ToString())
                    Next
Charles Mohd  Monday, August 24, 2009 4:30 AM
Hi Charles,

You need to fetch the data again when you update the database. The code
da.Update(ds, "CustDetail")
need to be changed to call the Fill method of da to retrieve the data again.

Regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 24, 2009 6:21 AM
i tried the fill method but the same results occur.
Charles Mohd  Monday, August 24, 2009 8:23 AM

You can use google to search for other answers

Custom Search

More Threads

• DivideByZeroException
• How do I copy only filtered data (rows) from DataView to DataTable and DataSet?
• Error adding a data source - Connecting to a UDB DB2 database on an iSeries Computer i810 V5R4
• Combo box data bind
• Master/detail drag and drop problem.
• how to show nothing when a 0 is in a binded textbox
• Master/Detail with multiple textboxes to a datagridview
• Dataset being modified??
• DataMember Property 'FK_ChildTable_ParentTable' cannot be found on the DataSource
• TabControl - backColor