I am new to angularjs and asp.net Web Method. Here i created a table in asp.net .aspx form. I bind the values, i created a controller for this Product.aspx. I used Web method to pass the data to the controller. What i actually facing the problem is, i could not able to get the data from the database and display into the html table. I am not getting any error in build time and also in the console. Please need help.
Product.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Product.aspx.cs" ClassName="Product" Inherits="Product" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="product/productcontroller.js"></script>
<body data-ng-app="app">
<form>
<div data-ng-controller="GridController" data-ng-init="firstCall()">
<button type="submit" data-ng-click="firstCall()" value="Fetch" class="btn btn-primary">Show</button>
<h2> Table </h2>
<div class="table-responsive com-table">
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th data-width="5%">productcode</th>
<th data-width="15%">productname</th>
<th data-width="15%">productprice</th>
<th data-width="15%">productqty</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="PR in ProductList">
<th data-width="5%">{{PR.productcode}}</th>
<th data-width="15%">{{PR.productname}}</th>
<th data-width="15%">{{PR.productprice}}</th>
<th data-width="15%">{{PR.productqty}}</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</form>
</body>
</html>
productcontroller.js
I am also not getting any eoor in this file, the problem is , unable to retrieve(GET) the data from the Product.aspx.cs file.
var app = angular.module("app", [])
app.controller("GridController", function ($scope, $http) {
$scope.firstCall = function () {
$http({
url: 'Product.aspx/GetProductInfo',
dataType: 'json',
method: 'GET',
data: '',
headers: {
"Content-Type": "application/json"
}
}).then(function (success) {
//debugger;
$scope.ProductList = success;
}, function (error) {
console.log(error, 'can not get data');
});
}
});
Product.aspx.cs
I created a List, and tried to fetch the data from the database and i called this method from the productcontroller.js , but i could not make it.
public class ProductInfo
{
public int productcode { get; set; }
public string productname { get; set; }
public float productprice { get; set; }
public int productqty { get; set; }
}
public partial class Product : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// GetProductInfo() //i called this function, then also not working.
}
[WebMethod]
[ScriptMethod(UseHttpGet = true,ResponseFormat =ResponseFormat.Json)]
public static List<ProductInfo> GetProductInfo()
{
List<ProductInfo> productinfos = new List<ProductInfo>();
using (NpgsqlConnection con = new NpgsqlConnection("Server=127.0.0.0;Port=1234;Database=TEST_DB;User Id=test;Password=****;"))
{
using (NpgsqlCommand cmd = new NpgsqlCommand("select *from product1", con))
{
con.Open();
NpgsqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
ProductInfo productInfo = new ProductInfo
{
productcode = Convert.ToInt32(rdr["productcode"].ToString()),
productname = rdr["productname"].ToString(),
productprice = Convert.ToInt32(rdr["productprice"].ToString()),
productqty = Convert.ToInt32(rdr["productqty"].ToString())
};
productinfos.Add(productInfo);
}
con.Close();
}
}
return productinfos;
}
}