通过Jquery将Wcf服务datset绑定到网格视图 [英] binding Wcf service datset to grid view through Jquery
问题描述
我编写了一个WCF服务,用于从数据库中检索数据。然后我将其存储到数据集中。然后我通过jQuery服务调用该服务也正常执行并检索数据。但是检索的数据是XML格式我如何将此XML格式转换为普通表以使用绑定网格视图Jquery和问题是当我使用Jquery将检索到的数据集数据绑定到网格视图时我没有在网格视图中获得任何数据。我可以解决这个问题吗?请任何人都可以帮助我....
和我的Jquery功能是
var 类型;
var 网址;
var 数据;
var ContentType;
var DataType;
var ProcessData;
// 调用AXMX / WCF服务的通用函数
函数CallService() {
$ .ajax({
type:Type, // GET或POST或PUT或DELETE动词
url:Url, // 服务的位置
数据:数据, // 发送到服务器的数据
contentType:ContentType, // 发送到服务器的内容类型
dataType:DataType, // 来自服务器的预期数据格式
processdata:ProcessData, // 对错
success:function(msg){ // 在成功的服务电话上
ServiceSucceeded(msg);
},
错误:ServiceFailed // 服务调用失败时
});
}
函数ServiceFailed(结果){
alert(' 服务调用失败:' + result.status + ' ' + result.statusText);
Type = null ; Url = null ;数据= null ; ContentType = null ; DataType = null ; ProcessData = null ;
}
函数WCFJSON(){
// var uesrid =2;
类型= POST;
Url = Service.svc / GetPatientdetails;
数据= ' {Id:}';
ContentType = application / json; charset = utf-8;
DataType = json; ProcessData = true ;
CallService();
}
函数ServiceSucceeded(结果){
if (DataType == json){
resultObject = result.GetPatientdetailsResult;
// for(i = 0; i< resultObject.length; i ++){
// alert(resultObject);
// }
var row = $( [id * = gvpatient] tr:last-child)。clone( true );
// 删除我们创建的虚拟行
$( [id * = gvpatient] tr)。not($( [id * = gvProducts] tr:first-child))。 remove ( );
for ( var i = 0 ; i < resultObject.d.length; i ++){
// < span class =code-comment>将产品属性设置为行并将其添加到gridview
$( td,row).eq( 0 )。html(resultObject.d [i] .lblID);
$( td,row).eq( 1 跨度>)的HTML(resultObject.d [I] .lblPatientId);
$( td,row).eq( 2 跨度>)的HTML(resultObject.d [I] .lblIsPatient);
$( td,row).eq( 3 跨度>)的HTML(resultObject.d [I] .lblFirstname)。
$( td,row).eq( 4 跨度>)的HTML(resultObject.d [I] .lblMiddleinti)。
$( td,row).eq( 5 跨度>)的HTML(resultObject.d [I] .lblLastname)。
$( td,row).eq( 6 跨度>)的HTML(resultObject.d [I] .lblBirthdate);
$( td,row).eq( 7 跨度>)的HTML(resultObject.d [I] .lblMobileno);
$( td,row).eq( 8 跨度>)的HTML(resultObject.d [I] .lblCity);
$( [id * = gvpatient])。append(row);
// 再次获取最后一行的副本。
row = $ ( [id * = gvpatient] tr:last-child)。clone(真跨度>);
}
}
}
函数ServiceFailed(xhr){
alert(xhr.responseText) );
if (xhr.responseText){
var err = xhr.responseText;
if (错误)
error(err);
else
error({Message: 未知的服务器错误。})
}
return ;
}
$(凭证).ready(
function(){
WCFJSON();
}
);
和我的服务功能
公共DataSet GetPatientdetails()
{
string Name = string.Empty;
//从数据库获取详细信息
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [conn]。ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(sp_per_GetPatientDetails,con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
// SqlDataReader dr = cmd.ExecuteReader();
// while(dr.Read())
// {
// Name = dr [Per_Firstname]。ToString();
//}
con.Close();
//返回姓名;
返回ds;
}
.ajax({
type:Type, // < span class =code-comment> GET或POST或PUT或DELETE动词
url:Url, // 服务的位置
数据:数据, // 发送到的数据服务器
contentType:ContentType, // 发送到服务器的内容类型
dataType:DataType, // 来自服务器的预期数据格式
processdata:ProcessData, // 正确或错误
成功:函数(msg){ // 成功的服务电话
ServiceSucceeded(msg);
},
错误:ServiceFailed // 服务调用失败时
});
}
函数ServiceFailed(结果){
alert(' 服务调用失败:' + result.status + ' ' + result.statusText);
Type = null ; Url = null ;数据= null ; ContentType = null ; DataType = null ; ProcessData = null ;
}
函数WCFJSON(){
// var uesrid =2;
类型= POST;
Url = Service.svc / GetPatientdetails;
数据= ' {Id:}';
ContentType = application / json; charset = utf-8;
DataType = json; ProcessData = true ;
CallService();
}
函数ServiceSucceeded(结果){
if (DataType == json){
resultObject = result.GetPatientdetailsResult;
// for(i = 0; i< resultObject.length; i ++){
// alert(resultObject);
// }
var row =
( [id * = gvpatient] tr:last- child)。clone( true );
// 删除我们创建的虚拟行
< blockquote>( [id * = gvpatient] tr)。not(
I have written a WCF service for retrieving data from database. and i am storing that in to a data set.then I am calling that service through jQuery service is executing fine and retrieving data also.But Retrieved data is in XML format how can i convert this XML format to normal table to bind grid view using Jquery and also the problem is while i am binding retrieved data set data to grid view using Jquery i am not getting any data in grid view .how can i solve this?Please any one can help me....
and my Jquery Function is
var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;
//Generic function to call AXMX/WCF Service
function CallService() {
$.ajax({
type: Type, //GET or POST or PUT or DELETE verb
url: Url, // Location of the service
data: Data, //Data sent to server
contentType: ContentType, // content type sent to server
dataType: DataType, //Expected data format from server
processdata: ProcessData, //True or False
success: function(msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null;
}
function WCFJSON() {
// var uesrid = "2";
Type = "POST";
Url = "Service.svc/GetPatientdetails";
Data = '{"Id": ""}';
ContentType = "application/json; charset=utf-8";
DataType = "json"; ProcessData = true;
CallService();
}
function ServiceSucceeded(result) {
if (DataType == "json") {
resultObject = result.GetPatientdetailsResult;
// for (i = 0; i < resultObject.length; i++) {
// alert(resultObject);
// }
var row = $("[id*=gvpatient] tr:last-child").clone(true);
//remove the dummy row we have created
$("[id*=gvpatient] tr").not($("[id*=gvProducts] tr:first-child")).remove();
for (var i = 0; i < resultObject.d.length; i++) {
//Set product attributes to the row and add it to the gridview
$("td", row).eq(0).html(resultObject.d[i].lblID);
$("td", row).eq(1).html(resultObject.d[i].lblPatientId);
$("td", row).eq(2).html(resultObject.d[i].lblIsPatient);
$("td", row).eq(3).html(resultObject.d[i].lblFirstname);
$("td", row).eq(4).html(resultObject.d[i].lblMiddleinti);
$("td", row).eq(5).html(resultObject.d[i].lblLastname);
$("td", row).eq(6).html(resultObject.d[i].lblBirthdate);
$("td", row).eq(7).html(resultObject.d[i].lblMobileno);
$("td", row).eq(8).html(resultObject.d[i].lblCity);
$("[id*=gvpatient]").append(row);
//get the copy of the last row again.
row = $("[id*=gvpatient] tr:last-child").clone(true);
}
}
}
function ServiceFailed(xhr) {
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
$(document).ready(
function() {
WCFJSON();
}
);
and my service function
public DataSet GetPatientdetails()
{
string Name = string.Empty;
// Getting Details from database
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("sp_per_GetPatientDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
//SqlDataReader dr = cmd.ExecuteReader();
//while (dr.Read())
//{
// Name = dr["Per_Firstname"].ToString();
//}
con.Close();
//return Name;
return ds;
}
.ajax({ type: Type, //GET or POST or PUT or DELETE verb url: Url, // Location of the service data: Data, //Data sent to server contentType: ContentType, // content type sent to server dataType: DataType, //Expected data format from server processdata: ProcessData, //True or False success: function(msg) {//On Successfull service call ServiceSucceeded(msg); }, error: ServiceFailed// When Service call fails }); } function ServiceFailed(result) { alert('Service call failed: ' + result.status + '' + result.statusText); Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null; } function WCFJSON() { // var uesrid = "2"; Type = "POST"; Url = "Service.svc/GetPatientdetails"; Data = '{"Id": ""}'; ContentType = "application/json; charset=utf-8"; DataType = "json"; ProcessData = true; CallService(); } function ServiceSucceeded(result) { if (DataType == "json") { resultObject = result.GetPatientdetailsResult; // for (i = 0; i < resultObject.length; i++) { // alert(resultObject); // } var row =
("[id*=gvpatient] tr:last-child").clone(true); //remove the dummy row we have created
("[id*=gvpatient] tr").not(
这篇关于通过Jquery将Wcf服务datset绑定到网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!