Click to See Complete Forum and Search --> : Problem: Crystal Reports through code


vrathaure
03-28-2008, 03:06 AM
Hi all,
I m using visual studio 2005 n I want to view a crystal report (rpt) on an aspx page without having to log on and pass parameters through the crystal report's default way.

I have 2 variable that I am populating thru the drop down lists, but I am not able to apply those parameter fields to my current report.

The code that I am using is as below:-


protected void btnShowReport_Click(object sender, EventArgs e)
{
CrystalDecisions.Shared.ParameterField PARAMETER_FIELD_Cat = new CrystalDecisions.Shared.ParameterField();
CrystalDecisions.Shared.ParameterField PARAMETER_FIELD_SKU = new CrystalDecisions.Shared.ParameterField();

PARAMETER_FIELD_Cat.Name = "CategoryName";
PARAMETER_FIELD_SKU.Name = "SKUName";

CrystalDecisions.Shared.ParameterDiscreteValue PARAMETER_Discrete_FIELD_Cat = new ParameterDiscreteValue();
CrystalDecisions.Shared.ParameterDiscreteValue PARAMETER_Discrete_FIELD_SKU = new ParameterDiscreteValue();

PARAMETER_Discrete_FIELD_Cat.Value = ddlItems.SelectedItem.Text.ToString();
PARAMETER_Discrete_FIELD_SKU.Value = ddlSKUs.SelectedItem.Text.ToString();

PARAMETER_FIELD_Cat.CurrentValues.Add(PARAMETER_Discrete_FIELD_Cat);
PARAMETER_FIELD_SKU.CurrentValues.Add(PARAMETER_Discrete_FIELD_SKU);

CrystalDecisions.Shared.ParameterFields PARAMETER_FIELDs = new ParameterFields();
PARAMETER_FIELDs.Add(PARAMETER_FIELD_Cat);
PARAMETER_FIELDs.Add(PARAMETER_FIELD_SKU);

CrystalReportViewer1.ParameterFieldInfo = PARAMETER_FIELDs;



//code to log on to the db server
string reportPath = Server.MapPath("RPT_PriceDissemination.rpt");
ReportDocument myReportDocument = new ReportDocument();
myReportDocument.Load(reportPath);
myReportDocument.SetDatabaseLogon("<username>", "<password>");
//********

CrystalReportViewer1.ReportSource = myReportDocument;

// CrystalReportViewer1.ReportSource = "RPT_PriceDissemination.rpt";
CrystalReportViewer1.RefreshReport();
}


But it doesn't seem to work as desired.

I want to view the resulting report on button click, depending on my selections in the drop down list.

Please try and help me find the mistake that I m doing here.

Thanks in advance.

Regards
veegrl

chazzy
03-28-2008, 07:01 AM
I believe you should be doing it more like this...


rptCount = New ReportDocument
rptCount.Load(Server.MapPath("reportname.rpt"))

''Get the collection of parameters from the report
crParameterFieldDefinitions = rptCount.DataDefinition.ParameterFields
''Access the specified parameter from the collection
crParameter1 = crParameterFieldDefinitions.Item("Param1")
crParameter2 = crParameterFieldDefinitions.Item(“Param2")

''Get the current values from the parameter field. At this point
''there are zero values set.
crParameter1Values = crParameter1.CurrentValues
crParameter2Values = crParameter2.CurrentValues

''Set the current values for the parameter field
crDiscrete1Value = New ParameterDiscreteValue
crDiscrete1Value.Value = Request.Form(“value from your form.“)

crDiscrete2Value = New ParameterDiscreteValue
crDiscrete2Value.Value = Request.Form(“value from your form.“)

''Add the first current value for the parameter field
crParameter1Values.Add(crDiscrete1Value)
crParameter2Values.Add(crDiscrete2Value)

''All current parameter values must be applied for the parameter field.
crParameter1.ApplyCurrentValues(crParameter1Values)
crParameter2.ApplyCurrentValues(crParameter2Values)