Hi!
Greetings of the day!
I am developing webform application in asp.net C# in Visual Studio 2015.
For implementing Autocomplete, I downloaded zip file here on C#Corner andun-zip them.
I created a new solution webform WebApplication1 (not emptyone) in asp.net C# in Visual Studio 2015, which creates Site.Master, Default.aspx, Default.aspx.cs, Web.config etc by default.
I pasted the CODE from downloaded files to Default.aspx, Default.aspx.cs also pasted the Jquery files to Scripts Folder and changed the path in src accordingly.
I am able to run the app in browser By removing the words WebApplication1 from the inheritance in line 1 of aspx, <form runat="server">, runat="server" from <html runat=server>.
I never done anything with with 2 dowloaded files jquery.ui.css and web.config.
my final Default.aspxcode is:
- <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %>
-
- <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
- <<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title>AutoComplete Text Box using jQuery in ASP.NET</title>
- <link href="jquery-ui.css" rel="stylesheet" type="text/css" />
- <script src="jquery.min.js" type="text/javascript"></script>
- <script src="jquery-ui.min.js" type="text/javascript"></script>
-
- <script type="text/javascript">
- $(document).ready(function() {
- SearchText();
- });
- function SearchText() {
- $("#txtEmpName").autocomplete({
- source: function(request, response) {
- $.ajax({
- type: "POST",
- contentType: "application/json; charset=utf-8",
- url: "Default.aspx/GetEmployeeName",
- data: "{'empName':'" + document.getElementById('txtEmpName').value + "'}",
- dataType: "json",
- success: function(data) {
- response(data.d);
- },
- error: function(result) {
- alert("No Match");
- }
- });
- }
- });
- }
- </script>
- </head>
- <body>
- <asp:TextBox ID="txtEmpName" runat="server" Width="160px" />
- </body>
- </html>
- </asp:Content>
my final Default.aspx.cs code is:
- using System;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Web.Services;
- using System.Data.SqlClient;
- using System.Collections.Generic;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- [WebMethod]
- public static List<string> GetEmployeeName(string empName)
- {
- string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;" +
- @"AttachDbFilename=|DataDirectory|\Database1.mdf;
- Integrated Security=True;
- Connect Timeout=30";
- List<string> empResult = new List<string>();
- using (SqlConnection con = new SqlConnection(connStr))
- {
- using (SqlCommand cmd = new SqlCommand())
- {
- cmd.CommandText = "SELECT name FROM student WHERE name LIKE ''+@SearchEmpName+'%'";
- cmd.Connection = con;
- con.Open();
- cmd.Parameters.AddWithValue("@SearchEmpName", empName);
- SqlDataReader dr = cmd.ExecuteReader();
- while (dr.Read())
- {
- empResult.Add(dr["name"].ToString());
- }
- con.Close();
- return empResult;
- }
- }
- }
- }
Nothing is showing when I type characters in test box.
When I apply Run Code Analysis on Solution, I am getting 2 Warings as below:
Severity Code Description Project File Line
1) Warning Validation (HTML5): Element 'html' cannot be nested within element 'div'. WebApplication1 C:\Users\Ahmed\documents\visual studio 2015\Projects\WebApplication1\WebApplication1\Default.aspx 5
2) Severity Code Description Project File Line
Warning CA2202 Object 'con' can be disposed more than once in method '_Default.GetEmployeeName(string)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 48 WebApplication1 C:\Users\Ahmed\documents\visual studio 2015\Projects\WebApplication1\WebApplication1\Default.aspx.cs 48
PLEASE HELP ME.
Thanks in advance.