【发布时间】:2018-10-25 16:01:41
【问题描述】:
我正在尝试使用水晶报告打印学生证,但我得到的只是弹出此错误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;
}
}
非常感谢您的帮助,谢谢...
【问题讨论】:
-
尝试获取
List<CarModel>的列表而不是 1 个 CarModel 实体并处理 rpt 文件中的项目。 see this -
好的,那是有效的,那么我怎样才能让一个项目显示......我该如何处理你所说的?
-
处理您的报告没有什么特别之处,只是正常工作。在您的
report details section(s)上拖放您想要的字段/公式字段/参数,按照您需要的方式将它们排列为列表布局、卡片布局,您可以根据您的输入或标准点击预览以查看示例结果。
标签: crystal-reports asp.net-mvc-5 c#-5.0