Can someone please show me how to correct this Crystal Report code?

H

Henry

I am trying to write an application that allows me to dynamically load
selected Crystal Report files, read the parameters from the report file,
give the user a chance to enter relevant data in a user friendly manner and
then run the report in a report viewer.

I can find bits and pieces of the instructions for doing this, but I don't
seem to be able to put it all together. For now I have some code that will
show a file Open dialog and let me select the report file. The next step I
believe is to load that file into a Report document. Once the document is
loaded, then I should be able to access its parameterFieldInfo. Here is
code I have cobbled together so far:
// I open the dialog by clicking a command button
private void button1_Click(object sender, System.EventArgs e)

{

// Displays an OpenFileDialog so the user can select a Cursor.

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.Filter = "Report Files (*.rpt)|*.rpt";

openFileDialog1.Title = "Select a Report File";

// Show the Dialog.

// If the user clicked OK in the dialog and a .RPT file was selected, then
open it.

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

// I thought the next line of code would load the file into the Report
Dcoument, but I get the error below.

//An unhandled exception of type 'System.NullReferenceException' occurred in
ReportManager.exe

ReportDocument1.Load(openFileDialog1.FileName);

//I am not quite sure how to access the values in the parameter fields They
are part of the Crystal Report Engine DataDefinition object, and so is the
Report document, but I don't understand the link..

for( int i =0 ;i< dataDef1.ParameterFields.Count;i++)

{

listBox1.Items.Add(dataDef1.ParameterFields.Current.ToString());

dataDef1.ParameterFields.MoveNext();

}

}

}
 
M

Mohamoss

Hi
look at this sample in this link that describes How to create a Dynamic
report using C#. I would say this could be all what you need to know
<http://support.crystaldecisions.com/communityCS/FilesAndUpdates/csharp_win_
dynamic_report_formula.exe.asp>
If you are using The Microsoft SQL server Database these also are very good
samples

<http://support.crystaldecisions.com/communityCS/FilesAndUpdates/csharp_web_
simplelogonviewer.exe.asp>
<http://support.crystaldecisions.com/communityCS/FilesAndUpdates/csharp_win_
dbengine.exe.asp>
Mohamed M .Mahfouz
Developer Support Engineer
ITWorx on behalf of Microsoft EMEA GTSC
 
B

Bruce Wood

Please, when you post code to newsgroups, try to make it as complete as
possible.

Somewhere in your code you declare:

ReportDocument ReportDocument1.

apparently you are missing the following:

ReportDocument1 = new ReportDocument();

(I can't really tell because you didn't post the declaration.) After
you've done this, then you can say:

ReportDocument1.Load(openFileD­ialog1.FileName);

and it should work.

As for the parameter definitions, you should be able to say:

foreach (ParameterFieldDefinition parm in
ReportDocument1.DataDefinition.ParameterFields)
{
listBox1.Items.Add(parm.Name);
// ...or do whatever you like with the parameter here...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top