Crystal Reports in VS2010: Prompted Parameter value in Design Time - c#

I have a crystal report and in design time on VS2010 I apply a parameter field via the [Database Expert][Add cmd][create parameter] wizard. But this prompts me for a specific value for my new parameter. I don't see why it should as its going to be dynamic!
In my c# code, my solution only works if the dynamic value at run time matches the design-time prompted value. This seems to defeat the purpose.
In my run-time code I have:
crystalReport.SetParameterValue("TenantID", tenantID);
TenanID is the CR parameter in design time
tenanID is the picked up dynamic value
Can't i make this truly dynamic?

Use:
crystalReport.ParameterFields(1).AddCurrentValue (num)
where "num" is your parameter value

Related

VS/C# Autogenerate Named Arguments when calling a method

Visual Studio (C#) has the concept of Named Arguments. Is there a way, without a 3rd party plug-in to auto-generate the parameter list when calling the method? (I'm looking to save a bunch of typing by having the list of param: [value], //type auto-generated via a keyboard shortcut/chord.) This would be akin to what the SSMS / RedGate intellisense does.
For example in SSMS:
EXEC dbo.ContactInfo_Get
#pk_ContactInfo = 0 -- int <---THIS IS WHAT I MEAN
For example in VS / C#
MyClass.UpdateMethod(
param1: 0, //int
param2: "" //string
);
I realize that intellisense will show me the parameters, but I'm looking to have a kind of template list of the named argument parameters magically appear to save typing all the named arguments by hand. For example, start typing the method's name, get to the param list, tab+tab (or some other shortcut magic), have a named argument parameter list show up that I can then update the values for/adjust as needed.
If there isn't a naive VS / intellisense way to do this, can anyone suggest an inexpensive or free plug-in?
Thanks!
It works in VS2022.
step - write method name and hit tab (this will generate all default values for all parameters)
step - Hit CTRL + '.' and select "Add argument name"
result

date time is being set to default, how can I use this parameter and get a value from textbox?

The code below seems to be setting the time to SQL's default rather than converting from the text box. How can i fix this? I'm trying to learn how to do parameters rather than referring to the text box directly, as this was advised in some answers to some of my earlier questions.
Additional information language being used is c#, IDE is visual and using local SQL server
command.Parameters.AddWithValue("#ScheduledDateTime", SqlDbType.DateTime);
{
DateTime.Parse(ScheduledDateTime.Text);
};
The AddWithValue method doesn't take the type as a parameter, it is just the parameter name and it's value:
command.Parameters.AddWithValue("#ScheduledDateTime", DateTime.Parse(ScheduledDateTime.Text));
Or you could use:
command.Parameters.Add("#ScheduledDateTime", SqlDbType.DateTime).Value = DateTime.Parse(ScheduledDateTime.Text);
As it stands though your statement adding the parameter, and statement parsing the datetime are not related, so you never actually pass the value to the command.

Is thera a way to pass NULL value to Crystal Reports programmatically

It is VS 2010 version.
CR is able to accept NULL values, because when it prompts me for parameter values, there is cute little check box which says 'Set to NULL'. This is behavior that I need.
But, I almost never call reports like that, I do it in code. So, if a user does not provide a value for a parameter, I would like to pass NULL value in its place, so I could do something differently in report itself.
I've searched around net for ages, mostly in CR forums, and I've got number of solution, non of which actually works.
This is one of them:
ParameterDiscreteValue objDiscreteValue = new ParameterDiscreteValue();
objDiscreteValue.Value = null;
rptH.SetParameterValue(parName, new ParameterDiscreteValue(objDiscreteValue));
CR says you haven't provided a value for ... parameter.
I muscle solve it by creating double parameters. One real parameter, and one satellite parameter for it (boolean value, only to see if there is a value for real parameter). And it works. But it is tedious, and makes my generics (automatic prompting for parameters based on report metadata) very very hard.
I am hoping someone has dug deep enough to find a solution for this problem.
Regards,
I did this with VS.2005, but i think it's the same problem. From experience, we didn't have any trouble with VARCHAR data type, but the problem arises when we use the Number data type.
For VARCHAR Data Type, you just pass an empty String to the parameter. For example, I have parameter within SQL Server with data type VARCHAR as below:
MyParamFields.Add(AddParamReport("#TheActiveYear", mTahunAktiv, ParameterValueKind.StringParameter))
In this case, if I want to pass NULL value I just give empty string to mTahunAktiv variable within my VB.Net Code, like this mTahunAktiv="". and the CR suggest this as null value and give the right result.
For kind of Number Data Type, it's little tricky. I think this is the best way for now, same as you I've googled but never know the solution yet.
For example in my case I have a parameter for passing Month in number value with data type SMALLINT, but sometimes a user needs the value as NULL. To overcome the problem I just send a value, example 13 to that parameter, but before that you must handle it in the SQL QUERY, if you give the 13 value to that parameter it becomes NULL. For this, I am using STORED PROCEDURE with dynamic Filtering Parameter. For me this can solve my problem.
I'm not sure if you will understand what I mean, but I hope this little exprience can help you to solve your problem.
I stumbled upon DBValue.Null from ADO.NET DataSets and it sparked me to try to pass that to Crystal, and voila, it works. Just like this:
ParameterDiscreteValue objDiscreteValue = new ParameterDiscreteValue();
objDiscreteValue.Value = DBNull.Value;
rptH.SetParameterValue(parName, objDiscreteValue);
I hope this helps to someone, but I think this should be documented somewhere.
Best Regards,

SSRS URL Parameter won't set

I have the following c# code in a web form:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string url;
string startdate;
string enddate;
string costcenter;
string account;
//url = "http://server/reportserver?/Finance/Cost Spending/Cost Center Details&rs:Command=Render&rc:Parameters=false";
url = "http://server/reportserver?/Finance/Cost Spending/Cost Center Totals&rs:Command=Render&rc:Parameters=false";
costcenter = "&costcenter=990";
startdate="&startdate=" + Convert.ToString(txtStartDate.Text);
enddate = "&enddate=" + Convert.ToString(txtEndDate.Text);
account="&account=" + Convert.ToString(GridView1.SelectedRow.Cells[1].Text);
url =url + startdate + enddate + costcenter + account;
//TextBox1.Text = Convert.ToString(GridView1.SelectedRow.Cells[1].Text);
Response.Redirect(url, false);
}
I've tested a very similer version of this code against another report, the only different being the costcenter parameter. The other report worked fine, but every time i run this code i get the error: "the 'costcenter' parameter is missing a value". The only thing i can think of that's significantly different between the two reports is that in the Cost Center Totals report the costcenter parameter is used to populate the accounts parameter (both are multi-select).
Here's what the parameters page looks like:
Are you using multiple datasets in your report? If so, try setting the datasets to execute serially:
Open the data source dialog in report designer
Select the "Use Single Transaction" checkbox
Some other troubleshooting steps you can try:
Try removing the Report Server Command "&rs:Command=Render" from the query string and see what happens.
Also, if you change the Viewer Command to true, are you able to at least see that the parameters are populated properly (ie: "&rc:Parameters=true"?
I've been under the impression that one difference between running a report on the web server and running it in code is that in code it does not bother with the "Available Values" queries for parameters. In code, you aren't selecting a human-readable label from a drop-down which then passes in the corresponding value. Instead, you just provide the value to the parameter.
So, have you tried not specifying the cost center parameter at all? If the account parameter is all the report's recordsets really are based on, then it may be superfluous to set it.
Update:
Since the time I wrote this answer originally I've learned that dependent parameters (at least in SSRS 2008) must be after the parameter(s) they're dependent on. Make sure your parameters are ordered in a sensible way.
It looks like that's what you've done, I just wanted to mention the possibility.
Check that the Costcenter 990 is in your database!
I did some testing. I have a table "Person" with 4 rows and a PersonID 1 to 4. If set &PersonID=5 for a required single value parameter with "&rc:Parameters=true" then I get a drop-down box so I can choose one of the 4 valid persons.
If I set &PersonID=5 with "&rc:Parameters=false" then I get the message "the PersonID parameter is missing a value".
I faced similar kind of issue. Removing the parameter dependency made it work. I had parameter B depending on parameter A. It worked perfectly when you access the report server directly. But when I tried to bind it programmatically to the report viewer control, even after supplying the parameter using ReportParameter and adding to Parameters list, it was merely saying that "The 'Parameter B' is missing".
So, I went back to the report server. Redeployed the report by removing parameter dependency. It worked. :-)
Ok, I figured out the issue thanks to Alison's poke at the parameters/data sets. What was happening was I had 4 parameters, start date, end date, cost center, and accounts. In the report the account data set relies on the cost center parameters to populate the drop down list. I created a copy of the report, removed the cost center parameter, and removed the data set from the account parameter. I updated the code and ran it and BAM! It worked.

Assign multiple values to a parameter in Crystal Reports

I have added a parameter to my report with the option "Allow Multiple Values" checked.
This is a status column (IE, Proposed, In Progress, Completed, Canceled), and I want the user to be able to select which (and how many) different OrderStatus to report on.
How I normally set parameters is:
report.SetParameterValue("#dtBegin", dtBegin.DateTime);
What I tried to do for the multiple values was something like this:
//pseudo loop
foreach(int intOrderStatus in intSelectedOrderStatuses)
{
report.Parameter_OrderStatus.CurrentValues.AddValue(intOrderStatus);
}
I have checked it does add the values to the OrderStatus parameter, but when the report runs, the CrystalReports dialog pops up and asks me to enter values for the OrderStatus parameter. So it seems as though the values aren't "commited" to the parameter. I have done a number of searches and can't figure out why it's not working.
Thanks,
Just set the parameter value with an array of ints.
report.SetParameterValue("#OrderStatus", new int[]{1,2,3});
in the select expert you would use the in operator.
{table.order_status_id} in {?#OrderStatus}
What you can do is, make a normal parameter field,i.e without multiple values, only discreet values true, all you need to pass is 1,2,3,4. "," is the delimiter for separation use what ever you think works for you, then in record selection formula simply put
{table.order_status_id} in split({#OrderStatus}, ",")
all you need to pass from you page is the string 1,2,3,4 and it should work
Have you set the parameter to Hidden in the Crystal Reports parameter options?
I haven't tried this, but I think that you should be able to add intOrderStatus to either a ParameterDiscreteValue or ParameterRangeValue and pass that into Parameter_OrderStatus.CurrentValues instead of intOrderStatus.
Well i have same issue. The work around is very simple. Don't add data source after parameters. e.g
report.SetParameterValue("#dtBegin", dtBegin.DateTime);
report.SetParameterValue("#dtBegin2", dtBegin.DateTime1);
//Note datasource is assigned after parameters
report.SetDatasource(dataset);
The crystal report will refresh parameters before applying data source to report.
The below is the not popup discrete dialog box
//Note Datasource is applied before parameters
report.SetDatasource(dataset);
report.SetParameterValue("#dtBegin", dtBegin.DateTime);
report.SetParameterValue("#dtBegin2", dtBegin.DateTime1);
Following is tested in Crystal Reports version 13.0.20:
1) In Parameter Fields section add new parameter as follow:
Name: ParamMultipleOrderStatus
Type: Number
Value Options:
Allow multiple values: true
2) Choose the Select Expert Record ... in Crystal Reports and code may like this (use = operator):
{Orders.OrderStatus} = {?ParamMultipleOrderStatus}
3) Use following code:
foreach (int intOrderStatus in intSelectedOrderStatuses)
{
report.ParameterFields["ParamMultipleOrderStatus"].CurrentValues.AddValue(intOrderStatus);
}

Categories