Reputation: 487
hi friends I have been working WCF since past one week. I have written some samples, it contains 2 layers one is business layer and a DAL layer. I want to send list and want to return the list.
Here is the code check out correct the mistakes.
IEmployee.cs
[OperationContract]
List<outEmployee> CreateEmployeeDetails(inemployee InsertEmployee);
[DataContract]
public class inemployee
{
public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
}
public class outEmailTemplate
{
public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
}
Employee.svc.cs
public List<outEmployee> CreateEmployeeDetails(inemployee InsertEmployee)
{
EmployeeDac emp = new EmployeeDac();
return emp.InsertEmployeeDetails(InsertEmployee);
}
EmployeeDac.cs
public List<outEmployee> InsertEmailTemplate(inEmployee InsertEmp)
{
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd;
cmd = new SqlCommand("SP_InsertEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("employeename", SqlDbType.VarChar).Value = InsertEmp.Employeename;
con.Open();
string id = (string)cmd.ExecuteScalar();
con.Close();
List<inemployee> oet = new List<inemployee>();
oet.Add(id);
return oet;
}
Can anyone please correct the mistakes and rewrite the code.
Here I want to return the list to ui (silverlight).
Upvotes: 0
Views: 1290
Reputation: 754258
Several problems:
1) your data contracts are incomplete - you need to first of all decorate all classes used in your service methods (for input and output parameters!) with [DataContract]
, and you should also decorate all members to be serialized with [DataMember]
:
[DataContract]
public class inemployee
{
[DataMember]
public string EmployeeID { get; set; }
[DataMember]
public string EmployeeName { get; set; }
}
[DataContract]
public class outemployee
{
[DataMember]
public string EmployeeID { get; set; }
[DataMember]
public string EmployeeName { get; set; }
}
BTW: why do you have two separate types - inemployee
and outemployee
?? Wouldn't just one type Employee
be sufficient??
2) Your DAL layer should embrace best practices: wrap your SqlConnection
and SqlCommand
into using blocks; for your VARCHAR
parameter, always define a length! and most of all: if you want to return a List<outemployee>
, you need to create a list of outemployee
- not of inemployee
!
And to finish off: you're getting back an id
of type string
and you're trying to add that string
to a List<outemployee>
- that will never work of course! Given the id
, you need to create an instance of outemployee
and add that to the list.
public List<outEmployee> InsertEmailTemplate(inEmployee InsertEmp)
{
using(SqlConnection con = new SqlConnection(connectionstring))
using(SqlCommand cmd = new SqlCommand("SP_InsertEmployee", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("employeename", SqlDbType.VarChar, 100).Value = InsertEmp.Employeename;
con.Open();
string id = (string)cmd.ExecuteScalar();
con.Close();
}
// you want to return outemployee -
// so you need to create an outemployee!
List<outemployee> oet = new List<outemployee>();
// create a new instance of an "outemployee"
outemployee emp = new outemployee();
// set the EmployeeID property of the "outemployee" to "id"
emp.EmployeeID = id;
// add new "outemployee" to list
oet.Add(emp);
return oet;
}
Upvotes: 1