Passing multiple parameters to Crystal Report

馋奶兔 提交于 2019-12-13 07:13:21

问题


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;

回答1:


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);



回答2:


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




回答3:


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);



回答4:


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");
}


来源:https://stackoverflow.com/questions/5145044/passing-multiple-parameters-to-crystal-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!