In case of WebGet you need to provide all parameter in string format.
[Serializable] //Added this new attribute
[DataContract]
public class Employee
{
[DataMember]
public int EmpId { get; set; }
[DataMember]
public string EmpName { get; set; }
[DataMember]
public int Salary { get; set; }
[DataMember]
public string Dept { get; set; }
}
public interface IService1
{
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Employee?id={id}")]
[OperationContract]
List<Employee> GetEmployeeDetails(**string Id**); //Changed int id to string id
}
public List<Employee> GetEmployeeDetails(**string Id**) //Changed int id to string id
{
Employee emp = new Employee();
con.Open();
SqlCommand cmd = new SqlCommand("Select Id,sEmpName,iSalary,sDept from Emp where Id='" + Id + "' ", con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
dr.Read();
emp.EmpId = Convert.ToInt32(dr["Id"]);
emp.EmpName = dr["sEmpName"].ToString();
emp.Salary = Convert.ToInt32(dr["iSalary"]);
emp.Dept = dr["sDept"].ToString();
con.Close();
List<Employee> list = new List<Employee>();
list.Add(emp);
return list;
}
You can also include WCF Trace for tracing any dispatch related logs. Open the WCF trace for getting logs.
<configuration>
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
<listeners>
<add name="wcfTraceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="G:\Errors\WcfTrace.svclog" traceOutputOptions="DateTime" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
UPDATE
function getEmployeeWCF() {
$.ajax({
type: "GET",
url: "http://localhost:1626/Service1.svc/GetEmployeeDetails?id=" + $('#txt1').val() ,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var employee = response.d;
$('#output').empty();
$.each(employee, function (index, emp) {
$('#output').append('<p></strong><br /> Id: ' +
emp.Id + '<br />Name: ' +
emp.Name + '<br />Salary: £' +
emp.Salary + '<br />Department: ' +
emp.Department + '</p>');
});
},
failure: function (msg) {
$('#output').text(msg);
}
});
}