locked
Insert data using WCF Rest based service RRS feed

  • Question

  • User1216489373 posted

    Hi,

    I want to inser data using my REST based service. While I am consuming the service, my service is not hit. My code is as below:-

    //IService1.cs       
     [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
            [OperationContract]
            void AddEmployee(string EmpName, int Salary, string Dept);
    
    
    //Service1.svc.cs
            public void AddEmployee(string EmpName, int Salary, string Dept)
            {
                //Employee emp = new Employee();
                con.Open();
                SqlCommand cmd = new SqlCommand("Insert into Emp (sEmpName,iSalary,sDept) Values('" + EmpName + "','" + Salary + "','" + Dept + "') ", con);
                cmd.ExecuteNonQuery();
                con.Close();
            }

    Consuming Service on my aspx page

                function insertEmployeeWCF() {
                    var Name = $('#txt2').val();
                    var Salary = $('#txt3').val();
                    var Department = $('#txt4').val();
    
                    $.ajax({
                        type: "POST",
                        url: "http://localhost:1626/Service1.svc/AddEmployee",
                        //data: "{}",
                        data: "{'EmpName':'" + Name + "','Salary':'" + Salary + "','Dept':'" + Department + "'}",
                        contentType: "application/json; charset=utf-8",
                        async: false,
                        dataType: "json",
                        success: function (response) {
                            var employee = response.d;
                            $('#<%=lbl5.ClientID%>').html("Value successfully inserted")
    
                        }
                    });
    
                }

    Please help!!

    Thanks,

    Rahul

    Thursday, May 22, 2014 1:55 PM

Answers