-1
<script type="text/javascript">
    $(function () {
        $("[id$=txtAuto]").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "NameList.asmx/GetNameList",
                    data: "{ 'Name': '" + request.term + "' }",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    async: true,
                    success: function (data) {
                        var Details = [];
                        for (i = 0; i < data.d.length; i++) {
                            Details[i] = data.d[i].Name;
                        } response(Details);
                    },
                    error: function (result) {
                    }
                });
            }
        });
    });
</script>

public class NameList : System.Web.Services.WebService
{
    [WebMethod]
    public List<UserNameList> GetNameList(string Name)
    {
        var emp = new UserNameList();
        var fetchName = emp.GetEmpList()
        .Where(m => m.Name.ToLower().StartsWith(Name.ToLower()));
        return fetchName.ToList();
    }  
}

public class UserNameList
{
    public int ID { get; set; }
    public string Name { get; set; }

    public List<UserNameList> GetEmpList()
    {
        List<UserNameList> emp = new List<UserNameList>();
       string QueryString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        using (SqlConnection obj_SqlConnection = new SqlConnection(QueryString))
        {
            using (SqlCommand obj_Sqlcommand = new SqlCommand("Select DISTINCT name as txt from lib_memtable where name like  @SearchText +'%' ", obj_SqlConnection))
            {
                obj_SqlConnection.Open();
                obj_Sqlcommand.Parameters.AddWithValue("@SearchText", Name);
                SqlDataReader obj_result = obj_Sqlcommand.ExecuteReader();
                while (obj_result.Read())
                {
                    emp.Add(obj_result["name"].ToString().TrimEnd());
                }
            }
        }

        return emp;
    }
  1. 这段代码有什么问题?从过去的两个小时开始,我一直在研究这个问题。它没有从数据库中获取名称。
  2. 我想这有什么问题emp.Add(obj_result["name"].ToString().TrimEnd());
4

2 回答 2

2

在你的 SQL 中,你有Select DISTINCT name as txt. 当您向下阅读时,您将其称为name,而不是txt

如果您将 while 内的行更改为此它应该可以工作

emp.Add(obj_result["txt"].ToString().TrimEnd());
于 2013-05-30T11:08:15.430 回答
0

如果您只是返回一个字符串列表,您应该更改 to 的返回类型UserNameList.GetEmpList()并相应地List<string>修改代码,它应该如下所示:

public class UserNameList
{ 
    public int ID { get; set; }
    public string Name { get; set; }

    public List<string> GetEmpList()
    {
        List<string> emp = new List<string>();
        string QueryString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        using (SqlConnection obj_SqlConnection = new SqlConnection(QueryString))
        {
            using (SqlCommand obj_Sqlcommand = new SqlCommand("Select DISTINCT name as txt from lib_memtable where name like  @SearchText +'%' ", obj_SqlConnection))
             {
                obj_SqlConnection.Open();
                obj_Sqlcommand.Parameters.AddWithValue("@SearchText", Name);
                SqlDataReader obj_result = obj_Sqlcommand.ExecuteReader();
                while (obj_result.Read())
                {
                    emp.Add(obj_result["name"].ToString().TrimEnd());
                }
            }
        }

        return emp;
    }
}
于 2013-05-30T11:10:37.860 回答