Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="serviceEmp">
<head runat="server">
    <title></title>
    <script src="angular.js"></script>
    <script src="angular.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div ng-controller="empController">
            search:<input type="text" ng-model="search" />
            <table>
                <tr ng-repeat="emp in employees | filter:search">
                    <td>{{emp.EmpId }}
                    </td>
                    <td>{{emp.EmpName}}
                    </td>
                    <td>{{emp.Contact }}
                    </td>
                    <td>{{emp.EmailID }}
                    </td>
                </tr>
            </table>
        </div>
        <script>
            var app = angular.module('serviceEmp', []);
            app.controller('empController', function ($scope, $http) {
                var url = "MY_Service.asmx/HelloWorld";
                $http.get(url).success(function (data) {
                    var myjson = JSON.parse(data);
                    $scope.employees = JSON.parse(myjson);
                })
            });


        </script>
    </form>
</body>
</html>

C#
[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    
    public class MY_Service : System.Web.Services.WebService
    {
        RetriveDataByAngularjs retrive = new RetriveDataByAngularjs();
        [WebMethod]
        //[System.Web.Script.Services.ScriptService]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public string HelloWorld()
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            string str=    retrive.ConvertDataTableTo_JSON_String(retrive.GetDataTable());
            Context.Response.Write(js.Serialize(str));
           
            return str;
        }
}

XML
public DataTable GetDataTable()
        {
            DataTable dataTable = new DataTable();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select * from Employee";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dataTable);
            return dataTable;
        }

        public string ConvertDataTableTo_JSON_String(DataTable dataTable)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<String, Object>> tableRows = new List<Dictionary<String, Object>>();
            Dictionary<String, Object> row;
            foreach (DataRow dr in dataTable.Rows)
            {
                row = new Dictionary<String, Object>();
                foreach (DataColumn col in dataTable.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                tableRows.Add(row);
            }
            string str = serializer.Serialize(tableRows);
            return str;
        }
Posted
Updated 17-Mar-15 19:48pm
v3
Comments
Member 10483577 17-Mar-15 5:25am    
It is not working plz help anybody

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