Web服务在本地主机上运行但不在线 [英] Web service runs on local host but not online
问题描述
我使用自动填充扩展程序在文本框上进行自动填充搜索。为此,我使用了一个Web服务。当我在本地主机上运行代码时,一切运行正常。但是当我在线上传代码时,ajax autocomplete extender不会调用web服务方法。下面是代码。
.aspx Page
I have used a autocomplete extender for autocomplete search on textbox. For this i have used a web service. When I run the code on local host everything runs fine. But when i upload the code online ajax autocomplete extender does not invoke web service method. Below is the code.
.aspx Page
<asp:ScriptManager ID="mgr" runat="Server">
<Services>
<asp:ServiceReference Path="~/AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
AutoComplete Extender:
AutoComplete Extender:
<asp:TextBox ID="txtKeyword1" runat="server" CssClass="search-field"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender
runat="server"
BehaviorID="autoComplete1"
ID="autoComplete1"
TargetControlID="txtKeyword1"
ServicePath="~/AutoComplete.asmx"
ServiceMethod="GetCompletionList2"
MinimumPrefixLength="1"
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="10"
CompletionListCssClass="autocomplete_completionListElement"
CompletionListItemCssClass="autocomplete_listItem"
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
DelimiterCharacters=";, :" OnClientPopulating="ShowProcessImage"
OnClientPopulated="HideProcessImage">
</ajaxToolkit:AutoCompleteExtender>
网络服务
使用系统;
使用System.Collections;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
使用System.Web.UI;
使用System.Web.Services.Protocols;
使用System.Xml.Linq;
使用System.Data;
使用System.Data.OleDb;
使用System.Collections.Generic;
使用System.Web.Script.Services;
使用System .XML; < br $>
///< summary>
///自动完成的摘要说明
///
[WebService(Namespace =http://tempuri.org/)]
[WebServiceBinding( ConformsTo = WsiProfiles.BasicProfile1_1)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
公共类AutoComplete:System.Web.Services.WebService
{
public AutoComplete()
{
//如果使用设计组件,请取消注释以下行
// InitializeComponent();
}
[WebMethod]
公共字符串HelloWorld()
{
返回你好;
}
[WebMethod]
public string [] GetCompletionList2(string prefixText,int count)
{
// ADO.Net
OleDbConnection cn = new OleDbConnection();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
//字符串strCn =Provider = Microsoft.Jet.OLEDB.4.0;数据源= E:\\F \\Hella \\ Force One \\Code \\\ \\ Force One 2014年5月14日\\HellaWarranty.mdb;;
String strCn =Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\Websites\ \ HellaWarranty \\HellaWarranty.mdb;;
cn.ConnectionString = strCn;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
//比较文本框中的字符串(prefixText)和字符串来自DataBase中的列(CompanyName)
//如果DataBase中的String等于TextBox中的String(prefixText),则将其添加到返回ItemList
// -----我定义了一个参数,而不是直接传递值以防止sql注入-------- //
cmd.CommandText =SELECT * FROM CaseMaster where CaseCode like'+ prefixText +% ';
//cmd.Parameters.AddWithValue(\"@Name,prefixText);
尝试
{
cn.Open();
cmd.ExecuteNonQuery();
OleDbDataAdapter da =新的OleDbDataAdapter(cmd);
da.Fill(ds);
}
catch
{
}
终于
{
cn.Close();
}
dt = ds.Tables [0 ];
//然后返回字符串列表(txtItems)作为结果
List< string> txtItems = new List< string>();
String dbValues;
foreach(dt.Rows中的DataRow行)
{
//来自DataBase的字符串(dbValues)
dbValues = row [CaseCode]。ToString();
dbValues = dbValues.ToLower();
txtItems.Add(dbValues);
}
返回txtItems.ToArray();
}
}
任何人都可以帮我这个。
Web Service
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Web.Script.Services;
using System.Xml;
/// <summary>
/// Summary description for AutoComplete
///
[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 AutoComplete : System.Web.Services.WebService
{
public AutoComplete ()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello";
}
[WebMethod]
public string[] GetCompletionList2(string prefixText, int count)
{
//ADO.Net
OleDbConnection cn = new OleDbConnection();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
//String strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\F\\Hella\\Force One\\Code\\Force One 14 May 2014\\HellaWarranty.mdb;";
String strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Websites\\HellaWarranty\\HellaWarranty.mdb;";
cn.ConnectionString = strCn;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
//Compare String From Textbox(prefixText) AND String From Column in DataBase(CompanyName)
//If String from DataBase is equal to String from TextBox(prefixText) then add it to return ItemList
//-----I Defined a parameter instead of passing value directly to prevent sql injection--------//
cmd.CommandText = "SELECT * FROM CaseMaster where CaseCode like '" + prefixText + "%'";
//cmd.Parameters.AddWithValue("@Name", prefixText);
try
{
cn.Open();
cmd.ExecuteNonQuery();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
}
catch
{
}
finally
{
cn.Close();
}
dt = ds.Tables[0];
//Then return List of string(txtItems) as result
List<string> txtItems = new List<string>();
String dbValues;
foreach (DataRow row in dt.Rows)
{
//String From DataBase(dbValues)
dbValues = row["CaseCode"].ToString();
dbValues = dbValues.ToLower();
txtItems.Add(dbValues);
}
return txtItems.ToArray();
}
}
Can Anyone Help me with this.
推荐答案
让我来吧:)
您正在吞咽异常,删除try catch块并再次部署应用程序。然后你可以看到异常并为此找到解决方案。
最有可能存在与OLEDB驱动程序和访问数据库文件,权限问题等相关的问题。
为什么访问数据库?您最好转移到SQL Express,SQlite或其他一些数据库服务器。
Let me gues :)
you are swallowing exceptions, remove the try catch block and deploy the application again. Then you can see the exception and find solution for that.
most probably there can be a issue related to OLEDB driver and access database file, permission issue etc..
Why access database? you better move to SQL express, SQlite or some other database server.
这篇关于Web服务在本地主机上运行但不在线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!