Predictive search isn't working?

This is aspx file code.

<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["crudconnection"].ConnectionString;
    }
    public static List<string> GetAutoCompleteData(string username)
    {
        List<string> result = new List<string>();
        SqlConnection con = new SqlConnection(getconnectionstring();

            using (SqlCommand cmd = new SqlCommand("select DISTINCT username from crudtable where username LIKE '%'+@SearchText+'%'", con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@SearchText", username);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    result.Add(dr["username"].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

Answers


Problem lies in this line of code

cmd.Parameters.AddWithValue("@SearchText", username);

Do not add @ in the parameter name. Use

cmd.Parameters.AddWithValue("SearchText", username);

Second add a breakpoint and check if your code is hit by ajax call or not.


Need Your Help

Overriding public virtual functions with private functions in C++

c++ override access-control virtual-functions

Is there is any reason to make the permissions on an overridden C++ virtual function different from the base class? Is there any danger in doing so?