0
Reply

Cannot serialize interface System.Collections.Generic.IEnume

Ask a question
alaa

alaa

10 years ago
966
1
hi every body i have astrange problem i never faced before i have searched more on web but nosense
this is my error :
Cannot serialize interface System.Collections.Generic.IEnumerable`1[[Employee, App_Code.6ohe-rkb, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].

my web services code is

<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Data;
using System.Data.SqlClient;
using System.Text;

/// <summary>
/// Summary description for EmployeeService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class EmployeeService : System.Web.Services.WebService {

List<Employee> Employees = new List<Employee>();


public EmployeeService()
{

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public DataSourceResult Read()
{
SqlDataReader reader = null;
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("Select * from Employee", con))
{
try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
Employee e = new Employee();
e.Id = (int)reader["Id"];
e.FirstName = (string)reader["FirstName"];
e.LastName = (string)reader["LastName"];
e.Title = (string)reader["Title"];
e.Salary = (decimal)reader["Salary"];
Employees.Add(e);


}
}
finally
{
if (reader != null)
reader.Close();
}
}
}

return new DataSourceResult
{
Total = Employees.Count, //number of records
Data = Employees //the data
};
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public IEnumerable<Employee> Create(IEnumerable<Employee> newEmployees)
{
string stmt = string.Empty;
StringBuilder ids = new StringBuilder();
SqlDataReader reader = null;
List<Employee> retrievedEmployees = new List<Employee>();
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("InsertEmployee", con))
{
con.Open();
foreach (Employee e in newEmployees)
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", e.FirstName);
cmd.Parameters.AddWithValue("@LastName", e.LastName);
cmd.Parameters.AddWithValue("@Title", e.Title);
cmd.Parameters.AddWithValue("@Salary", Convert.ToDecimal(e.Salary));
SqlParameter retValue = cmd.Parameters.Add("@NewId", SqlDbType.Int);
retValue.Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
ids.Append(Convert.ToInt16(retValue.Value)).Append(",");
}
//remove the last comma
ids.Remove(ids.Length - 1, 1);
}

using (SqlCommand cmd = new SqlCommand(
"Select * from Employee where id in (" + ids + ")", con))
{
try
{
reader = cmd.ExecuteReader();
while (reader.Read())
{
Employee e = new Employee();
e.Id = (int)reader["Id"];
e.FirstName = (string)reader["FirstName"];
e.LastName = (string)reader["LastName"];
e.Title = (string)reader["Title"];
e.Salary = (decimal)reader["Salary"];
retrievedEmployees.Add(e);
}
}
finally
{
if (reader != null)
reader.Close();
}
}
}
return retrievedEmployees;
}

[WebMethod]
public void Update(IEnumerable<Employee> editEmployees)
{
string stmt = string.Empty;
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(stmt, con))
{
foreach (Employee e in editEmployees)
{
stmt = "update Employee set FirstName = '" + e.FirstName +
"', LastName = '" + e.LastName +
"', Title = '" + e.Title + "', Salary = " +
e.Salary + "where Id = " + e.Id;
con.Open();
cmd.ExecuteNonQuery();
}
}
}
}

[WebMethod]
public void Destroy(IEnumerable<Employee> deleteEmployees)
{
StringBuilder ids = new StringBuilder();
foreach (Employee e in deleteEmployees)
{
ids.Append(e.Id).Append(",");
}
//remove the last comma
ids.Remove(ids.Length - 1, 1);

string stmt = "delete from Employee where id in (" + ids + ")";
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(stmt, con))
{
con.Open();
cmd.ExecuteNonQuery();
}
}
}

}
</pre>

and this is my DataSourceResult

<pre lang="c#">
using System.Collections.Generic;

/// <summary>
/// Describes the result of Kendo DataSource read operation.
/// </summary>
public class DataSourceResult
{
/// <summary>
/// Represents a single page of processed data.
/// </summary>
public List<Employee> Data { get; set; }

/// <summary>
/// The total number of records available.
/// </summary>
public int Total { get; set; }
}
</pre>

where is the error and how can i solve the problem ?