Crystal Report错误:数据源对象无效

时间:2015-04-27 11:11:59

标签: crystal-reports asp.net-mvc-5 c#-5.0

我正在尝试使用水晶报告打印学生身份证,但我能得到的是这个错误弹出The data source object is invalid.

如果我有任何错误,请帮我查一下这段代码......

这是模型

public class CardModel
{
        // Properties
        public string Department { get; set; }

        public string ExpiryDate { get; set; }

        public string FirstName { get; set; }

        public Sex Gender { get; set; }

        public Guid Id { get; set; }

        public string MiddleName { get; set; }

        public string RegistrationNo { get; set; }

        public byte[] SecuritySign { get; set; }

        public byte[] StudentPhoto { get; set; }
        public string Surname { get; set; }
}

public static class CardModelExtention
{
    public static CardModel ToCardModel(this Student identity)
    {
        return new CardModel
        {
            Id = identity.Id,
            FirstName = identity.FirstName,
            MiddleName = identity.MiddleName,
            Surname = identity.Surname,
            StudentPhoto = identity.Photo.RawPhoto,
            SecuritySign = identity.SecuritySignature.RawSignature,
            Gender = identity.Sex,
            ExpiryDate = identity.ExpiryDate,
            Department = identity.Department.DepartmentName,
            RegistrationNo = identity.RegistrationNo
        };
    }
}

这是我用来从数据库中提取信息的服务

public class StudentService : IStudentService
{
    ERMUoW _ow;
    public StudentService()
    {
        _ow = new ERMUoW();
    }

    public CardModel GetStudentById(Guid id)
    {
        CardModel obj3 = new CardModel();

            Student student  = _ow.Students.GetAllIncluding(new Expression<Func<Student, object>>[] { st => st.Photo, st => st.Signature, st => st.SecuritySignature, st => st.Department }).Where(x => x.Id == id).SingleOrDefault();

            var cardInfo = student.ToCardModel();

        return cardInfo;
    }
}

public interface IStudentService
{
    CardModel GetStudentById(Guid id);
}

这就是它,这里的一切都运行正常,我得到的数据非常好但是当我把它发送到我的控制器生成身份证的方法时,我得到了错误信息

这是使用crytal报告生成卡片的代码

public ActionResult PrintCard(Guid id)
    {
        var student = _studentCardService.GetStudentById(id);

        ReportDocument read = new ReportDocument();

        read.Load(Server.MapPath("~/Reports/rpt_StudentCard.rpt"));

        read.SetDataSource(student);
        Response.Buffer = false;
        Response.ClearContent();
        Response.ClearHeaders();

        try
        {
            Stream stream = read.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            stream.Seek(0, SeekOrigin.Begin);
            return File(stream, "application/pdf", "StudentIdentityCard.pdf");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

我会非常感谢您的帮助,谢谢......

1 个答案:

答案 0 :(得分:1)

数据源必须是元素列表...而不是单个元素。