Calling Web service using jquery returns 500 internal server error?

694 Views Asked by At

I written webservice to insert data to my database and webservice works when i access the service using url "http:\\localhost\Webservice.asmx".If i called the same url using jquery ajax it returns (500)Internal server. This is my source code.

//Code to insert Customer Details

$('#submitAddDetail').on('click', function (e) {

        //alert('Hello World');
        var custId = $(".addcustomer-body #custId").val();
        var custName = $(".addcustomer-body #custName").val();
        var custCity = $(".addcustomer-body #custCity").val();
        var custPostalCode = $(".addcustomer-body #custPostalCode").val();
        var custCountry = $(".addcustomer-body #custCountry").val();
        var custPhone = $(".addcustomer-body #custPhone").val();
        var custFax = $(".addcustomer-body #custFax").val();
        debugger;
        //alert(custName);
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "WebService.asmx/AddCustomers",
            data: "{ 'sCustomerId': '" + custId + "','sCompanyName': '" + custName + "','sCity':'" + custCity + "','sPostalCode':'" + custPostalCode + "','sCountry':'" + custCountry + "','sPhone:'" + custPhone + "','sFax':'" + custFax + "'}",                
            success: function (data) {
                debugger;
                if (data.d == 'true') {
                    location.reload();
                }
                else {
                    debugger;
                    alert('Error in Inserting data');
                }
            }
        });
    });

//My Webservice to insert Customer Data. [WebMethod]

 public string AddCustomers(string sCustomerId, string sCompanyName, string sCity, string sPostalCode, string sCountry, string sPhone, string sFax)
    {
        string msg = string.Empty;
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            string sQuery = "INSERT INTO Customers (CustomerID,CompanyName,City,PostalCode,Country,Phone,Fax)VALUES" +
                            "(@CustomerID,@CompanyName,@City,@PostalCode,@Country,@Phone,@Fax)";
            using (SqlCommand cmd = new SqlCommand(sQuery))
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@CustomerID", sCustomerId);
                cmd.Parameters.AddWithValue("@CompanyName", sCompanyName);
                cmd.Parameters.AddWithValue("@City", sCity);
                cmd.Parameters.AddWithValue("@PostalCode", sPostalCode);
                cmd.Parameters.AddWithValue("@Country", sCountry);
                cmd.Parameters.AddWithValue("@Phone", sPhone);
                cmd.Parameters.AddWithValue("@Fax", sFax);
                con.Open();
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    msg = "true";
                }
                else
                {
                    msg = "false";
                }
            }
        }
        return msg;
    }//End

I need your help in solving this issue.Thanks in advance.

1

There are 1 best solutions below

0
On

I changed a line in Jquery syntax and it worked.

data: '{"sCustomerId":"' + custId + '","sCompanyName":"' + custName + '","sCity":"' + custCity + '","sPostalCode":"' + custPostalCode + '","sCountry":"' + custCountry + '","sPhone":"' + custPhone + '","sFax":"'+custFax+'"}',

Thanks.