Change XML data source in Crystal Report

I created Crystal Report and linked it to an XML file in C:\SomeDir\Data.xml.

At runtime, I may need to put the data in C:\SomeOtherDir\Data.xml.

The code that I still have is as follows:

ReportDocument report = new ReportDocument();
report.Load("Report.rpt");
PrinterSettings printerSettings = new PrinterSettings();
PageSettings pageSettings = printerSettings.DefaultPageSettings;
report.PrintToPrinter(printerSettings, pageSettings, false);

This will print a report with the data in C:\SomeDir\Data.xml. I want him to print the data in C:\SomeOtherDir\Data.xml.

How can i do this?

+5
source share
4 answers
ReportDocument report = new ReportDocument();
report.Load("Report.rpt");

DataSet reportData = new DataSet();
reportData.ReadXml(@"C:\SomeOtherDir\Data.xml");
report.SetDataSource(reportData);

PrinterSettings printerSettings = new PrinterSettings();
PageSettings pageSettings = printerSettings.DefaultPageSettings;
report.PrintToPrinter(printerSettings, pageSettings, false);

If the XML schema changes, you will need to open the report in the CR editor and “check the database” to update the schema to which it is bound, or it will cause the mysterious error “Login failed”.

+1
source

Java, , , , ( db xml - ) rpt.Database.Tables. setDataSource " " , CR , . , #. , , , .

0
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports BL

Public Class frmRptViewer
    Dim strReportName As String
    Dim ds As New DataSet
    Public bl As New BL.InvoiceBL
    Private Sub configureCrystalReports()
        Dim strReportName As String
        Try
            strReportName = "Inv"
            ds = bl.FillHDDT(TrnCode)
            Dim strReportPath As String = Application.StartupPath & "\Reports\" & strReportName & ".rpt"
            Dim rptDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
            ds.WriteXml(Application.StartupPath & "\xmlFiles\INVOICE.xml", XmlWriteMode.WriteSchema)
            rptDocument.Load(strReportPath)

            Dim crpConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo
            With crpConnectionInfo
                .ServerName = Application.StartupPath & "\xmlFiles\INVOICE.xml"
                .DatabaseName = "NewDataset"
                .UserID = ""
                .Password = ""
            End With

            Dim tblCurrent As Table
            Dim crpTableLogOnInfo As New CrystalDecisions.Shared.TableLogOnInfo()
            For Each tblCurrent In rptDocument.Database.Tables
                tblCurrent.LogOnInfo.ConnectionInfo.ServerName = Application.StartupPath & "\xmlFiles\INVOICE.xml"
                tblCurrent.LogOnInfo.ConnectionInfo.DatabaseName = "NewDataset"
                tblCurrent.LogOnInfo.ConnectionInfo.UserID = ""
                tblCurrent.LogOnInfo.ConnectionInfo.Password = ""
                tblCurrent.LogOnInfo.ConnectionInfo.Type = CrystalDecisions.Shared.ConnectionInfoType.MetaData
            Next
            rptDocument.Database.Tables(0).SetDataSource(ds.Tables("Table"))
            rptDocument.Database.Tables(1).SetDataSource(ds.Tables("Table1"))

            crptViewer.ShowRefreshButton = False
            crptViewer.ShowCloseButton = False
            crptViewer.ShowGroupTreeButton = False
            crptViewer.ReportSource = rptDocument
        Catch ex As Exception
        End Try
    End Sub
0

, DataGridView Crystalelportport XML

**(This is very helpful if you not using any database)**
  • ( DataGridview)
  • XML

 DataTable dt = new DataTable();
 dt.Columns.Add("Item_Id", typeof(string));
 dt.Columns.Add("Categry", typeof(string));
 dt.Columns.Add("Item_Name", typeof(string));
 dt.Columns.Add("Unit_Price", typeof(double));
 dt.Columns.Add("Quantity", typeof(int));
 dt.Columns.Add("Discount", typeof(string));
 dt.Columns.Add("Total_Payable", typeof(double));
  foreach (DataGridViewRow dgr in DGVsell.Rows) 
 {
   dt.Rows.Add(dgr.Cells[0].Value, dgr.Cells[1].Value, dgr.Cells[2].Value, dgr.Cells[3].Value, dgr.Cells[4].Value, dgr.Cells[5].Value, dgr.Cells[6].Value);
 }
 ds.Tables.Add(dt);
 ds.WriteXmlSchema("Bill.xml");

note Xml App.config,

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  </startup>-->
    <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

xml Crystalel

 frmreport obj = new frmreport(); //load report viwer form
 obj.ShowDialog();

viwer

crBill cr = new crBill();
cr.SetDataSource(frmSell.ds);
crystalReportViewer1.ReportSource = cr;
crystalReportViewer1.RefreshReport();
crystalReportViewer1.Refresh();
0

All Articles