Article

Ajax Auto Complete Extender Using Web Services in Asp.Net With Progress bar

08 Jun 2015 Kamal Pratap
0 Comments 1688 Views



Here We Explain how to implement Ajax Auto Complete Extender Using Web Services in Asp.Net With Progress bar.

Ajax Auto Complete Extender Using Web Services in Asp.Net With Progress bar

Database :

Ajax Auto Complete Extender Using Web Services in Asp.Net With Progress bar

CREATE TABLE [dbo].[STUDENT](
	[StudentID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](50) NULL,
	[RollNumber] [decimal](10, 2) NULL,
	[Class] [nvarchar](50) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[STUDENT] ON
INSERT [dbo].[STUDENT] ([StudentID], [Name], [RollNumber], [Class]) VALUES (1, N'Andy', CAST(100.00 AS Decimal(10, 2)), N'MCA')
INSERT [dbo].[STUDENT] ([StudentID], [Name], [RollNumber], [Class]) VALUES (7, N'Anar', CAST(100.00 AS Decimal(10, 2)), N'MCA')
INSERT [dbo].[STUDENT] ([StudentID], [Name], [RollNumber], [Class]) VALUES (3, N'Bill', CAST(200.00 AS Decimal(10, 2)), N'BCA')
INSERT [dbo].[STUDENT] ([StudentID], [Name], [RollNumber], [Class]) VALUES (8, N'Amit', CAST(100.00 AS Decimal(10, 2)), N'MCA')
INSERT [dbo].[STUDENT] ([StudentID], [Name], [RollNumber], [Class]) VALUES (9, N'Anjali', CAST(200.00 AS Decimal(10, 2)), N'BCA')
INSERT [dbo].[STUDENT] ([StudentID], [Name], [RollNumber], [Class]) VALUES (6, N'Chris', CAST(300.00 AS Decimal(10, 2)), N'MBBS')
INSERT [dbo].[STUDENT] ([StudentID], [Name], [RollNumber], [Class]) VALUES (10, N'Avinash', CAST(200.00 AS Decimal(10, 2)), N'BCA')
INSERT [dbo].[STUDENT] ([StudentID], [Name], [RollNumber], [Class]) VALUES (11, N'Rohan', CAST(200.00 AS Decimal(10, 2)), N'BCA')
SET IDENTITY_INSERT [dbo].[STUDENT] OFF

Connection string in Web.config :

<connectionStrings>
    <add name="Web" connectionString="Data Source=*;Initial Catalog=dbname;uid=sa;pwd=sql" providerName="System.Data.SqlClient"/>
</connectionStrings>

Asp.Net :

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Auto Complete Extender Using Web Services</title>
     <script type="text/javascript">
         function ShowProcessImage() {
             var autocomplete = document.getElementById('txtStudent');
             autocomplete.style.backgroundImage = 'url(loading.gif)';
             autocomplete.style.backgroundRepeat = 'no-repeat';
             autocomplete.style.backgroundPosition = 'right';
         }
         function HideProcessImage() {
             var autocomplete = document.getElementById('txtStudent');
             autocomplete.style.backgroundImage = 'none';
         }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
         </asp:ScriptManager>
        <asp:TextBox ID="txtStudent" runat="server"></asp:TextBox>
        <cc1:AutoCompleteExtender ServiceMethod="GetStudents" MinimumPrefixLength="1" ServicePath="WebService.asmx"
            CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" TargetControlID="txtStudent"
            ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false"  OnClientPopulating="ShowProcessImage" OnClientPopulated="HideProcessImage">
        </cc1:AutoCompleteExtender>
    </div>
    </form>
</body>
</html>

WebService.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    public WebService()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public List<string> GetStudents(string prefixText, int count)
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["Web"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select Name, StudentID from STUDENT where " + "Name like @prefix + '%' ";
                cmd.Parameters.AddWithValue("@prefix", prefixText);
                cmd.Connection = conn;
                conn.Open();
                List<string> students = new List<string>();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        students.Add(sdr["Name"].ToString());
                    }
                }
                conn.Close();
                return students;
            }
        }
    }
}

 

Kamal Pratap

I have 6+ years experience in .Net technologies like Asp.Net, C#, WCF, Web Services, SQL Server, Ajax, LinQ. Currently I am working in Netcarrots Loyalty Services as a Software Developer.

Comments

No coments found to display!

Leave a Comment