Saturday 17 November 2012

Ajax AutoCompleteExtender Control in Asp.Net in C# using WebServices.


Hi friends , Solution is here.
Here I  have explained AutoCompleteExtender Control using WebServices file.

First Create a web site in visual studio. In this project add a webservices file. Code for Webservices file is given below.

WebServices.asms.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

/// <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 string[] search_source_name(string prefixText)
    {
        SqlConnection con = new SqlConnection("server=SUSHIL-PC;database=BusReservation ;Integrated Security=true;");
        con.Open();
        SqlCommand com = new SqlCommand("select CountryName from Country where CountryName LIKE '" + prefixText + "%' ", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        string[] str = new string[ds.Tables[0].Rows.Count];
        int i=0;
        try
        {
            foreach(DataRow dr in ds.Tables[0].Rows)
            {
                str.SetValue(dr["CountryName "].ToString(),i);
                i++;
            }
        }
        catch{}
        finally
        {
            con.Close();
        }
        return str;


    }
}

After that come to design page. Design the page as the code give below:-
Default.aspx:-

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
                <asp:ServiceReference Path="~/WebService.asmx" />
            </Services>
        </asp:ScriptManager>
    <ul>
        <asp:Label ID="Label1" runat="server" Text="CountryName"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
            DelimiterCharacters="" MinimumPrefixLength="1" Enabled="True" EnableCaching="true"
            ServiceMethod="search_source_name" ServicePath="~/WebService.asmx"
            TargetControlID="TextBox1" CompletionInterval="100">
        </asp:AutoCompleteExtender>
       
       
    </ul>
    </div>
   
    </form>
</body>
</html>


Run the code see the output how AutoCompleteExtender is working. In Next post of Ajax I will come with AutoCompleteExtender without webservice file.  

No comments:

Post a Comment

Note: only a member of this blog may post a comment.