Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone. Could someone please help with my problem. I already have the codes for this but I can't seem to combine the two.
Legend: dt1 = Dataset
crpt = Crystal Report File

This is my code for loading the database values into the Crystal Report filtered with the value of the txtGender textbox:
C#
SqlConnection conn = conString.getCon();
ReportDocument cy = new ReportDocument();
dt1 ds = new dt1();
conn.Open();
cy.Load(Application.StartupPath + @"\crpt.rpt");



SqlDataAdapter da = new SqlDataAdapter("exec viewInfo @gen", conn);
da.SelectCommand.Parameters.AddWithValue("@gen", txtGender.Text);


da.Fill(ds.Info);
cy.SetDataSource(ds);

crystalReportViewer1.ReportSource = cy;

conn.Close();


While this is my code for Passing the textbox txtGender value into the Crystal Report Parameter object named "Gen":

C#
ParameterFields pField = new ParameterFields();
ParameterField pTitle = new ParameterField();
ParameterDiscreteValue pValue = new ParameterDiscreteValue();

pTitle.ParameterFieldName = "Gen"; //the name of the field @ Crystal Report

pValue.Value = txtGender.Text; //sending the text box value
pTitle.CurrentValues.Add(pValue);
pField.Add(pTitle);

crystalReportViewer1.ParameterFieldInfo = pField;


crpt objBT = new crpt();
objBT.Refresh();

crystalReportViewer1.ReportSource = objBT;


My problem is how do I combine these two codes so that when I enter the value in the textbox it will load the database values into the Crystal Report and pass the value of the textbox into the Crystal Report Parameter Object "Gen". I have tried combining these codes but it prompts a message "Parameter value is incorrect". Anyone?
Posted

1 solution

Please find below code it migit be help you

ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt");

ParameterFieldDefinitions crParameterFieldDefinitions ;
ParameterFieldDefinition crParameterFieldDefinition ;
ParameterValues crParameterValues = new ParameterValues();
ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

crParameterDiscreteValue.Value = textBox1.Text;
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
crParameterFieldDefinition = crParameterFieldDefinitions["Customername"];
crParameterValues = crParameterFieldDefinition.CurrentValues;

crParameterValues.Clear();
crParameterValues.Add(crParameterDiscreteValue);
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();
 
Share this answer
 
v2

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