Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
This is aspx file code.

XML
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>AutoComplete Box with jQuery</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>
    <script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $(".autosuggest").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "Default.aspx/GetAutoCompleteData",
                        data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            });
        }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div class="demo"></div>
    <div class="ui-widget">
    <label for="tbAuto">Enter UserName: </label>
    <input type="text" id="txtSearch" class="autosuggest" />
    </div>
    </form>
    </body>
    </html>

This is .cs file code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    using System.Web.Services;

    public partial class demo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
         public static string getconnectionstring()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings[&quot;crudconnection&quot;].ConnectionString;
        }
        public static List&lt;string&gt; GetAutoCompleteData(string username)
        {
            List&lt;string&gt; result = new List&lt;string&gt;();
            SqlConnection con = new SqlConnection(getconnectionstring();

                using (SqlCommand cmd = new SqlCommand(&quot;select DISTINCT username from crudtable where username LIKE &#39;%&#39;+@SearchText+&#39;%&#39;&quot;, con))
                {
                    con.Open();
                    cmd.Parameters.AddWithValue(&quot;@SearchText&quot;, username);
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        result.Add(dr[&quot;username&quot;].ToString());
                    }
                    return result;
                }

        }
    }


The database "crud" has "crudtable" with columns (username,firstname,lastname,address,id).When i type anything in the textbox and pres button the ,Search isn't working.
Everything seems to be right but don't know where is the error.

Help friends.Thanks
Posted
Updated 3-Aug-12 21:59pm
v2

1 solution

Default.aspx/GetAutoCompleteData" - does this look like a valid URL to you ? It doesn't to me.

If you want help, you need to tell us, what is not working ? What happens when you try to use it ? What have you done to debug it ? Is an AJAX call happening on the server ? Is it being made on the client ? Is your error message showing ? If so, have you changed it to show the actual error ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900