将文本框值传递给Crystal Report参数 [英] Passing Textbox value to Crystal Report Parameter
问题描述
大家好。有人可以帮我解决我的问题。我已经有了这个代码,但我似乎无法将两者结合起来。
图例:dt1 =数据集
crpt =水晶报告文件
这是我的代码,用于将数据库值加载到使用txtGender文本框的值过滤的Crystal Report中:
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:
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();
虽然这是我将文本框txtGender值传递到名为Gen的Crystal Report Parameter对象的代码:< br $>
While this is my code for Passing the textbox txtGender value into the Crystal Report Parameter object named "Gen":
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;
我的问题是我如何组合这两个代码,以便当我在文本框中输入值时它将加载数据库值进入Crystal Report并将文本框的值传递到Crystal Report参数对象Gen。我尝试组合这些代码,但它会提示参数值不正确的消息。任何人?
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?
推荐答案
请找到以下代码,它可以帮助你
ReportDocument cryRpt = new ReportDocument();
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt); < br $>
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; < br $>
crParameterValues.Clear();
crParameterValues.Add(crParameterDiscreteValue);
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues) ;
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();
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();
这篇关于将文本框值传递给Crystal Report参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!