本文介绍了无法序列化System.Collections.Generic.IEnumerable接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的每个身体都有我以前从未遇到过的奇怪问题,我在网络上搜索了更多但又毫无意义的
这是我的错误:
无法序列化接口System.Collections.Generic.IEnumerable`1 [[Employee,App_Code.6ohe-rkb,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null]].

我的网络服务代码是

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

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

}



这是我的DataSourceResult



and this is my DataSourceResult

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 ?

推荐答案


这篇关于无法序列化System.Collections.Generic.IEnumerable接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:14