Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to fetch data but i am unable to do so Here is code Default.aspx
XML
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="project121._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">

</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

    <script src="Scripts/jquery-1.7.1.js"></script>
    <script  type="text/javascript">
        function GetStudents() {

            $("#studentPanel").html("<div style='text-align:center;background-color:yellow;border:1px solid red;'>Please Wait...... Fetching Data</div>")
            $.ajax({
                type: "POST",
                url: "Default.aspx",
                data: "{RollNo"+data.d.SName+"'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: onSuccess,
                error: onError
            });

        }
            function onSuccess(data) {
                var tableContent = "<table border='0'>" +
                    "<tr>" +
                                  "<td>RollNo</td>" +
                                  "<td>SName</td>" +
                                  "<td>Fname</td>" +
                                  "<td>Class</td>" +
                                  "<td>Address</td>" +
                    "</tr>";
                for (var i = 0; i < data.d.lenght;i++){
                    tableContent += "<tr>" +
                                  "<td>" + data.d[i].RollNo + "</td>" +
                                  "<td>" + data.d[i].SName + "</td>" +
                                  "<td>" + data.d[i].Fname + "</td>" +
                                  "<td>" + date.d[i].Class + "</td>" +
                                  "<td>" + date.d[i].Address + "</td>" +
                                    "</tr>";
                    }

                tableContent+="</table>";
                $("#studentPanel").html(tableContent);
            }
            function onError(data) {


            }


    </script>



     <h3> This is database Fetching through JSON</h3>
    <input id="btn" type="button" value="load database" onclick="GetStudents()" />
    <div id="studentPanel">
</div>


</asp:Content>


HERE IS my Default.aspx.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace project121
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
        private static List<student> GetStudents()
        {
            List<student> allStudents = new List<student>();
            using (dbEntitiy dc = new dbEntitiy())
            {

                allStudents = dc.students.ToList();
            }
            return allStudents;


        }
    }
}

Let Me know what is problem with this code
Posted
Comments
abhijeetgupta1988 9-Apr-15 12:42pm    
Your "Data" doesn't seems to be a valid JSON :

data: "{RollNo"+data.d.SName+"'}",

it should be like (I've interchanged parenthesis just for sake my understanding):

data: '{"RollNo" : ' +data.d.SName+'}',

Also the URL should be :

url: "Default.aspx/GetStudents",

url: "Default.aspx/GetStudents",
next
can you check some are data.d
while some are date.d
 
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