Hi
I am getting the following error, see error below and my code
Unhandled exception at line 16, column 5 in http://localhost:8080/Schools/2019.aspx
0x800a1391 - JavaScript runtime error: '$' is undefined
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="2019.aspx.vb" Inherits="_2019" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js" type="text/javascript"></script>
<link rel="Stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css" />
<script type="text/javascript">
$(function () {
$("[id$=txtSearch]").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/CS.aspx/GetCustomers") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
var selectedValue = i.item.label.trim();
$.ajax({
url: '<%=ResolveUrl("~/CS.aspx/GetLink") %>',
data: "{ 'selected': '" + selectedValue + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
document.location.href = data.d;
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
minLength: 1
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter search term:
<asp:TextBox ID="txtSearch" runat="server" />
</div>
</form>
</body>
</html>
aspx.vb file
Imports System.Web.Services
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.Configuration
Partial Class _2019
Inherits System.Web.UI.Page
<WebMethod> _
Public Shared Function GetCustomers(prefix As String) As String()
Dim customers As New List(Of String)()
Using conn As New SqlConnection()
conn.ConnectionString = WebConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As New SqlCommand()
cmd.CommandText = "select TownName from LocTown where TownName like @SearchText + '%'"
cmd.Parameters.AddWithValue("@SearchText", prefix)
cmd.Connection = conn
conn.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
customers.Add(String.Format("{0}-{1}", sdr("TownName"), sdr("TownId")))
End While
End Using
conn.Close()
End Using
End Using
Return customers.ToArray()
End Function
<WebMethod> _
Public Shared Function GetLink(selected As String) As String
' Get DataTable from database that contains url for respective name.
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name"), New DataColumn("Url")})
dt.Rows.Add(1, "Maria", "http://www.aspsnippets.com/")
dt.Rows.Add(2, "Mudassar Khan", "http://www.aspforums.net/Threads")
dt.Rows.Add(3, "Maria Larsson", "http://www.jqueryfaqs.com/")
dt.Rows.Add(4, "Mario Pontes", "http://stackoverflow.com/")
Dim dr As DataRow() = dt.[Select]((Convert.ToString("Name='") & selected) + "'")
Dim url As String = ""
For Each row As DataRow In dr
url = row("Url").ToString()
Next
Return url
End Function
End Class