pass a value from winform to crystal report text object through c#

2.7k Views Asked by At

I'm a newbie in C# programming and also Crystal Report. I face a problem to send a value that entered by user from WinForm to be displayed at Crystal Report through C# (VS10). I only need the input value to be displayed as a text object in the Report Header. I already looked for solutions but still I don't know what's wrong in my code.

I created a crystal report connected to its viewer. Here is my WinForm code

private void btnSubmit_Click(object sender, EventArgs e)
{
     crRpt TI = new crRpt();            
     CrystalReportViewer crv = new CrystalReportViewer();

     TextObject tiNo = (TextObject)TI.ReportDefinition.Sections["Section2"].ReportObjects["TIN"];
     tiNo.Text = txtTI.Text.toString();
     crv.Visible = false;            
     crv.ReportSource = TI;
     ShowDialog(crv);                     
}

it returned an error says:

Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog.

Even though I already put -> crv.visible = false.

I saw from the Crystal Report Viewer, WinForm generated a code automatically to display the report that connected to an excel file as its datasource. If I add 2 lines code into the bottom of code like below :

displayCR_form dispCR = new displayCR_form();
dispCR.ShowDialog();

and remark -> ShowDialog(crv);

The report will appear but still with the blank text object that I already assigned. What did I miss? Please help. Thank you.

1

There are 1 best solutions below

0
On
private void btnSubmit_Click(object sender, EventArgs e)
{
     crRpt TI = new crRpt();            
     CrystalReportViewer crv = new CrystalReportViewer();
     Form frmCrViewer = new Form();
     frmCrViewer.Controls.Add(crv);

     TextObject tiNo = (TextObject)TI.ReportDefinition.Sections["Section2"].ReportObjects["TIN"];
     tiNo.Text = txtTI.Text.toString();
     crv.ReportSource = TI;
     crv.Dock = Fill;
     frmCrViewer.ShowDialog();
}