I am new to using asp.net web service and I am trying to data-bind the html table to database. my written code doesn't output the data in the table and it keeps getting an error message, can someone please help me.
This is where I written my code for data-binding.
web service
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;
}
}
}
}
Ajax function for calling the web service
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>