我对使用asp.net Web服务是陌生的,我正在尝试将html表数据绑定到数据库。我的书面代码没有在表格中输出数据,并且不断收到错误消息,请有人帮我。
这是我编写用于数据绑定的代码的地方。
网络服务
using System;
using System.Collections.Generic;
using System.Linq;
using system.web;
using system.web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using system.web.Script.Serialization;
using Newtonsoft.Json;
namespace B*****
{
/// <summary>
/// Summary description for E****_L****_AjaxWS
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 E****_L****_AjaxWS : system.web.Services.WebService {
[WebMethod]
public static string GetRecentCases()
{
string Connectstring =
Configurationmanager.ConnectionStrings["V***"].ConnectionString;
using (SqlConnection con = new SqlConnection(Connectstring))
{
con.Open();
string _data = "";
SqlCommand cmd = new SqlCommand(
"SELECT TOP 10
C.CASE_KEY,C.DEPARTMENT_CASE_NUMber,D.DEPARTMENT_NAME,O.OFFENSE_DESCRIPTION AS CHARGE,LAB_CASE,OFFENSE_DATE
FROM TV_LABCASE C
INNER JOIN TV_DEPTNAME D ON
C.DEPARTMENT_CODE = D.DEPARTMENT_CODE
INNER JOIN TV_OFFENSE O ON
C.OFFENSE_CODE = O.OFFENSE_CODE
ORDER BY CASE_DATE DESC",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
_data = JsonConvert.SerializeObject(ds.Tables[0]);
}
return _data;
}
}
}
}
用于调用Web服务的Ajax函数
AJAX
//Retreive Record
$(document).ready(function () {
//This function will load the datatable
GetData();
});
function GetData() {
$.ajax({
url: 'E****_L*****_AjaxWS.asmx/GetRecentCases',type: 'post',contentType: 'application/json;charset=utf-8',dataType: 'json',data: "{}",success: function (_data) {
_data = JSON.parse(_data.d);
$("#tblCases").find("tr:gt(0)").remove();
for (var i = 0; i < _data.length; i++) {
$("#tblCases").append('<tr><td>' + _data[i].CASE_KEY + '</td><td>'
+ _data[i].DEPARTMENT_CASE_NUMber + '</td><td>'
+ _data[i].CHARGE + '</td><td>'
+ _data[i].LAB_CASE + '</td><td>'
+ _data[i].OFFENSE_DATE + '</td>');
}
},error: function () {
alert("Get Error");
}
});
}
html
<table id="tblCases">
<tr>
<th>CASE_KEY</th>
<th>DEPARTMENT_CASE_NUMber</th>
<th>DEPARTMENT_NAME</th>
<th>CHARGE</th>
<th>LAB_CASE</th>
<th>OFFENSE_DATE</th>
</tr>
</table>