Auto Search Using Jquery and Generic Handler in Asp.Net/C#.Net

Auto Search Using Jquery and Generic Handler in Asp.Net/C#.Net

Step 1 : Create a table :
 
create table DbTable (
ID int Primary key identity (1,1),
Name nvarchar(max) Not null,
Manufacturer nvarchar(max),
Model nvarchar(max),
 );


Step 2 : Open Visual Studio And Right Click on our Project Take a GenericHandler


Step 3 : Write This Code in a handler

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Sql;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.SessionState;

namespace AMS_New_Project.Handler
{
    /// <summary>
    /// Summary description for HW_AssetName
    /// </summary>
    public class HW_AssetName : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string term = context.Request["term"] ?? "";
            List<string> listofassetnames = new List<string>();
            string constr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
 
                con.Open();
                SqlCommand cmd = new SqlCommand("select Name from DbTable where Name like @name + '%' group by Name", con);
                cmd.Parameters.Add(new SqlParameter()
                {
                    ParameterName = "@Name",
                    Value = term,
                });
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    listofassetnames.Add(rdr["Name"].ToString());
                }
                con.Close();

            }
            JavaScriptSerializer jss = new JavaScriptSerializer();
            context.Response.Write(jss.Serialize(listofassetnames));



        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

Step 4 : Take One Webform Add below script on Head section :
<head>
<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 href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css"
        rel="Stylesheet" type="text/css" />
    <%--Name--%>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#TextBox1").autocomplete({
                source: "Handler1.ashx"
            });
        });
    </script>
</head>

Step 5 : Take a Text box:
 <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </div>

Now execute this webform:





Comments

Popular posts from this blog

Automatically send Birthday email using C#.Net

Drag and Drop multiple File upload using jQuery AJAX in ASP.Net using C# and VB.Net

Difference between each and map in jquery