Tuesday, 10 September 2013

jquery using Autocomplete TextBox asp.Database WebService Database

jquery using Autocomplete TextBox asp.Database WebService Database:

aspx:
<head runat="server">
   <title></title>
<link      href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>

//Text Box AutoComplete
    <script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
//Function:
        function SearchText() {
//WebServices Pagename and TextBox Name


            $("[id$='txtSearchName']").autocomplete({
              source: function (request, response) {
               $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "AutoComplete.asmx/GetName",
               data: "{'Name':'" +          document.getElementById('txtSearchName').value + "'}",
                dataType: "json",
                 success: function (data) {
                   response(data.d);
                        },
                        error: function (lstGetName) {
                            alert("Error");
                        }
                    });
                }
            });
        }
</script>
</head>
// TextBox
 <div  align="center">
           <asp:TextBox ID="txtSearchName" runat="server"></asp:TextBox>
           </div>
       

//Webservices
//AutoComplete.asmx

namespace Sample
{
 
/// <summary>
    /// Summary description for AutoComplete
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 AutoComplete : System.Web.Services.WebService
    {

        [WebMethod]
        public List<string> GetName(string Name)
        {
            SqlConnection Conn = new      
           SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            string Select;
            Select = "Select * From Employee Where EmpName Like'%"+Name+"%'";
            da = new SqlDataAdapter(Select, Conn);
            da.Fill(ds);
            List<string> lstGetName = new List<string>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                lstGetName.Add(dr["EmpName"].ToString());
            }
            return lstGetName;
        }
    }
}