2017-04-13 5 views
0

を経由して、私はCrystalレポートもアップへの日付でVS 2015、SQL Serverの2012Crystalレポートのデータソースを取り込み、オブジェクトとExportToStream

を使用しています私はのviewmodelクラスを経由してCrystalレポートのデータソースを移入し、それをエクスポートしたいです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"); 

私はエラー

データ・ソース・ログインが

失敗得る助けてください

私は、データベースへのレポートを接続していないですし、また、私は次の2つのデータソースを設定しようとしていることを仮定してい

答えて

1

助けてください.NETオブジェクト

を使用して設定レポートを持っています。 関連するデータテーブルに設定する必要があります。 あなたは以下のようなものを試すことができます。

私はこのエラーが発生しました
rptH.Database.Tables[0].SetDataSource(querylist); 
rptH.Database.Tables[1].SetDataSource(showInvoiceDetails); 
1

、 まず、あなたがたDataTableが異なるテーブルのフィールドを含むカスタムを作成する必要があります。

Image

は、あなたが .ToDatableが

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

CrystalレポートがスローするシステムNULL可能エラーを回避するにはReportHelperからである。このコードを追加する必要があり、次のことがあなたのリストのクエリを変換する必要があります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); 
     } 
    } 

} 
関連する問題