Click here to Skip to main content
15,662,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to call a webservice to add Employee to sql database through my AngularJS application. Http Get method working fine in same application.

POST http://localhost:56330/EmployeeService.asmx/AddEmployee?EmailId=a2S@s&Fname=Rohit&Gender=Male&Lname=Sharma&Locartion=sd&MobileNumber=459401421 500 (Internal Server Error)

When I copy the above link in separate browser, it works.

addEmployee method not working in following code:

app.provider("searchservice", function () {
    this.$get = function ($http, $location) {
        return {
            fetchdata: function () {
                var empinfo = [];
                var url = "EmployeeService.asmx/GetAllEmployees";
                $http.get(url).then(function (response) {
                }, function (response) {
                    console.log("something went wrong while fetching the data");
                return empinfo;
            addEmployee: function (emp) {
                var inputdata = { Fname: emp.firstname,
                    Lname: emp.lastname,
                    Gender: emp.gender,
                    MobileNumber: emp.mobileNumber,
                    Locartion: emp.location
                var response = $http({
                    method: "POST",
                    url: "EmployeeService.asmx/AddEmployee",
                    params: inputdata,
                }).then(function () {
                    console.log("Employee Saved successfully");
                    $scope.allEmployees = searchservice.fetchdata();
                    if ($scope.allEmployees !== null)
                        $scope.result = 'true';
                        $scope.result = 'false';
                }, function () {
                    console.log("some error occured");
                return response;

namespace EmployeeApp
    /// <summary>
    /// Summary description for EmployeeService
    /// </summary>
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    public class EmployeeService : System.Web.Services.WebService
        public string connectionstring = ConfigurationManager.ConnectionStrings["EmployeeDB"].ConnectionString.ToString();

       [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet = true)]
        public void AddEmployee(string Fname, string Lname, string Gender, string MobileNumber, string EmailId,  string Locartion)

            using (SqlConnection con = new SqlConnection(connectionstring))
                string sqlQuery = "Insert into Employee (Fname, Lname, Gender, MobileNumber,EmailId,Locartion) VALUES" +
                    "(@Fname, @Lname, @Gender, @MobileNumber,@EmailId,@Locartion)";
               // Console.WriteLine("" + obj.firstname);
                SqlCommand cmd = new SqlCommand(sqlQuery, con);
                cmd.Parameters.AddWithValue("@Fname", Fname);
                cmd.Parameters.AddWithValue("@Lname", Lname);
                cmd.Parameters.AddWithValue("@Gender", Gender);
                cmd.Parameters.AddWithValue("@MobileNumber", MobileNumber.ToString());
                cmd.Parameters.AddWithValue("@EmailId", EmailId);
                cmd.Parameters.AddWithValue("@Locartion", Locartion);


                int result = cmd.ExecuteNonQuery();

                if (result == 1)
                    //get all the employees details 

               // return obj;


What I have tried:

Adding HTTPGET,HTTPPOST protocols in web.config:

<?xml version="1.0"?>
    <add name="EmployeeDB" connectionString="Data Source=LAPTOP-4S65AVT0\SQLEXPRESS;Initial Catalog=WorkshopDB2;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
        <add name="HttpPostLocalhost"/>
    <compilation debug="true"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

Searched a lot on internet, not finding any solution. Please Help
F-ES Sitecore 6-Mar-18 9:15am    
Use the network tab of the browser tools (f12) to examine the call that is failing. If you look at the response body you'll probably find more detailed info about what went wrong.
Muralikrishna8811 12-Mar-18 5:57am    

Looks like you are defined "AddEmployee" method in service as httpGet try by changing it to POST method in your service handler not sure about the syntax though , As per my understanding it is working fine in browser as it is accepting in GET method

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