水晶报表报错:数据源对象无效

问题描述 投票:0回答:2

我正在尝试使用水晶报表打印学生证,但我所能得到的只是弹出此错误

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

就是这样,这里的一切都工作正常并且正在很好地获取数据但是当我将它发送到我的控制器中生成身份证的方法时,我收到了该错误消息

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

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

我真的很感谢你的帮助谢谢...

crystal-reports asp.net-mvc-5 c#-5.0
2个回答
1
投票

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


0
投票

请确保您设置了 NOCOUNT ON

© www.soinside.com 2019 - 2024. All rights reserved.