简体   繁体   中英

Passing multiple parameters to Crystal Report

I'm working on Visual Studio 2008 and SQL Server 2008, language C#

I want to pass multiple parameters to Crystal Report in ASP.NET. I have two parameters @accountnumber and @customerid . But I can only pass one parameter to my report as in code below.

CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.ServerName = "CJ-PC";
CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.UserID = "sa";
CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.Password = "***";
CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.DatabaseName = "Online";

string accountnumber = "acc001";
string customerID = "cus001";

ParameterField paramField = new ParameterField();
ParameterFields paramFields = new ParameterFields();
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

paramField.Name = "@account_number";
paramDiscreteValue.Value = accountnumber;
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
CrystalReportViewer1.ParameterFieldInfo = paramFields;

ParameterField paramField1 = new ParameterField();
ParameterFields paramFields1 = new ParameterFields();
ParameterDiscreteValue paramDiscreteValue1 = new ParameterDiscreteValue();

paramField1.Name = "@account_number";
paramDiscreteValue1.Value = accountnumber;
paramField1.CurrentValues.Add(paramDiscreteValue1);
paramFields1.Add(paramField1);

paramField1.Name = "@customer_id";
paramDiscreteValue1.Value = customerID;
paramField1.CurrentValues.Add(paramDiscreteValue1);
paramFields1.Add(paramField1);
CrystalReportViewer1.ParameterFieldInfo = paramFields1;

You must use new keyword before assigning new values for paramField1

paramField1.Name = "@account_number";
paramDiscreteValue1.Value = accountnumber;
paramField1.CurrentValues.Add(paramDiscreteValue1);
paramFields1.Add(paramField1);

//Here is the missing code
paramField1 = new ParameterField();

paramField1.Name = "@customer_id";
paramDiscreteValue1.Value = customerID;
paramField1.CurrentValues.Add(paramDiscreteValue1);
paramFields1.Add(paramField1);

This is what I did in my code

CrystalReportViewer1.RefreshReport();

String ssCustomer = Session["ssCustomer"].ToString();
string strconstring = ConfigurationManager.ConnectionStrings["ONLINE_BANKING2_ConnectionString"].ConnectionString;
string sqlquery2;
sqlquery2 = "SELECT [account_number],[customer_id] from customer_details where customer_id = '" + ssCustomer + "'";
SqlConnection mycon2 = new SqlConnection(strconstring);
SqlCommand cmd = new SqlCommand(sqlquery2, mycon2);
mycon2.Open();
SqlDataReader myreader = cmd.ExecuteReader();
myreader.Read();
string accountnumber = myreader["account_number"].ToString();
string customerID = myreader["customer_id"].ToString();
myreader.Close();
mycon2.Close();

ParameterDiscreteValue objDiscreteValue;
ParameterField objParameterField;

//specify all the database Login details
CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.ServerName = "CJ-PC";
CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.UserID = "sa";
CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.Password = "123";
CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.DatabaseName = "online_banking2";

//Set value for first parameter
objDiscreteValue = new ParameterDiscreteValue();
objDiscreteValue.Value = accountnumber;
objParameterField = CrystalReportViewer1.ParameterFieldInfo["@account_number"];
objParameterField.CurrentValues.Add(objDiscreteValue);
CrystalReportViewer1.ParameterFieldInfo.Add(objParameterField);

objParameterField = CrystalReportViewer1.ParameterFieldInfo["@customer_id"];
objDiscreteValue = new ParameterDiscreteValue();
objDiscreteValue.Value = customerID;
objParameterField.CurrentValues.Add(objDiscreteValue);
CrystalReportViewer1.ParameterFieldInfo.Add(objParameterField);

If the above code doesn't help you, follow the instructions below:

Go to CrystalReportSource properties -> Report properties -> Paratemers properties -> Add new parameter

And there you go. You must name the parameter as @account_number and give it a ControlID

You can pass values to various parameters in Crystal Reports following this way.

void SetParameterValues()
{
    crReport report = new crReport();
    report.SetParameterValue("parameterName1","parameterValue1");
    report.SetParameterValue("parameterName2","parameterValue2");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM