cancel
Showing results for 
Search instead for 
Did you mean: 

asp.net - passing inline Parameter forms vs. Webforms fails

Former Member
0 Kudos

Hi there, I am experimenting with CR. I am able to get a crystal report through forms however having issues to make the same thing work with webforms...

I am presenting here 3 sets of codes, the first 2 works fine however having issues getting the 3rd working. Any suggestions? Details below:

  1. passing inline parameter through forms which works fine
  2. no inline parameter trough webforms just getting a crystal report works fine
  3. passing inline parameter through a webform having the following issues:
    • I am being prompted to enter value which is not desired
    • getting an error

The sample code below works fine, having DATAS2 as a DataSet2 created and crystal report added to the project. I am getting the report:

I did simplify it therefore only 1 parameter is being passed for now to the Crystal Report Viewer and the Crystal Report "crName.rpt" has only 1 parameter under "Parameter Fields".


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

using System.Data.SqlClient;

namespace SP_DS_CrystalRptDemo

{

    public partial class Form1 : Form

    {

        ReportDocument cry = new ReportDocument();

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            cry.Load(@"C:\Users\\userName\Documents\Visual Studio 2008\Projects\SP_DS_CrystalRptDemo\SP_DS_CrystalRptDemo\crName.rpt");

            SqlConnection con = new SqlConnection("Data Source=datasourceName;Initial Catalog=dataBaseName;User ID=sa;Password=password;Connect Timeout=15");

            SqlDataAdapter sda = new SqlDataAdapter("storedProcedureName, con);

            sda.SelectCommand.Parameters.Add("@dtDateBegin", System.Data.SqlDbType.DateTime).Value = "3-3-2016";

            sda.SelectCommand.Parameters.Add("@dtDateEND", System.Data.SqlDbType.DateTime).Value = "6-10-2016";

            sda.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataSet ds = new System.Data.DataSet();

            sda.Fill(ds, "DATAS2");

            cry.SetDataSource(ds);

            cry.SetParameterValue("crParameterName", "crParameterValue");

            //rd.SetParameterValue("@dtDateBegin", "3-3-2016");

            //rd.SetParameterValue("@dtDateEND", "6-10-2016");

            crystalReportViewer1.ReportSource = cry;

            crystalReportViewer1.Refresh();

        }

    }

}

The following report with asp.net webform works fine however there is no parameter being passed to the Crystal Report. Please note I am using all across the same database, same stored procedure. I am also using the same report however changing the "Parameter Field" to get my point across.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

namespace WebApplication1

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Button1_Click(object sender, EventArgs e)

        {

            ReportDocument cry = new ReportDocument();

            SqlConnection con = new SqlConnection("Data Source=serverName;Initial Catalog=databaseName;User ID=sa;Password=password;Connect Timeout=15");

            cry.Load(@"C:\Users\userName\Documents\Visual Studio 2008\Projects\WebApplication1\WebApplication1\crName.rpt");

            SqlDataAdapter sda = new SqlDataAdapter("storedProcedureName", con);

            sda.SelectCommand.Parameters.Add("@dtDateBegin", System.Data.SqlDbType.DateTime).Value = "1-1-2016";

            sda.SelectCommand.Parameters.Add("@dtDateEND", System.Data.SqlDbType.DateTime).Value = "7-10-2016";

            sda.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataSet st = new System.Data.DataSet();

            sda.Fill(st, "DATAS2");

            cry.SetDataSource(st);

            cry.SetDataSource(st);

            crystalReportViewer1.ReportSource = cry;

            crystalReportViewer1.Visible = true;

            crystalReportViewer1.RefreshReport();

        }

    }

}

Now comes the part where I am having issues.

When I invoke the report I always get prompted to enter the "crParameterField" value.

When I enter a value and click ok I am keep getting the following error message: "Object reference not set to an instance of an object." Everything is pretty much the same expect I am trying to pass 1 parameter now to the Crystal Report. I'll keep trying however if any suggestion that would be great since I am not quite sure what I am missing.

Here is the revised code of the previous working version again trying to pass 1 parameter to the Crystal Report:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

namespace WebApplication1

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Button1_Click(object sender, EventArgs e)

        {

            ReportDocument cry = new ReportDocument();

            SqlConnection con = new SqlConnection("Data Source=serverName;Initial Catalog=databaseName;User ID=sa;Password=password;Connect Timeout=15");

            cry.Load(@"C:\Users\userName\Documents\Visual Studio 2008\Projects\WebApplication1\WebApplication1\crName.rpt");

            SqlDataAdapter sda = new SqlDataAdapter("storedProcedureName", con);

            sda.SelectCommand.Parameters.Add("@dtDateBegin", System.Data.SqlDbType.DateTime).Value = "1-1-2016";

            sda.SelectCommand.Parameters.Add("@dtDateEND", System.Data.SqlDbType.DateTime).Value = "7-10-2016";

            sda.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataSet st = new System.Data.DataSet();

            sda.Fill(st, "DATAS2");

            cry.SetDataSource(st);

//new code

            ParameterFieldDefinitions crParameterFieldDefinitions;

            ParameterFieldDefinition crParameterFieldDefinition;

            ParameterValues crParameterValues = new ParameterValues();

            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            crParameterDiscreteValue.Value = "Parameter Value";

            crParameterFieldDefinitions = cry.DataDefinition.ParameterFields;

            crParameterFieldDefinition = crParameterFieldDefinitions["crParameterField"];

            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();

            crParameterValues.Add(crParameterDiscreteValue);

            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

//new code ends

            crystalReportViewer1.ReportSource = cry;

            crystalReportViewer1.Visible = true;

            crystalReportViewer1.RefreshReport();

        }

    }

}

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

I've thought I'll give an update if someone runs into something like this. When you are new to a project and CR naming conventions are not "quite" consistent all across the reports make sure the CR report parameter fields are having the same spelling and the case as in the code... something easy like that.

Hope this helps someone.

Attila

Answers (1)

Answers (1)

0 Kudos

Moved to .NET forums