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"D IL 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]
6 7388 - select movie from movies where movie = '" & txtMovies.text & "' LIKE '%%'
Hi there,
(1) Please run the below script in your database which creates a table & inserts the movie names -
Create Table tblMovies
-
(
-
idMovie INT Identity Primary Key,
-
movieName Varchar(100)
-
)
-
-
Insert Into tblMovies Values ('Hum Dil de chuke sanam')
-
Insert Into tblMovies Values ('Dil tho pagal hai')
-
Insert Into tblMovies Values ('Dilagi')
-
Insert Into tblMovies Values ('Dil hai tumhara')
-
(2) Now paste the below event handler in your project renaming to the textbox which you have specified -
Private Sub txtMovieName_TextChanged(ByVal sender As System.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
-
cmbMovies.DataSource = Nothing
-
Return
-
End If
-
cmbMovies.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)
-
cmbMovies.DataSource = Nothing
-
If (DtMovies IsNot Nothing) Then
-
With cmbMovies
-
.ValueMember = "idMovie"
-
.DisplayMember = "movieName"
-
.DataSource = DtMovies
-
End With
-
End If
-
-
Catch ex As Exception
-
MessageBox.Show(ex.Message)
-
End Try
-
End Sub
-
(3) Finally run your project & you are all done!
Regards,
Aads
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 "MovieSampl e not declared" and "Conn_Strin g not declared". Here is my modified code:
Imports System
Imports System.Configur ation
Imports System.Data.Sql Client
Public Class Form1
Private Sub txtMovieName_Te xtChanged(ByVal sender As Object, ByVal e As System.EventArg s) Handles txtMovieName.Te xtChanged
Dim DtMovies As DataTable
Dim Query As String = "SELECT idMovie, movieName From tblMovies "
Try
If (txtMovieName.T ext.Trim.Length = 0) Then
MovieSample.Dat aSource = Nothing
Return
End If
MovieSample.Dat aSource = Nothing
For len As Int32 = 0 To txtMovieName.Te xt.Split(Space( 1)).Length
If (len = 0) Then
Query &= "Where movieName Like '%" & txtMovieName.Te xt.Split(Space( 1))(len) & "%'"
Else
If (len = txtMovieName.Te xt.Split(Space( 1)).Length) Then
Exit For
End If
Query &= vbCrLf & " AND " & vbCrLf & "movieName Like '%" & txtMovieName.Te xt.Split(Space( 1))(len) & "%'"
End If
Next
Dim Conn As SqlConnection = New SqlConnection(C ONN_STRING)
Conn.Open()
Dim sqlCmd As SqlCommand = New SqlCommand
With sqlCmd
.Connection = Conn
.CommandText = Query
.CommandType = CommandType.Tex t
End With
Dim sqlDa As SqlDataAdapter = New SqlDataAdapter( sqlCmd)
DtMovies = New DataTable
sqlDa.Fill(DtMo vies)
MovieSample.Dat aSource = 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
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
-
Private Const CONN_STRING As String = "Server = localhost;Database = someDbName; Integrated Security = SSPI"
-
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.
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.
-
Private mResetOnClear As Boolean = False
-
Public Property ResetOnClear() As Boolean
-
Get
-
Return mResetOnClear
-
End Get
-
Set(ByVal Value As Boolean)
-
mResetOnClear = Value
-
End Set
-
End Property
-
-
Private Sub cmbMovies_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmbMovies.KeyPress
-
Try
-
Dim intIndex As Integer
-
Dim strEntry As String
-
-
If Char.IsControl(e.KeyChar) Then
-
If cmbMovies.SelectionStart <= 1 Then
-
If mResetOnClear Then
-
cmbMovies.SelectedIndex = 0
-
cmbMovies.SelectAll()
-
Else
-
cmbMovies.Text = String.Empty
-
cmbMovies.SelectedIndex = -1
-
End If
-
e.Handled = True
-
Exit Sub
-
End If
-
If cmbMovies.SelectionLength = 0 Then
-
strEntry = cmbMovies.Text.Substring(0, cmbMovies.Text.Length - 1)
-
Else
-
strEntry = cmbMovies.Text.Substring(0, cmbMovies.SelectionStart - 1)
-
End If
-
fetchData(strEntry)
-
ElseIf (Not Char.IsLetterOrDigit(e.KeyChar)) And (Not Char.IsWhiteSpace(e.KeyChar)) Then '< 32 Or KeyAscii > 127 Then
-
Exit Sub
-
Else
-
If cmbMovies.SelectionLength = 0 Then
-
strEntry = UCase(cmbMovies.Text & e.KeyChar)
-
Else
-
strEntry = cmbMovies.Text.Substring(0, cmbMovies.SelectionStart) & e.KeyChar
-
End If
-
fetchData(strEntry)
-
End If
-
-
intIndex = cmbMovies.FindString(strEntry)
-
-
If intIndex <> -1 Then
-
cmbMovies.SelectedIndex = intIndex
-
cmbMovies.SelectionStart = strEntry.Length
-
cmbMovies.SelectionLength = cmbMovies.Text.Length - cmbMovies.SelectionStart
-
Else
-
cmbMovies.Focus()
-
cmbMovies.Text = strEntry
-
End If
-
e.Handled = True
-
Exit Sub
-
Catch ex As Exception
-
MessageBox.Show(ex.Message)
-
End Try
-
End Sub
-
-
Private Sub fetchData(ByVal currentEntry As String)
-
Dim DtMovies As DataTable
-
Dim Query As String = "SELECT idMovie, movieName From tblMovies "
-
Try
-
cmbMovies.DataSource = Nothing
-
-
Query &= "Where movieName Like '" & currentEntry & "%'"
-
-
Dim Conn As SqlConnection = New SqlConnection("Server=in-bl-akash-lap\DSI;Database=MyDb;Integrated Security = SSPI")
-
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)
-
cmbMovies.DataSource = Nothing
-
If (DtMovies IsNot Nothing) Then
-
If (DtMovies.Rows.Count = 0) Then
-
'MessageBox.Show("Could not find title '" & e)
-
Me.Text = currentEntry
-
End If
-
-
cmbMovies.ValueMember = "idMovie"
-
cmbMovies.DisplayMember = "movieName"
-
cmbMovies.DataSource = DtMovies
-
-
End If
-
-
Catch ex As Exception
-
MessageBox.Show(ex.Message)
-
End Try
-
End Sub
-
- Finally you can change the code as you like for any custom functionality required - this code "ONLY" demonstrates the required functionality.
Thanks,
Aads
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Troels F. Smit |
last post by:
Where can I find information on implementing a search function on my
site ?
--
Mvh.
Troels F. Smit
|
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 relative NOT to the immediate script that is
including it, but is relative to the top-level calling script.
In practice, this means that you have to constantly worry and adjust
paths in includes, based on the startup scripts that call these...
|
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 "search" link?
Leu
--
|
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 Topics" in "Search" tab. The HTML
help work nornally without loading the COM.
Can anyone provide some starting points, so that I can look at the problem ?
Thanks in advacne.
|
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
| |
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 a table (tblMember) containing information for various people.
Table Name=tblMember
Field; Type; IndexInfo
MemberID; AutoNumber; PK
Surname; String
|
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 number of fields that eventually all add into the same record.
After the first step, you type in the primary field's number to retrieve the second set of fields to fill in.
Is there anyway to make a pop-up come up if the # that they typed in...
|
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 be spelled correctly, I need to search by phoneNo. (the fastest way!) I place the cursor in the appropriate field and click search. when the find window opens up, by default, the Look in: field is: 'Main application form: form' and the 'search field...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |