将文本框值传递给水晶报表

时间:2011-12-23 01:59:20

标签: c# visual-studio-2010 crystal-reports

将TextBox值传递给Crystal Report时出错:

System.Runtime.InteropServices.COMException(0x8002000B): Invalid index. (Exception from HRESULT: 0x8002000B(DIS_E_BADINDEX))
at CrystalDecisions.ReportAppServer.DataDefModel.FieldsClass.get_Item(Int32 Index)
at CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions.get_Item(Int32 Index)
at CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions.get_Item(String  fieldName)
at WindowsFormsApplication1.Form15.button1_Click(Object sender, EventArgs e) in ..\Form15.cs: line 134

这是我的代码,用于在报表中显示我在文本框中输入的值,包括错误行:

  ParameterFieldDefinitions paramFieldDefinitions;
  paramValue = new ParameterValues();
  discreteValue = new ParameterDiscreteValue();

  discreteValue.Value = Convert.ToString(txtYear.Text);
  paramFieldDefinitions = CrystalReport81.DataDefinition.ParameterFields;
  fieldDefinition = paramFieldDefinitions["Year"]; <-- the is the line 134
  commonParam();

  discreteValue.Value = Convert.ToString(comMonth.Text);
  paramFieldDefinitions = CrystalReport81.DataDefinition.ParameterFields;
  fieldDefinition = paramFieldDefinitions["Month"];
  commonParam();

  discreteValue.Value = Convert.ToString(Grade);
  paramFieldDefinitions = CrystalReport81.DataDefinition.ParameterFields;
  fieldDefinition = paramFieldDefinitions["Grade"];
  commonParam();

  discreteValue.Value = Convert.ToString(date);
  paramFieldDefinitions = CrystalReport81.DataDefinition.ParameterFields;
  fieldDefinition = paramFieldDefinitions["date"];
  commonParam();

以下代码用于将参数传递给存储过程:(正在运行)

  ParameterFields myParams = new ParameterFields();

  ParameterField name = new ParameterField();
  ParameterDiscreteValue valName = new ParameterDiscreteValue();
  name.ParameterFieldName = "@Log_User";
  valName.Value = comEmployee.Text;
  name.CurrentValues.Add(valName);
  myParams.Add(name);

  ParameterField month = new ParameterField();
  ParameterDiscreteValue valMonth = new ParameterDiscreteValue();
  month.ParameterFieldName = "@Month";
  valMonth.Value = Convert.ToInt32(iMonth);
  month.CurrentValues.Add(valMonth);
  myParams.Add(month);

  ParameterField year = new ParameterField();
  ParameterDiscreteValue valYear = new ParameterDiscreteValue();
  year.ParameterFieldName = "@Year";
  valYear.Value = Convert.ToInt32(txtYear.Text);
  year.CurrentValues.Add(valYear);
  myParams.Add(year);

  crystalReportViewer1.ParameterFieldInfo = myParams;
  crystalReportViewer1.ReportSource = CrystalReport81;
  crystalReportViewer1.Refresh();

我真的不知道如何将文本框中的值添加到Crystal Report中..请帮我做...

谢谢你,上帝保佑:D

2 个答案:

答案 0 :(得分:0)

TextObject yr =  (TextObject)CrystalReport81.ReportDefinition.Sections["Section3"].ReportObjects["Text1"];
            yr.Text = txtYear.Text;

FORM Textbox - &gt; CRYSTAL REPORT文本框

通过添加上面的代码,您可以显示在文本框中输入的值..但首先您应该在Crystal Report中有一个空的文本框,因为这将捕获您从FORM文本框中抛出的值

我希望它可以帮助别人......因为它帮助了我很多..

答案 1 :(得分:0)

 if (textBox1.Text != "")
                    {
                    //just Intlization Object's Here n Access Data using .xsd in Project
                        DataSet1.DataTable2DataTable dtcity = null;
                        DataSet1TableAdapters.DataTable2TableAdapter adpt = null;
                        ParameterFields myParams = null;
                        ParameterField name = null;
                        CrystalReport1 rpt = null;
                        ParameterDiscreteValue valYear = null;
                      try
                        {
// For Get Data For DB
                            dtcity = new DataSet1.DataTable2DataTable();
                            adpt = new DataSet1TableAdapters.DataTable2TableAdapter();
                            dtcity = adpt.GetStateNCityData();
                            rpt = new CrystalReport1();
               rpt.Database.Tables["DataTable2"].SetDataSource(dtcity.Copy() as DataTable);
//For Get Data For DB
//Add Paramater 
                            myParams = new ParameterFields();
                            name = new ParameterField();
                            valYear = new ParameterDiscreteValue();
                            name.ParameterFieldName = "@textName";
                            valYear.Value = textBox1.Text;
                            name.CurrentValues.Add(valYear);
                            myParams.Add(name);
                            crystalReportViewer1.ParameterFieldInfo = myParams;
//Add Paramater 
                            crystalReportViewer1.ReportSource = rpt;
                        }
                        catch (Exception ex)
                        {
                        }
                        finally { }
          }
                    else
                    {
                        MessageBox.Show("Please Enter Name");
                        textBox1.Focus();
                    }

@textName中,您必须在Crystal Report中添加一个参数作为ParameterFields ..然后将其拖放到“报告”部分中。