I am tying to make a project related to wcf and json but whenever I run this code I am getting null value in wcf. showing you my code plz check and correct me if I am wrong
wcf code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class
EmployeeService
{
[OperationContract]
[System.ServiceModel.Web.WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
public Employee
GetEmployeeById(string employeeId)
{
Employee employee = new Employee();
string cs = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
string query = "select top 1 id, empname,address,basicsalary from employee where id=@id";
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
//cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramid = new SqlParameter();
paramid.ParameterName = "@id";
paramid.Value = employeeId;
cmd.Parameters.Add(paramid);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
employee.name = rdr["empname"].ToString();
employee.address = rdr["address"].ToString();
employee.basicSalary = rdr["basicsalary"].ToString();
}
return employee;
}
}
// Add more operations here and mark them with [OperationContract]
}
What I have tried:
html code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<title>
$(document).ready(function () {
$("#btnGetEmployee").click(function () {
var empid = $("#txtid");
var employeeId = empid.val();
$.ajax({
//url: 'EmployeeService.svc/GetEmployeeById',
url: 'http://localhost:29764/EmployeeService.svc/GetEmployeeById',
type: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ GetEmployeeById: employeeId }),
//data: "{'GetEmployeeById':'" + employeeId + "'}",
dataType: 'json',
success: function (data) {
$('#txtName').val(data.d.name);
$('#txtAddress').val(data.d.address);
$('#txtbasicSalary').val(data.d.basicSalary);
},
error: function (err) {
alert(empid);
}
});
});
});
<%--
$(document).ready(function () {
$('#<%= btnGetEmployee.ClientID %>').click(function () {
alert("Handler for .click() called.");
});
});
--%>
ID:
<%--<asp:Button ID="btnGetEmployee" runat="server" Text="Button" />--%>
name:
Address:
Basic Salary: