473,378 Members | 1,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

vb.net -- Enabling "Search" within a textbox

angelabi264
I am workin on a project of cd library...if we hav to search for a movie...whn we type the name of movie in a textbox...all (possibilities) movies havin tat name shud appear...
for eg : in database thr are movies such as
Hum Dil de chuke sanam
Dil tho pagal hai
Dilagi
Dil hai tumhara

now if i type in textbox,the word "DIL"...all the 4 movie names shud appear within the dropdown of the texbox...

if i add a word such as "HAI" in textbox...(ie"DIL HAI")...movie names containing those 2 words shud b displayed within dropdown of textbox...(in this case Dil tho pagal hai & Dil hai tumhara)

Can somebody plzzz help me out in writtin a code for this in vb.net....

[ i use visual studio 2005 and sql server 2005]
Feb 16 '09 #1
6 7366
OuTCasT
374 256MB
Expand|Select|Wrap|Line Numbers
  1. select movie from movies where movie = '" & txtMovies.text & "' LIKE '%%'
Feb 17 '09 #2
Aads
48
Hi there,

(1) Please run the below script in your database which creates a table & inserts the movie names

Expand|Select|Wrap|Line Numbers
  1. Create Table tblMovies
  2. (
  3. idMovie INT Identity Primary Key,
  4. movieName Varchar(100)
  5. )
  6.  
  7. Insert Into tblMovies Values ('Hum Dil de chuke sanam')
  8. Insert Into tblMovies Values ('Dil tho pagal hai')
  9. Insert Into tblMovies Values ('Dilagi')
  10. Insert Into tblMovies Values ('Dil hai tumhara')
  11.  
(2) Now paste the below event handler in your project renaming to the textbox which you have specified

Expand|Select|Wrap|Line Numbers
  1.   Private Sub txtMovieName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMovieName.TextChanged
  2.         Dim DtMovies As DataTable
  3.         Dim Query As String = "SELECT idMovie, movieName From tblMovies "
  4.         Try
  5.             If (txtMovieName.Text.Trim.Length = 0) Then
  6.                 cmbMovies.DataSource = Nothing
  7.                 Return
  8.             End If
  9.             cmbMovies.DataSource = Nothing
  10.  
  11.             For len As Int32 = 0 To txtMovieName.Text.Split(Space(1)).Length
  12.                 If (len = 0) Then
  13.                     Query &= "Where movieName Like '%" & txtMovieName.Text.Split(Space(1))(len) & "%'"
  14.                 Else
  15.                     If (len = txtMovieName.Text.Split(Space(1)).Length) Then
  16.                         Exit For
  17.                     End If
  18.                     Query &= vbCrLf & " AND " & vbCrLf & "movieName Like '%" & txtMovieName.Text.Split(Space(1))(len) & "%'"
  19.                 End If
  20.             Next
  21.  
  22.  
  23.             Dim Conn As SqlConnection = New SqlConnection(CONN_STRING)
  24.             Conn.Open()
  25.             Dim sqlCmd As SqlCommand = New SqlCommand
  26.             With sqlCmd
  27.                 .Connection = Conn
  28.                 .CommandText = Query
  29.                 .CommandType = CommandType.Text
  30.             End With
  31.             Dim sqlDa As SqlDataAdapter = New SqlDataAdapter(sqlCmd)
  32.             DtMovies = New DataTable
  33.             sqlDa.Fill(DtMovies)
  34.             cmbMovies.DataSource = Nothing
  35.             If (DtMovies IsNot Nothing) Then
  36.                 With cmbMovies
  37.                     .ValueMember = "idMovie"
  38.                     .DisplayMember = "movieName"
  39.                     .DataSource = DtMovies
  40.                 End With
  41.             End If
  42.  
  43.         Catch ex As Exception
  44.             MessageBox.Show(ex.Message)
  45.         End Try
  46.     End Sub
  47.  
(3) Finally run your project & you are all done!

Regards,
Aads
Feb 22 '09 #3
Tyecom
5
Hi Aads,

I'm trying to do the same thing and I'm trying to use the code you provided. However, I'm getting errors messages. The error I'm getting is "MovieSample not declared" and "Conn_String not declared". Here is my modified code:

Imports System
Imports System.Configuration
Imports System.Data.SqlClient

Public Class Form1

Private Sub txtMovieName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMovieName.TextChanged

Dim DtMovies As DataTable
Dim Query As String = "SELECT idMovie, movieName From tblMovies "

Try
If (txtMovieName.Text.Trim.Length = 0) Then
MovieSample.DataSource = Nothing
Return
End If
MovieSample.DataSource = Nothing

For len As Int32 = 0 To txtMovieName.Text.Split(Space(1)).Length
If (len = 0) Then
Query &= "Where movieName Like '%" & txtMovieName.Text.Split(Space(1))(len) & "%'"
Else
If (len = txtMovieName.Text.Split(Space(1)).Length) Then
Exit For
End If
Query &= vbCrLf & " AND " & vbCrLf & "movieName Like '%" & txtMovieName.Text.Split(Space(1))(len) & "%'"
End If
Next

Dim Conn As SqlConnection = New SqlConnection(CONN_STRING)
Conn.Open()
Dim sqlCmd As SqlCommand = New SqlCommand
With sqlCmd
.Connection = Conn
.CommandText = Query
.CommandType = CommandType.Text
End With
Dim sqlDa As SqlDataAdapter = New SqlDataAdapter(sqlCmd)
DtMovies = New DataTable
sqlDa.Fill(DtMovies)
MovieSample.DataSource = Nothing
If (DtMovies IsNot Nothing) Then
With MovieSample
.ValueMember = "idMovie"
.DisplayMember = "movieName"
.DataSource = DtMovies
End With
End If

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Feb 24 '09 #4
Aads
48
Hi there,
  • MovieSample is the name of the combobox where it displays all the matched movie names and
  • CONN_STRING is the connection string to connect to the database - you need to declare this variable at the top like this
Expand|Select|Wrap|Line Numbers
  1. Private Const CONN_STRING As String = "Server = localhost;Database = someDbName; Integrated Security = SSPI"
  2.  
Feb 25 '09 #5
Tyecom
5
Hi Aads,

Thank you for your assistance. I'm trying to do something very similiar to what you described above. I also want to be able to search a combobox, but using the combobox "only". For instance, I want to type in a combobox titles of movies stored in my database. As I type, the movies will appear in automatically in the dropdown menu in alphabetical order. The filter in the above code does not filter the way I would like it to. Example, any title with the letter "T" will appear when you type a "T". Even if the "T" is the last letter of the title. I would like the title to appear in sequence. If I type a "T" all titles that begins with "T" will appear. If I type a "TH" all titles what begins with "TH" will appear and so on.

I tried to modified this code to reflect these changes, but have so far failed to make it work. Any assistance would be greatly appreciated.
Feb 25 '09 #6
Aads
48
Hi there,
  • First of all add a new form to your project.
  • Drag one Combobox from the toolbox & add it to you form; rename the combobox to cmbMovies.
  • Paste the below code in your form.
  • You will have to change the connection string to point to your database.

Expand|Select|Wrap|Line Numbers
  1.     Private mResetOnClear As Boolean = False
  2.     Public Property ResetOnClear() As Boolean
  3.         Get
  4.             Return mResetOnClear
  5.         End Get
  6.         Set(ByVal Value As Boolean)
  7.             mResetOnClear = Value
  8.         End Set
  9.     End Property
  10.  
  11.     Private Sub cmbMovies_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmbMovies.KeyPress
  12.         Try
  13.             Dim intIndex As Integer
  14.             Dim strEntry As String
  15.  
  16.             If Char.IsControl(e.KeyChar) Then
  17.                 If cmbMovies.SelectionStart <= 1 Then
  18.                     If mResetOnClear Then
  19.                         cmbMovies.SelectedIndex = 0
  20.                         cmbMovies.SelectAll()
  21.                     Else
  22.                         cmbMovies.Text = String.Empty
  23.                         cmbMovies.SelectedIndex = -1
  24.                     End If
  25.                     e.Handled = True
  26.                     Exit Sub
  27.                 End If
  28.                 If cmbMovies.SelectionLength = 0 Then
  29.                     strEntry = cmbMovies.Text.Substring(0, cmbMovies.Text.Length - 1)
  30.                 Else
  31.                     strEntry = cmbMovies.Text.Substring(0, cmbMovies.SelectionStart - 1)
  32.                 End If
  33.                 fetchData(strEntry)
  34.             ElseIf (Not Char.IsLetterOrDigit(e.KeyChar)) And (Not Char.IsWhiteSpace(e.KeyChar)) Then  '< 32 Or KeyAscii > 127 Then
  35.                 Exit Sub
  36.             Else
  37.                 If cmbMovies.SelectionLength = 0 Then
  38.                     strEntry = UCase(cmbMovies.Text & e.KeyChar)
  39.                 Else
  40.                     strEntry = cmbMovies.Text.Substring(0, cmbMovies.SelectionStart) & e.KeyChar
  41.                 End If
  42.                 fetchData(strEntry)
  43.             End If
  44.  
  45.             intIndex = cmbMovies.FindString(strEntry)
  46.  
  47.             If intIndex <> -1 Then
  48.                 cmbMovies.SelectedIndex = intIndex
  49.                 cmbMovies.SelectionStart = strEntry.Length
  50.                 cmbMovies.SelectionLength = cmbMovies.Text.Length - cmbMovies.SelectionStart
  51.             Else
  52.                 cmbMovies.Focus()
  53.                 cmbMovies.Text = strEntry
  54.             End If
  55.             e.Handled = True
  56.             Exit Sub
  57.         Catch ex As Exception
  58.             MessageBox.Show(ex.Message)
  59.         End Try
  60.     End Sub
  61.  
  62.     Private Sub fetchData(ByVal currentEntry As String)
  63.         Dim DtMovies As DataTable
  64.         Dim Query As String = "SELECT idMovie, movieName From tblMovies "
  65.         Try
  66.             cmbMovies.DataSource = Nothing
  67.  
  68.             Query &= "Where movieName Like '" & currentEntry & "%'"
  69.  
  70.             Dim Conn As SqlConnection = New SqlConnection("Server=in-bl-akash-lap\DSI;Database=MyDb;Integrated Security = SSPI")
  71.             Conn.Open()
  72.             Dim sqlCmd As SqlCommand = New SqlCommand
  73.             With sqlCmd
  74.                 .Connection = Conn
  75.                 .CommandText = Query
  76.                 .CommandType = CommandType.Text
  77.             End With
  78.             Dim sqlDa As SqlDataAdapter = New SqlDataAdapter(sqlCmd)
  79.             DtMovies = New DataTable
  80.             sqlDa.Fill(DtMovies)
  81.             cmbMovies.DataSource = Nothing
  82.             If (DtMovies IsNot Nothing) Then
  83.                 If (DtMovies.Rows.Count = 0) Then
  84.                     'MessageBox.Show("Could not find title '" & e)                  
  85.                     Me.Text = currentEntry
  86.                 End If
  87.  
  88.                 cmbMovies.ValueMember = "idMovie"
  89.                 cmbMovies.DisplayMember = "movieName"
  90.                 cmbMovies.DataSource = DtMovies
  91.  
  92.             End If
  93.  
  94.         Catch ex As Exception
  95.             MessageBox.Show(ex.Message)
  96.         End Try
  97.     End Sub
  98.  
  • Finally you can change the code as you like for any custom functionality required - this code "ONLY" demonstrates the required functionality.

Thanks,
Aads
Feb 26 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Troels F. Smit | last post by:
Where can I find information on implementing a search function on my site ? -- Mvh. Troels F. Smit
43
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
1
by: Leu | last post by:
I like to search in the forum but I can't find the (in FAQ described) "search" link: "... To access the search feature, click on the "search" link at the top of most pages. ..." Where is the...
0
by: Tallgeese | last post by:
We developed an in-process COM object for our own application. A toolbar button is used to activate the COM object. But when the COM object loaded, the HTML help hang when clicking the "List...
5
by: Agnes | last post by:
For my own practices. I like to put "Me". e.g IF Me.txtInvoice.textlength = 0 ....... etc Me.txt.....etc However, Is there any difference (without Me) ?? Thanks
0
NeoPa
by: NeoPa | last post by:
Intention : To prepare a WHERE clause for multiple field selection, but to ignore any fields where the selection criteria are not set. ONLY WORKS WITH TEXT FIELD SELECTIONS. Scenario : You have...
1
by: awigren | last post by:
In my database, I have a special form specifically for searching one field. I am using this database for a 3 step process where each of the steps are on the main menu and each button takes you to a...
1
by: Nour469 | last post by:
Hello, I have a problem when I run search, I have fields for: name, clientID and phoneNo. When a client calls I need to find them in the database, they might give me the name and it might not...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.