Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
crystalReportViewer_purchase1.ReportSource = RptDocCrt shows null value.

and then While generating a report, i have got a error "object reference is not set to an instance of an object."

What I have tried:

public Frm_Purchase_Report(DataSet ds, string p)
       {
           // TODO: Complete member initialization
           this.ds = ds;
           this.p = p;

           try
           {
               string s = ds.Tables[0].Rows.Count.ToString();
               if (s != "0")
               {
                   string AppPath = Application.StartupPath;
                   DataTable dt = new DataTable();
                   AppPath += "\\Report\\genrate_Purchase_Report1.rpt";
                   RptDocCrt.Load(@"" + AppPath);

                   dt = ds.Tables[0];
                   RptDocCrt.SetDataSource(dt);
                   crystalReportViewer_purchase1.ReportSource = RptDocCrt;
                   crystalReportViewer_purchase1.Refresh();

               }
               else
               {
                   MessageBox.Show("Record Not Found..!!!");
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
Posted
Updated 17-Apr-18 11:44am
Comments
Richard MacCutchan 17-Apr-18 8:47am    
You need to find out why that value is null, and correct it.

1 solution

The problem is the path to the Report file. RptDocCrt.Load is unable to locate the file in the provided path.

Application.StartUpPath is dependent on how you are starting the application.
For instance, when debugging the StartUp path will be;
<Path to Project>\bin\<Debug or Release>\Executable

<Debug or Release> is dependent on the configuration properties of the project.

To ensure the report file can be accessed you would need the report files to be copied to the correct location, or put the reports in an alternate location and hard-code the path to the files.

NOTE: If the reports are part of the Solution you can set the report files to be copied to the output location

Kind Regards
 
Share this answer
 
Comments
[no name] 18-Apr-18 2:41am    
I tried, RptDocCrt.Load(@"F:\Newfolder\Hotel_Management\Hotel_Management\bin\Debug\ Report\genrate_Purchase_Report1.rpt");

and also,
RptDocCrt.Load(@"F:\New folder\Hotel_Management\Hotel_Management\ genrate_Purchase_Report1.rpt");


but same error thrown on crystalReportViewer_purchase1.ReportSource = RptDocCrt;
this line "object reference is not set to an instance of an object."


crystalReportViewer_purchase1 <-- showing null value..
an0ther1 18-Apr-18 17:18pm    
Are you sure the path is correct & you have access to it?
For instance, what happens if you use System.IO.File.Exists("<path and file name>")? You may have a permissions issue - try running Visual Studio as an Admin..

What happens if you move the report to a directory such as C:\Temp\Report.rpt?

Kind Regards
[no name] 19-Apr-18 1:38am    
Another Reports are from same path that are running, but problem is to only for that one.
[no name] 19-Apr-18 9:30am    
data is also present in ds(dataset object).
an0ther1 19-Apr-18 17:14pm    
Then the problem is within the report itself.
Reports are very difficult to troubleshoot when you supply a dataset as the source - you will need to try ruling things out - remove all formulas from the report - just comment them out is a good start - then does the report run?
If not, start looking at grouping

Kind Regards

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