Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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

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();
            }
        }
    }
    
}


and this is my DataSourceResult

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; }
}


where is the error and how can i solve the problem ?
Posted
Comments
George Jonsson 3-Jul-14 6:16am    
Maybe this can help
http://www.codeproject.com/Articles/14491/Serialize-and-Deserialize-IEnumerable-Objects

1 solution

1.Your problem is generated by the fact that you are trying to use IEnumerable<employee> </employee>as parameter and also as return type into your Web Methods. By default only the system primitive types and arrays of these primitive types (like int, float, double, string) can be used.

2.If you want to use your own classes as parameters you have to define them as complex types by using XML definition. Here is a MSDN link about this.[^]

3.I strongly advice you, if is posible, not to use Web Service anymore, but to use WCF in place of them (Windows Communication Foundation), because WCF is a new technology and is more configurable and easy to use for all type of communications between applications.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900