I am following a tutorial and trying to pass some data in json format. I have tried to overload the constructor thinking it will display different data but this is not the case.
On both cases i get the same output.
this is my interface:
namespace JsonWcfService
{
[ServiceContract]
public interface IGetJson
{
// display user`s department
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/userDepartment/{name}")]
List<Departments> userDepartments(string name);
// display user`s app
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/userApp/{name}")]
List<Departments> userApp(string name);
}
}
this is my class:
namespace JsonWcfService
{
public class GetJson : IGetJson
{
//display user`s departments
public List<Departments> userDepartments(string name)
{
List<Departments> listUserDepartments = new List<Departments>();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tabletConnection"].ToString()))
{
conn.Open();
string cmdStr = String.Format("SELECT users.userName, users.departmentID, department.departmentName, users.isActive FROM users,department "
+ "WHERE users.departmentID = department.departmentID "
+ "AND userName = '" + name +"'");
SqlCommand cmd = new SqlCommand(cmdStr, conn);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
listUserDepartments.Add(new Departments(rd.GetString(0), rd.GetInt32(1), rd.GetString(2), rd.GetString(3)));
}
conn.Close();
}
return listUserDepartments;
}
//display user`s app
public List<Departments> userApp(string name)
{
List<Departments> listUserApp = new List<Departments>();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tabletConnection"].ToString()))
{
conn.Open();
string cmdStr = String.Format("SELECT * FROM application");
SqlCommand cmd = new SqlCommand(cmdStr, conn);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
listUserApp.Add(new Departments(rd.GetInt32(0), rd.GetString(1)));
}
conn.Close();
}
return listUserApp;
}
}
[DataContract]
public class Departments
{
[DataMember]
public int departmentId { get; set; }
[DataMember]
public string departmentName { get; set; }
[DataMember]
public string userName { get; set; }
[DataMember]
public string isActive { get; set; }
public Departments(int temp_departmentId, string temp_departmentName)
{
departmentId = temp_departmentId;
departmentName = temp_departmentName;
}
public Departments(string temp_userName, int temp_departmentId, string temp_departmentName, string temp_isActive)
{
userName = temp_userName;
departmentId = temp_departmentId;
departmentName = temp_departmentName;
isActive = temp_isActive;
}
}
}
output for constructor 1:
{"userAppResult":[{"departmentId":1,"departmentName":"A","isActive":null,"userName":null},{"departmentId":2,"departmentName":"A","isActive":null,"userName":null},{"departmentId":3,"departmentName":"A","isActive":null,"userName":null},{"departmentId":4,"departmentName":"A","isActive":null,"userName":null},{"departmentId":5,"departmentName":"A","isActive":null,"userName":null}]}
output for constructor 2:
{"userDepartmentsResult":[{"departmentId":1,"departmentName":"A","isActive":"Y","userName":"b"},{"departmentId":2,"departmentName":"A","isActive":"Y","userName":"b"},{"departmentId":3,"departmentName":"A","isActive":"Y","userName":"b"}]}