How to insert data into database using stored procedure and web api in angularjs c# without using entity framework?
SuppliersController.cs :
namespace WebApiDemo.Controllers
{
public class SuppliersController : ApiController
{
[HttpPost]
[ActionName("AddSuppliers")]
public string insertSupplier(Supplier supplier)
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString;
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "INSERT_PURCHASEINVOICE_OPENING";
sqlCmd.Connection = myConnection;
sqlCmd.Parameters.AddWithValue("@SupplierId", supplier.SupplierId);
sqlCmd.Parameters.AddWithValue("@Amount", supplier.Amount);
sqlCmd.Parameters.AddWithValue("@AsOn", supplier.AsOn);
sqlCmd.Parameters.AddWithValue("@CreatedBy", supplier.CreatedBy);
sqlCmd.Parameters.AddWithValue("@CreatedById", supplier.CreatedById);
myConnection.Open();
myConnection.Close();
}
}
}
Supplier.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApiDemo.Models
{
public class Supplier
{
public int SupplierId { get; set; }
public Decimal Amount { get; set; }
public string AsOn { get; set; }
public string CreatedBy { get; set; }
public int CreatedById { get; set; }
}
}
controller.js :
var app = angular.module('myApp', []);
app.controller('supplierController', ['$scope', '$http', supplierController]);
function supplierController($scope, $http) {
//$scope.loading = true;
$scope.add = function () {
debugger;
//$scope.loading = true;
$http.post('http://localhost:57888/api/Suppliers/AddSuppliers', this.newsupplier).success(function (data) {
$scope.suppliers = data;
$scope.newsupplier = '';
}).error(function (data) {
$scope.error = "An error has occured while adding supplier! " + data;
});
}
}
Regards,
skp.