Click here to Skip to main content
15,898,728 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear honors,

I'm developing one windows application. In that project I've some crystal reports.

When I click the print button the report should print automatically.

The following code works fine in my local system. If I run my in project another system it gets error.

So I need your suggestion, how to get the dynamic path for load Report Document?.

Here my code:
C#
public void PrintMethod()
      {
          lsInvNo = frmCustomerInv.gsInvNo;
          string query2 = "select InvNo from tblCustomerInvoice where InvNo='" + lsInvNo + "' and State=''";
          string InvoNu = DataAccess.GetSingleValue(query2);
          if (InvoNu == "")
          {
              lsInvNo = "";
              MessageBox.Show("This Invoice Number Not in Customer Invoice List!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
              Query = "select distinct p.InvNo,p.InvDate ,p.CustomerName,p.TinNo,p.Sno,p.vehicleNo,p.ProductName,p.ProductKg,p.Packing,p.Units,p.Rate,p.Amount,p.Address,a.Mobile,a.CSTNO,c.NetAmt,c.Tax,c.TotAmt,w.Words  from tblCustomerInvoice as p inner join tblCustomerDetails as a on p.CustomerName=a.CustomerName inner join tblcustomerInvoiceAmt as c on c.InvNo=p.InvNo inner join tblCustomerInvWords as w on p.InvNo=w.InvNo where p.InvNo='" + lsInvNo + "'order by Sno";

          }
          else
          {
              SqlCon = dbCon.DBOpenConnection();
              Query = "select distinct p.InvNo,p.InvDate ,p.CustomerName,p.TinNo,p.Sno,p.vehicleNo,p.ProductName,p.ProductKg,p.Packing,p.Units,p.Rate,p.Amount,p.Address,a.Mobile,a.CSTNO,c.NetAmt,c.Tax,c.TotAmt,w.Words  from tblCustomerInvoice as p inner join tblCustomerDetails as a on p.CustomerName=a.CustomerName inner join tblcustomerInvoiceAmt as c on c.InvNo=p.InvNo inner join tblCustomerInvWords as w on p.InvNo=w.InvNo where p.InvNo='" + lsInvNo + "'order by Sno";
          }
          SqlDa = new SqlDataAdapter(Query, SqlCon);
          DataTable dt = new DataTable();
          dt.Clear();
          SqlDa.Fill(dt);
          ReportDocument REportDoct = new ReportDocument();
          REportDoct.Load(@"D:\vsp\vsp\Project\Reports\FinalInvoice.rpt");
          REportDoct.SetDataSource(dt);
          crystalReportViewer1.ReportSource = REportDoct;
          crystalReportViewer1.Refresh();
          REportDoct.PrintToPrinter(1, false, 0, 0);

      }



Thanks in Advance..
Posted
Updated 24-Nov-11 2:40am
v2

I think this is Not because of Report Path, this is becase of DataBase.When we Run project on other System it Ask Path.I was also got the same error when i was developing My First Project.This happens
because of Path of DataBase Change When you run yor project on other System.
The solution on that is you have to Create DataSet Which create permanent Structure of DataBase which is fixed whether you run your project on your System or Anywere else.

I used the following code which solve this problem.
VB
Public Class FormReport
 'Creating New Objects
 Dim DataSet As New Data.DataSet
 Dim DataView As New Data.DataView
 Dim report As New ReportName 'ReportName is the Name of the Report Display in SolutionExplorer
 Dim objda As New OledbDataAdapter

Private Sub FormReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OpenConnection()'To Open DataBase Connection
        DataSet.Clear()
        objda.SelectCommand.CommandText = "SELECT * FROM TabelName"
        Try
            objda.SelectCommand.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)'Display Error if Any
        End Try
        objda.Fill(DataSet, "TabelName")
        DataView.Table = DataSet.Tables("TabelName")
        report.SetDataSource(DataView)
        'Clearing ReportViewer Source
        CrystalReportViewer1.ReportSource = ""
        'Set Report to ReportViewer
        CrystalReportViewer1.ReportSource = report
End Sub
End Class
 
Share this answer
 
v2
Already answered a similar question here before, check it.
[Solved] How to include RPT file path in C# apps with crystal report[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900