Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何正确格式化Web服务以将数据从SQL返回到jQuery自动完成?_C#_Jquery_Asp.net_Ajax_Json - Fatal编程技术网

C# 如何正确格式化Web服务以将数据从SQL返回到jQuery自动完成?

C# 如何正确格式化Web服务以将数据从SQL返回到jQuery自动完成?,c#,jquery,asp.net,ajax,json,C#,Jquery,Asp.net,Ajax,Json,我一直在Google上,尝试一个又一个AJAX和jQueryAutoComplete的例子 我已经决定使用jQuery的脚本在我的页面上呈现autocomplete方法,并且我正在使用web服务从我的SQL Ce数据库收集数据 这是我从jquery收到的错误消息,我很确定它来自我的Web服务Marr是万豪的缩写,我开始键入搜索词。 {“Message”:“@SearchText:Marr-输入字符串格式不正确。”,“StackTrace”:“at System.Data.SqlServerCe.

我一直在Google上,尝试一个又一个AJAX和jQueryAutoComplete的例子

我已经决定使用jQuery的脚本在我的页面上呈现autocomplete方法,并且我正在使用web服务从我的SQL Ce数据库收集数据

这是我从jquery收到的错误消息,我很确定它来自我的Web服务Marr是万豪的缩写,我开始键入搜索词。

{“Message”:“@SearchText:Marr-输入字符串格式不正确。”,“StackTrace”:“at System.Data.SqlServerCe.SqlCeCommand.FillParameterDataBindings(布尔验证值)\r\n at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(命令行为、字符串方法、结果选项)\r\n在System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior行为)\r\n在System.Data.SqlServerCe.SqlCeCommand.ExecuteReader()\r\n在GetClients.GetClientNames(字符串前缀)处,在c:\Users\CRH-DEV\Desktop\DEV Projects\HelpDesk\App\u Code\GetClients.cs:第42行,“异常类型”:“System.FormatException”}

错误源于我的Web服务的.cs的第42行,但我不太确定我在看什么。我猜数据的格式不正确

我使用的例子来自这个网站

这是我完整的网络服务

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Web.Script.Services;
using System.Data.SqlServerCe;

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

public GetClients()
{

    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] GetClientNames(string prefix)
{
    List<string> customers = new List<string>();
    using (SqlCeConnection conn = new SqlCeConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlCeCommand cmd = new SqlCeCommand())
        {
            cmd.CommandText = "SELECT [Name], [ID] FROM [Current] WHERE " +
            "[Name] LIKE @SearchText + '%'";
            cmd.Parameters.AddWithValue("@SearchText", prefix);
            cmd.Connection = conn;
            conn.Open();
            using (SqlCeDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(string.Format("{0}-{1}", sdr["Name"], sdr["ID"]));
                }
            }
            conn.Close();
        }
        return customers.ToArray();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Web;
使用System.Web.Services;
使用系统配置;
使用System.Web.Script.Services;
使用System.Data.SqlServerCe;
/// 
///服务的摘要说明
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类GetClients:System.Web.Services.WebService
{
公共GetClients()
{
//如果使用设计的组件,请取消注释以下行
//初始化组件();
}
[网络方法]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
公共字符串[]GetClientNames(字符串前缀)
{
列出客户=新列表();
使用(SqlCeConnection conn=new SqlCeConnection())
{
conn.ConnectionString=ConfigurationManager
.ConnectionString[“ConnectionString”]。ConnectionString;
使用(SqlCeCommand cmd=new SqlCeCommand())
{
cmd.CommandText=“从[Current]中选择[Name],[ID]。”+
“[名称]类似于@SearchText+'%'”;
cmd.Parameters.AddWithValue(“@SearchText”,前缀);
cmd.Connection=conn;
conn.Open();
使用(SqlCeDataReader sdr=cmd.ExecuteReader())
{
while(sdr.Read())
{
Add(string.Format(“{0}-{1}”、sdr[“Name”]、sdr[“ID”]);
}
}
康涅狄格州关闭();
}
返回客户。ToArray();
}
}
}

我的aspx页面..(页眉)


$(文档).ready(函数(){
$(“#”)自动完成({
来源:功能(请求、响应){
$.ajax({
url:“”,
数据:“{'前缀':'”+request.term+“}”,
数据类型:“json”,
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(数据){
响应($.map)(数据.d,函数(项){
返回{
标签:item.split('-')[0],
val:item.split('-')[1]
}
}))
},
错误:函数(响应){
警报(response.responseText);
},
故障:功能(响应){
警报(response.responseText);
}
});
},
选择:功能(e,i){
$(“#”)val(i.item.val);
},
最小长度:1
});
});
aspx(body)

    <h1>jQuery Autocomplete Lab</h1>
<asp:TextBox ID="ClientSearch" runat="server"></asp:TextBox>
<asp:HiddenField ID="hfClientID" runat="server" />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick = "Submit" />
jQuery自动完成实验室

如果需要,我可以发布其他代码,但我认为问题在于我的Web服务

我将非常感谢对这个问题的任何反馈。我认为,在我尝试过的所有例子中,Web服务一直在扼杀我的生命

亲切问候,,
-Cody

您需要从Web服务返回JSON

JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(YourArray);
还可以将webservice函数的返回类型更改为string

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetClientNames(string prefix)
{
 //YOUR CODE
}

哪一条是42号线?(这是
cmd.ExecuteReader()
one,不是吗?)很抱歉,是的。我想问题可能出在第39行
[Name]中,比如@SearchText+'%'
。你应该像这样尝试
prefix=“”+prefix+“%”。可能是。我尝试过在查询中封装类似参数的各种示例;似乎有帮助。我现在在建议区域内收到一条消息。System.Collection.Generic.List如何格式化字符串json=js.Serialize(YourArray);用我的数组?
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetClientNames(string prefix)
{
 //YOUR CODE
}