2
Reply

Data inserting using web service !!!!

Anilananda Chakrabortty

Anilananda Chakrabortty

Jul 5 2017 12:02 PM
182
Hi , Can any one tell how to insert data in to the database using webservice with the help of angularJS in asp.net , (not MVC) .
 
Here i created >>>
1. One html Page
2. Web service asmx page
3. Class Proparty page
 
 
1.========html page========================= 
 
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
var myApp=angular.module('MyApp') 
.controller('addEmployee', function ($scope, RegistrationService) { 
$scope.User = {
Name: '',
Gender: '',
Salary: ''
};
$http({
url: 'employeeService.asmx/addEmployee',
method: 'POST',
data: JSON.stringify(data),
headers: { 'content-type': 'application/json' }
})
});
</script>
</head>
<body ng-app="myApp" ng-controller="addEmployee">
<table border="1" style="border-collapse:collapse">
<tr>
<td>Name</td>
<td><input id="txtName" type="text" ng-model="User.Name" /></td>
</tr>
<tr>
<td>Gender</td>
<td><input id="txtGender" type="text" ng-model="User.Gender" /></td>
</tr>
<tr>
<td>Salary</td>
<td><input id="txtSalary" type="text" ng-model="User.Salary" /></td>
</tr>
<tr>
<td colspan="2">
<input type="button" id="btnAddEmployee" value="Add Employee" ng-click="insertData()" />
</td>
</tr>
</table>
<div ng-controller="addEmployee">{{txtName}}</div>
</body>
</html>
 
 
 
2.============Web service
 
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Services;
namespace WebApplication1
{
///
/// Summary description for employeeService
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class employeeService : System.Web.Services.WebService
{
[WebMethod]
public void addEmployee(Employee emp)
{
//string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand("spInsertEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@Name",
Value = emp.Name
});
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@Gender",
Value = emp.Gender
});
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@Salary",
Value = emp.Salary
});
con.Open();
cmd.ExecuteNonQuery();
}
}
}
}
 
3.============Class proparty====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class Employee
{
public string Name { get; set; }
public string Gender { get; set; }
public string Salary { get; set; }
}
}
 
====
 
Can any one tell the next process ... 
 

Answers (2)