2017-04-13 52 views
0
我使用VS 2015年

,水晶報表也上到今天爲止,SQL Server 2012的通過對象填充水晶報表數據源和ExportToStream

我想通過一個視圖模型類填充水晶報表數據源並將其導出到PDF

ReportDocument rptH = new ReportDocument(); 
rptH.FileName = Server.MapPath("~/reports/InvoiceReportsSample1.rpt"); 

var showInvoices = 
       (from ids in context.Invoices 
       where ids.InvoiceNumber == "01-04-2017-1" 
       select new Invoice_Report_ViewModel() 
       { 
        Invoiceid = ids.Invoiceid, 
        InvoiceNumber = ids.InvoiceNumber, 
        CustomerCompanyName = ids.CustomerCompanyName, 
        FirstBlankSpaceForPanel1 = ids.FirstBlankSpaceForPanel1, 
        MainDiscount = ids.MainDiscount, 
        MainTaxes = ids.MainTaxes, 
        MainTotal = ids.MainTotal, 
        TypeOfPortals = ids.TypeOfPortals, 
        TypeOfTickets = ids.TypeOfTickets, 
        SecondBlankSpaceForPanel2 = ids.SecondBlankSpaceForPanel2, 
       }).First(); 

     var showInvoiceDetails = (from ids in context.InvoiceDetailses 
            where 
             ids.Invoiceid == showInvoices.Invoiceid 
            select new InvoiceDetails_Report_ViewModel() 
            { 

            }).ToList(); 
     var querylist = new List<Invoice_Report_ViewModel> { showInvoices }; 
     rptH.Database.Tables[0].SetDataSource(querylist); 
     rptH.Database.Tables[0].SetDataSource(showInvoiceDetails); 
     rptH.Refresh();   
     Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); 
     return File(stream, "application/pdf"); 

請幫助我得到

數據源登錄失敗

錯誤

但我沒有連接到數據庫的報告,也是我使用.NET對象

請幫

回答

1

我假設你正在嘗試設置兩個數據源具有配置報告。 您需要將它們設置爲相關的數據表。 你可以嘗試下面的東西。

rptH.Database.Tables[0].SetDataSource(querylist); 
rptH.Database.Tables[1].SetDataSource(showInvoiceDetails); 
1

我遇到這個錯誤, 首先,你需要創建一個自定義數據表中包含不同的表中的字段。

Image

那麼你需要添加此代碼 .ToDatable從ReportHelper

//domResult -> List of your View Model 
    DataTable dt = ReportHelper.ToDataTable(domResult); 
    // LCDraft_Domestic--> Crystal Report 
    LCDraft_Domestic rpt = new LCDraft_Domestic(); 
    //My Data Table 
     rpt.Database.Tables["DraftData"].SetDataSource(dt); 

爲了避免系統可爲空的錯誤水晶報表拋出,你需要到您的列表查詢轉換DataTable

創建靜態類名ReportHelper

public static class ReportHelper 
{ 



    public static DataTable ToDataTable<T>(this IList<T> items) 
    { 
     var tb = new DataTable(typeof(T).Name); 

     PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 

     foreach (PropertyInfo prop in props) 
     { 
      Type t = GetCoreType(prop.PropertyType); 
      tb.Columns.Add(prop.Name, t); 
     } 

     foreach (T item in items) 
     { 
      var values = new object[props.Length]; 

      for (int i = 0; i < props.Length; i++) 
      { 
       values[i] = props[i].GetValue(item, null); 
      } 

      tb.Rows.Add(values); 
     } 

     return tb; 
    } 

    /// <summary> 
    /// Determine of specified type is nullable 
    /// </summary> 
    public static bool IsNullable(Type type) 
    { 
     return !type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)); 
    } 

    /// <summary> 
    /// Return underlying type if type is Nullable otherwise return the type 
    /// </summary> 
    public static Type GetCoreType(Type type) 
    { 
     if (type != null && IsNullable(type)) 
     { 
      if (!type.IsValueType) 
      { 
       return type; 
      } 
      else 
      { 
       return Nullable.GetUnderlyingType(type); 
      } 
     } 
     else 
     { 
      return type; 
     } 
    } 
    static TableLogOnInfo crTableLogonInfo; 
    static ConnectionInfo crConnectionInfo; 
    static Tables crTables; 
    static Database crDatabase; 

    public static void ReportLogin(ReportDocument crDoc, string Server, string Database, string UserID, string Password) 
    { 
     crConnectionInfo = new ConnectionInfo(); 
     crConnectionInfo.ServerName = Server; 
     crConnectionInfo.DatabaseName = Database; 
     crConnectionInfo.UserID = UserID; 
     crConnectionInfo.Password = Password; 
     crDatabase = crDoc.Database; 
     crTables = crDatabase.Tables; 
     foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables) 
     { 
      crTableLogonInfo = crTable.LogOnInfo; 
      crTableLogonInfo.ConnectionInfo = crConnectionInfo; 
      crTable.ApplyLogOnInfo(crTableLogonInfo); 
     } 
    } 
    //No Login 
    public static void ReportLogin(ReportDocument crDoc, string Server, string Database) 
    { 
     crConnectionInfo = new ConnectionInfo(); 
     crConnectionInfo.ServerName = Server; 
     crConnectionInfo.DatabaseName = Database; 
     crConnectionInfo.IntegratedSecurity = true; 
     crDatabase = crDoc.Database; 
     crTables = crDatabase.Tables; 
     foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables) 
     { 
      crTableLogonInfo = crTable.LogOnInfo; 
      crTableLogonInfo.ConnectionInfo = crConnectionInfo; 
      crTable.ApplyLogOnInfo(crTableLogonInfo); 
     } 
    } 

}