Click here to Skip to main content
16,009,728 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created one webservice which name is " WebService ".
This webservice has one webmethod called "GetStudentdata".
here is the webmethod code:

C#
[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 to populate our DataSet
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = cmd;
        ///DataSet to hold the users information      
        DataTable dsInfo = new DataTable();

        try
        {

            adapter.Fill(dsInfo);

            //return the DataSet to the calling aspx page
            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
<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.
XML
<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.
Posted
Updated 22-Feb-12 5:38am
v3
Comments
ZurdoDev 22-Feb-12 7:52am    
Since you are getting the data back on client side you have to use jQuery/JavaScript to put the data where you want it on the screen. Just access your controls and change the values. You have to do it manually.

1 solution

Hi,

You can modify this[^] example to fit your needs.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900