Getting ComException while generating report using FieldDefinition. RRS feed

  • Question

  • In the link given below -

    I am just trying to show the crystal report based on that code given inside the page of the link.

    Here the details and codes given-

    Group Fields Runtime Customization

    You can use group fields to group the data in the report based on conditions supplied.

    The following example lets the user modify, at runtime, the group condition field for a report.

    Scenario: Changing groups at runtime.

    Suppose you have a Crystal report and the following conditions exist in the report:

    • The report contains three fields in the details section {Customer.City}, {Customer.Country}, and {Customer.Region}.
    • A group is created in the report on the {Customer.City} field.

    You can change groups in the report by making selections with a ComboBox control.

    To change groups at runtime

    1. Create a report that uses the Customer table in the sample database, xtreme.mdb .
    2. Add a Group to the report using the field {Customer.City}.
    3. Bind the report to a viewer.
    4. Add a ComboBox control to the form.
    5. In the ComboBox control, list the fields {Customer.City}, {Customer.Country}, and {Customer.Region} as options.

      From this list, users select the field they want to group on.

    6. Add a Button control to the form.

      Users click this button to perform grouping on the field selected in the ComboBox.

    7. Double-click the Button control to specify code for the Click event. Depending on the language you are using, insert the appropriate code (from the examples below) in the corresponding source file.
      [Visual Basic]

      FieldDefinition FieldDef = new FieldDefinition ();
      FieldDef = Report.Database.Tables [0].Fields [comboBox1.Text];
      Report.DataDefinition.Groups [0].ConditionField = FieldDef;
    8. CrystalReportViewer.RefreshReport();

    After executing this code  I am getting Error - (Here some code snippet of my application is-)

     private void btnShow_Click(object sender, EventArgs e)
                ReportDocument crDoc = new ReportDocument();
                //xtreme1 obj = new xtreme1();
                //Database dbase = obj.Database;        

                OdbcDataAdapter adap = new OdbcDataAdapter("SELECT * FROM Customer", con);
                DataSet ds = new DataSet();
                crystalReportViewer1.ReportSource = crDoc;           
                FieldDefinition fieldDef = crDoc.Database.Tables["Customer"].Fields[cmbReport.Text];  //Error in this line
                crDoc.DataDefinition.Groups[0].ConditionField = fieldDef;
                //crystalReportViewer1.ReportSource = crDoc;
    and the error is

    ComException was unhandled
    Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

    I am not getting what is it? Please help me.
    • Moved by nobugzModerator Tuesday, September 8, 2009 10:10 AM (From:.NET Base Class Library)
    Tuesday, September 8, 2009 7:44 AM




    Same code you have given worked for me. 

    FieldDef = D.Database.Tables.Item(0).Fields.Item("GROUPCODE")
    D.DataDefinition.Groups.Item(0).ConditionField = FieldDef

    Then I changed it to

    FieldDef = D.Database.Tables.Item(0).Fields.Item("ACCOUNTS.GROUPCODE")
    D.DataDefinition.Groups.Item(0).ConditionField = FieldDef

    Then I got the same exception you got.  So, change it to City, Country, Region instead of Customer.City, Customer.Country, Customer.Region.

    • Proposed as answer by Harry Zhu Monday, September 14, 2009 3:07 AM
    • Marked as answer by Harry Zhu Wednesday, September 16, 2009 3:08 AM
    Wednesday, September 9, 2009 5:05 AM