Hi rani,
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Inline Editing in AngularJS</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', []);
app.controller('MyController', function ($scope, $http, $window) {
$scope.selected = {};
$scope.GetTemplateView = function (customer) {
if (customer.Id === $scope.selected.Id) {
return 'edit';
} else {
return 'display';
}
}
PopulateCustomers($scope, $http);
$scope.EditCustomer = function (customer) {
$scope.selected = angular.copy(customer);
}
$scope.CancelEdit = function () {
$scope.selected = {};
}
$scope.UpdateCustomer = function (index) {
var updatedCustomer = $scope.Customers[index];
Update($scope, $http, updatedCustomer.Id, updatedCustomer.Name, updatedCustomer.Country);
PopulateCustomers($scope, $http);
}
$scope.DeleteCustomer = function (customer) {
if ($window.confirm("Do you want to delete customer " + customer.Name + " ?")) {
Delete($scope, $http, customer.Id);
PopulateCustomers($scope, $http);
}
}
});
function PopulateCustomers($scope, $http) {
$http.post("Default.aspx/GetCustomers", { headers: { 'Content-Type': 'application/json'} })
.then(function (response) {
$scope.Customers = eval(response.data.d);
});
}
function Update($scope, $http, id, name, country) {
var data = { id: id, name: name, country: country };
$http.post("Default.aspx/UpdateCustomer", JSON.stringify(data), { headers: { 'Content-Type': 'application/json'} });
}
function Delete($scope, $http, id) {
var data = { id: id };
$http.post("Default.aspx/DeleteCustomer", JSON.stringify(data), { headers: { 'Content-Type': 'application/json'} });
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div ng-app="MyApp" ng-controller="MyController">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Country</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="customer in Customers" ng-include="GetTemplateView(customer)">
</tr>
</tbody>
</table>
<%--Template For Display View--%>
<script type="text/ng-template" id="display">
<td>{{customer.Name}}</td>
<td>{{customer.Country}}</td>
<td>
<button class="btn btn-primary btn-sm glyphicon glyphicon-edit" ng-click="EditCustomer(customer)"> Edit</button>
<button class="btn btn-danger btn-sm glyphicon glyphicon-remove" ng-click="DeleteCustomer(customer)"> Delete</button>
</td>
</script>
<%--Template For Edit View--%>
<script type="text/ng-template" id="edit">
<td><input class="form-control" type="text" ng-model="customer.Name" width="100px" /></td>
<td><input class="form-control" type="text" ng-model="customer.Country" /></td>
<td>
<button class="btn btn-success btn-sm glyphicon glyphicon-ok" ng-click="UpdateCustomer($index)"> Update</button>
<button class="btn btn-default btn-sm glyphicon glyphicon-erase" ng-click="CancelEdit()"> Cancel</button>
</td>
</script>
</div>
</form>
</body>
</html>
Namespaces
C#
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Services;
VB.Net
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Script.Serialization
Imports System.Web.Services
Code
C#
[WebMethod]
public static string GetCustomers()
{
List<object> customers = new List<object>();
string sql = "SELECT * FROM Customers";
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand(sql))
{
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new
{
Id = sdr["CustomerId"],
Name = sdr["Name"],
Country = sdr["Country"]
});
}
}
conn.Close();
}
return (new JavaScriptSerializer().Serialize(customers));
}
}
[WebMethod]
public static void UpdateCustomer(int id, string name, string country)
{
string sql = "UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @Id";
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand(sql))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
[WebMethod]
public static void DeleteCustomer(int id)
{
string sql = "DELETE FROM Customers WHERE CustomerId = @Id";
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand(sql))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
<WebMethod()>
Public Shared Function GetCustomers() As String
Dim customers As List(Of Object) = New List(Of Object)()
Dim sql As String = "SELECT * FROM Customers"
Using conn As SqlConnection = New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As SqlCommand = New SqlCommand(sql)
cmd.Connection = conn
conn.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
customers.Add(New With {
.Id = sdr("CustomerId"),
.Name = sdr("Name"),
.Country = sdr("Country")
})
End While
End Using
conn.Close()
End Using
Return (New JavaScriptSerializer().Serialize(customers))
End Using
End Function
<WebMethod()>
Public Shared Sub UpdateCustomer(ByVal id As Integer, ByVal name As String, ByVal country As String)
Dim sql As String = "UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @Id"
Using con As SqlConnection = New SqlConnection()
con.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As SqlCommand = New SqlCommand(sql)
cmd.Connection = con
cmd.Parameters.AddWithValue("@Id", id)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", country)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
<WebMethod()>
Public Shared Sub DeleteCustomer(ByVal id As Integer)
Dim sql As String = "DELETE FROM Customers WHERE CustomerId = @Id"
Using con As SqlConnection = New SqlConnection()
con.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As SqlCommand = New SqlCommand(sql)
cmd.Connection = con
cmd.Parameters.AddWithValue("@Id", id)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Screenshot