<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;
}
- このコードのどこが間違っていますか? 私は過去2時間からこれに取り組んできました。データベースから名前を取得していません。
- 私はこれで何かが間違っていると思います
emp.Add(obj_result["name"].ToString().TrimEnd());