Click here to Skip to main content
12,506,584 members (54,316 online)
Rate this:
 
Please Sign up or sign in to vote.
Hi all, I need to create a web service to pass the dataset to javascript function. I don't have that much idea in webservice.I wish to know the step by step process. Can anyone help to solve this out.

Thanks in advance.

arun
Posted 26-Jun-12 19:32pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
arun.m.mr 27-Jun-12 4:24am
   
Thank you Vani Kulkarni.
Vani Kulkarni 27-Jun-12 5:01am
   
Welcome arun!
arun.m.mr 27-Jun-12 5:26am
   
<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>

<script runat="server">
[System.Web.Services.WebMethod]

[System.Web.Script.Services.ScriptMethod]
public static System.Data.DataTable MyMethod(int value)
{
return GetDataSet(value);
}

public static System.Data.DataTable GetDataSet(int value)
{

DataTable dt = new DataTable("Author");
DataRow dr;
dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
dt.Columns.Add(new DataColumn("Author", typeof(string)));

for (int i = 0; i <= 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Author" + i.ToString();
dt.Rows.Add(dr);
}
for (int i = 20; i <= 40; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Author" + i.ToString();
dt.Rows.Add(dr);
}


DataView dv = new DataView(dt);
dv.RowFilter = "Id='" + value + "'";



return dv.Table;
}



</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>

<script language="javascript" type="text/javascript">
function CallMyWebService()
{
WebService.MyMethod(1,OnRequestComplete);
}
function OnRequestComplete(result)
{
var str="";
for(var i = 0; i < result.rows.length; i++)

{
str+=result.rows[i].Author +"<br>";

}
document.getElementById('result').innerHTML=str;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="scriptManger1" runat="server"
EnablePageMethods="true">


<div id="result">
</div>
<input type="button" önclick="CallMyWebService();" id="myButton"
value="Call MyWebService" />
</div>
</form>
</body>
</html>
if you tried, you likely received some sort of circular reference serialization error to take advantage of this, you will need to add the following to your web.config:







I use the same code. But it is not working. Can u check it Vani.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160927.1 | Last Updated 27 Jun 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100