Click here to Skip to main content
14,937,212 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.

Error:
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) {
                      angular.copy(response.data,empinfo);
                }, 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,
                    EmailId: emp.email,
                    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';
                    else
                        $scope.result = 'false';
                   
                }, function () {
                    console.log("some error occured");
                });;
               console.log(response);
                return response;
            }
            
        }
    }
})


webserice:
namespace EmployeeApp
{
    /// <summary>
    /// Summary description for EmployeeService
    /// </summary>
    [WebService]
    [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
    {
        public string connectionstring = ConfigurationManager.ConnectionStrings["EmployeeDB"].ConnectionString.ToString();

  [WebMethod]
       [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)";
                con.Open();
               // 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 
                }

                con.Close();
                Context.Response.Write("success");
               // return obj;

            }
        }
    }


What I have tried:

Adding HTTPGET,HTTPPOST protocols in web.config:

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


Searched a lot on internet, not finding any solution. Please Help
Posted
Comments
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
   
Hi,

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