I'm trying to create a search textbox that will bring up titles as I type them. For instance, when I start typing D..all titles that begin with "D" will appear in drop down menu. I found a code that I believe does this but I'm getting error messages. The error message I am getting is: "cmbMovies not declared" and "CONN_STRING not declared". I created a DataSource named cmbMovies and it did not solve the problem. Am I looking at this correctly? Here is the 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
MoviesSample.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
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
MoviesSample.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