I have created one webservice which name is "
WebService ".
This webservice has one webmethod called "
GetStudentdata".
here is the webmethod code:
[WebMethod]
public List<student> GetStudentdata()
{
student objstudent;
string str = ConfigurationManager.ConnectionStrings["connection_String"].ConnectionString;
SqlConnection conn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("sp_getStudentData", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
DataTable dsInfo = new DataTable();
try
{
adapter.Fill(dsInfo);
List<student> drlist = new List<student>();
foreach (DataRow row in dsInfo.Rows)
{
objstudent = new student();
objstudent.rollnumber = Convert.ToInt32(row["Rollno"]);
objstudent.studentname = row["Studentname"].ToString();
objstudent.subject = row["Subject"].ToString();
drlist.Add(objstudent);
}
return drlist;
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
return null;
}
finally
{
}
}
public class student
{
public int rollnumber;
public string studentname;
public string subject;
}
Now, I have called this webservice in to my .aspx page
here is the .aspx page code.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function call() {
$.ajax({
type: "GET",
url: "http://localhost:2156/Websitetesting/WebService.asmx/GetStudentdata",
dataType: document.json,
error: function (xhr, ajaxOptions, thrownError) { alert('error:' + xhr.status); },
success: function (xmlDoc) {
var abc;
$(xmlDoc).find("ArrayOfStudent").each(function ()
{
$(this).find("student").each(function () {
abc.value = abc + $(this).find('rollnumber').text();
abc.value = abc + $(this).find('studentname').text();
abc.value = abc + $(this).find('subject').text();
alert(abc.value);
});
});
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="call();return false;" />
</form>
</body>
</html>
My webservice return me the data like this.
<arrayofstudent>
<student>
<rollnumber>1</rollnumber>
<studentname>ABC</studentname>
<subject>Maths</subject>
</student>
<student>
<rollnumber>2</rollnumber>
<studentname>Xyz</studentname>
<subject>Computer</subject>
</student>
<student>
<rollnumber>3</rollnumber>
<studentname>Pqr</studentname>
<subject>Science</subject>
</student>
</arrayofstudent>
I want to display this data in to datagrid using javascript,
but I am unable to do.
So please anyone can help me.
thanks in advance.