我正在嘗試使用Web服務來從數據庫中讀取自動完成文本框。當我嘗試編譯我的錯誤Web服務自動完成失敗ASP.NET
HttpExceptionUnhandled
標記和JS與其中發生異常
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"
type="text/javascript"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"
rel="Stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
$(document).ready(function() {
$("#txtSearch").autocomplete({
source: function (request, response) {
$.ajax({
error on this line url: '<%=ResolveUrl("~/Service.asmx/GetDrugs") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("#txtHidden").val(i.item.val);
},
minLength: 1
});
});
</script>
<form id="form1" runat="server">
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:HiddenField ID="txtHidden" runat="server" />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit" />
</form>
</asp:Content>
的指示。當我試圖運行在瀏覽器中的Web服務,我能看到的方法列表可用並點擊GetDrugs
,它會帶我進入一個屏幕,我可以輸入文字,但也會失敗。當它失敗時,我在瀏覽器中收到以下文本
當我嘗試調用Web服務時發生錯誤。這是在重新構建SQL並在while循環中向讀者添加ToString()之後的。
System.FormatException: Input string was not in a correct format.
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at System.String.Format(String format, Object arg0)
at Service.GetDrugs(String prefix) in c:\Users\dtceci2\Desktop\WebSite2\App_Code\Service.cs:line 45
連接字符串罰款和SQL Server在我的本地機器上運行。下面是Web服務
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Services;
/// <summary>
/// Summary description for Service_CS
/// </summary>
[WebService(Namespace = "http://localhost/~Service.asmx")]
[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 Service : System.Web.Services.WebService
{
public Service()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] GetDrugs(string prefix)
{
List<string> drugs= new List<string>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select distinct drug_name from rx where drug_name like @SearchText" + "'%'";
cmd.Parameters.AddWithValue("@SearchText", prefix);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
drugs.Add(string.Format("{0}}", rdr["drug_name"].ToString()));
}
}
conn.Close();
}
return drugs.ToArray();
}
}
}
後面的代碼爲我的標記網頁的代碼只是
public partial class _Default : System.Web.UI.Page
{
protected void Submit(object sender, EventArgs e)
{
string drugName = Request.Form[txtSearch.Text];
}
}
什麼我搞亂任何線索?
烏爾'WebMethod'被'GetCustomers' ....其中是'GetDrugs' ?? – 2013-04-10 15:53:39
我會建議開放'Fiddler'並檢查您發送到Web服務的數據。它可能是畸形的。現在問題的一部分,現在有一個格式異常。謝謝,雖然:) – Justin 2013-04-10 15:53:46