Seal Report Forum
»
Report Edition
»
Reports
»
Update restrictions at the init script
Rank: Member
Groups: Registered
Joined: 2/25/2017(UTC) Posts: 13 Location: Monterrey
|
Hi
Is it possible to set the value of a restriction at the init script without prompt? And if is possible how can I do it?
Thanks.
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 12/20/2013(UTC) Posts: 1,209 Thanks: 14 times Was thanked: 206 time(s) in 199 post(s)
|
Yes, select the main 'Views' node in the tree view, then you can edit the property 'Init Script', you have several samples to modify your restrictions: Code: //Script executed when the report is initialized
log.LogMessage("Executing Init Script");
//report.Models[0].Restrictions[0].Value1 = "1994";
//report.Models[0].Restrictions[0].Date1 = DateTime.Now.AddYears(-20);
//report.Models[0].GetRestrictionByName("Order Year").Value1 = "2015";
//report.Models[0].GetRestrictionByName("Category").EnumValues.Add("1");
//report.DisplayName = System.IO.Path.GetFileNameWithoutExtension(report.FilePath) + "-" + DateTime.Now.ToShortDateString();
//Set the first value of an enum
//var enums = report.Models[0].Source.MetaData.Enums.FirstOrDefault(i=>i.Name == "Category");
//report.Models[0].GetRestrictionByName("Category").EnumValues.Add(enums.Values[0].Id);
Additional properties for the restrictions are: public bool Required public PromptType Prompt where PromptType can be: [Description("No prompt")] None, [Description("Prompt at execution")] Prompt, [Description("Prompt only one value")] PromptOneValue, e.g. Code:report.Models[0].Restrictions[0].Required = false;
report.Models[0].Restrictions[0].Prompt = PromptType.None;
Edited by user Tuesday, May 9, 2017 6:53:43 AM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 12/3/2018(UTC) Posts: 33 Thanks: 13 times
|
Hi, I have a report that includes about 20 models. These models have some restrictions in common. My questions are: 1)How can I update my restrictions at start with your code but in a report with many models.
2)how can I set a restriction in one model to the value of another restriction in another model.
Thanks in advance
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 12/20/2013(UTC) Posts: 1,209 Thanks: 14 times Was thanked: 206 time(s) in 199 post(s)
|
It is just browsing the report models and restrictions: Code:
foreach (var model in report.Models) {
foreach (var restriction in model.Restrictions) {
restriction.Value1 = "1994";
}
}
You can also take restriction 1 from model 1 to update restriction 2 of model 2....
|
1 user thanked epf for this useful post.
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 12/3/2018(UTC) Posts: 33 Thanks: 13 times
|
Hi epf, thanks for responding. I have attached a sample report based on Northwind. I want "Supplier Country" in "model" to be set by default to the first item in its enumerated list.Then I need to make "Customer Contry" in "model2" to be equal to the values selected in model1's "Supplier Country". Restriction.srexI cant't attach files, so uploaded it to my google drive. I used your code with no luck.I really appreciate it if you would fix the attached report. Thanks Edited by user Wednesday, April 24, 2019 10:09:22 AM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 12/3/2018(UTC) Posts: 33 Thanks: 13 times
|
Hi, This is the code I added to my init script: Code: var modelSource = report.Models[0];
foreach (var model in report.Models) {
foreach (var restriction in model.Restrictions) {
if(restriction.Name == "Customer-Country") {
restriction.EnumValues = modelSource.GetRestrictionByName("Supplier Country").EnumValues;
}
}
}
but it seem there is something wrong with this line: Code:if(restriction.Name == "Customer-Country")
As soon as I change it to Code:if(restriction.Name != null)
everything works fine. Could you please correct me , Thanks.
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 12/3/2018(UTC) Posts: 33 Thanks: 13 times
|
Problem Solved! I changed with Code:restriction.DisplayNameEl
and it works just fine now. Thanks
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 12/3/2018(UTC) Posts: 33 Thanks: 13 times
|
Hi, What if I want to update the other restrictions operator to the main restriction operator.For example my report has two date type restrictions : MainDate & SubDate. I want SubDate's operator and restrction value be set to whatever is selected for MainDate. How is that possible? PS: I have added this code to my init script to just update the restriction values : Code: var modelSource = report.Models[0];
foreach (var model in report.Models) {
foreach (var restriction in model.Restrictions) {
if(restriction.DisplayNameEl == "SubDate") {
restriction.Date1 = modelSource.GetRestrictionByName("MainDate").Date1;
restriction.Date2 = modelSource.GetRestrictionByName("MainDate").Date2;
}
But there is a problem that at first execution of my report the "MainDate" values are not updated to "SubDate" but at the second execution it works fine !!! Edited by user Sunday, October 6, 2019 8:14:38 AM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 12/20/2013(UTC) Posts: 1,209 Thanks: 14 times Was thanked: 206 time(s) in 199 post(s)
|
I made a quick test on the 'Order Search' sample report. Just added the 'Required Date' as a restriction and the following init script: Code: var restr = modelSource.GetRestrictionByName("Required Date");
restr.Date1 = modelSource.GetRestrictionByName("Order Date").Date1;
restr.Date2 = modelSource.GetRestrictionByName("Order Date").Date2;
and this works fine at the first execution... can you redo the sample to find the problem
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 12/3/2018(UTC) Posts: 33 Thanks: 13 times
|
Originally Posted by: epf I made a quick test on the 'Order Search' sample report. Just added the 'Required Date' as a restriction and the following init script: Code: var restr = modelSource.GetRestrictionByName("Required Date");
restr.Date1 = modelSource.GetRestrictionByName("Order Date").Date1;
restr.Date2 = modelSource.GetRestrictionByName("Order Date").Date2;
and this works fine at the first execution... can you redo the sample to find the problem Hi epf, I did what you mentioned above , but the operator for "Order Date" is set to "is between" bu the operator for "Required Date" is "Equals". How to update the operators?? Thanks
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 12/20/2013(UTC) Posts: 1,209 Thanks: 14 times Was thanked: 206 time(s) in 199 post(s)
|
Actually, you should use the Pre-Load Script of your model to update the restriction values: Select you model, then edit the 'Pre-Load Script' property and set: Here is the code to update the restriction: Code:@using Seal.Model
@{
ReportModel model = Model;
var modelSource = model.Report.Models[0];
var restrSource = modelSource.GetRestrictionByName("Order Date");
var restr = model.GetRestrictionByName("Required Date");
restr.Date1 = restrSource.Date1;
restr.Date2 = restrSource.Date2;
restr.Operator = restrSource.Operator;
}
Check also: https://sealreport.org/H...ad-09f5-b2facac3ff82.htmEdited by user Monday, October 7, 2019 11:53:27 AM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 12/3/2018(UTC) Posts: 33 Thanks: 13 times
|
Hi epf, The init script works fine. The problem is that when the restriction is date/time type, it is not updated at the first execution . I attached a sample to show that: Just Open the report and before executing it change the end time to " 1/30/1995". You will see results that have Required Date of 1/31/1995 ,although I have changed the init script to make the Required Date the same as Order Date. But if you just press execute for the second time you will notice that only results with Required Date of 1/30/1995 are shown. https://gofile.io/?c=1rbvOHI have no problem with other restriction using enumlist. Only date/time type is making problem. Thanks Edited by user Tuesday, October 8, 2019 7:37:51 AM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 12/20/2013(UTC) Posts: 1,209 Thanks: 14 times Was thanked: 206 time(s) in 199 post(s)
|
I checked the code and it appears that the Init Script is executed before the prompted restrictions are set in the report... So you should move your code the the 'Pre Load' script of your models...
Note that you can share C# function/script using the Server Manager -> Configure Server and then create a Common Script using the 'Common Scripts' property. Then just call your function from any script in your reports.
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 12/3/2018(UTC) Posts: 33 Thanks: 13 times
|
Originally Posted by: epf I checked the code and it appears that the Init Script is executed before the prompted restrictions are set in the report... So you should move your code the the 'Pre Load' script of your models...
Note that you can share C# function/script using the Server Manager -> Configure Server and then create a Common Script using the 'Common Scripts' property. Then just call your function from any script in your reports. Hi epf, I just edited this report and changed the restriction to enumlist type and added the "DateEnum" enumlist to Northwind DB. You can see in this Report that whatever you set for the first restriction gets updated to the second one even at the first report execution. Report & DBSo it appears Init Script is executed after the prompted restrictions are set in the report When they are set to custom enumlist. Edited by user Wednesday, October 9, 2019 6:26:37 AM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 12/3/2018(UTC) Posts: 33 Thanks: 13 times
|
Hi epf, Did you get any chance to check what the problem is? Thanks
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 12/20/2013(UTC) Posts: 1,209 Thanks: 14 times Was thanked: 206 time(s) in 199 post(s)
|
Yes, in your sample you assign the reference of the Enum Value list...so you did not made the copy by value (it's C# consideration). Code:restriction.EnumValues = modelSource.GetRestrictionByName("Order Date").EnumValues;
so it works because the EnumValues of the "Required Date" restriction is the same object as the one of the "Order Date" restriction once again you should not use the Init Script to do that, the best practice is to create a Task that will do the job: Just create a task in you report, then add the following script: Code:@using Seal.Model
@{
ReportTask task = Model;
Report report = task.Report;
report.Models[0].GetRestrictionByName("Required Date").Date1 = report.Models[0].GetRestrictionByName("Order Date").Date1;
report.Models[0].GetRestrictionByName("Required Date").Date2 = report.Models[0].GetRestrictionByName("Order Date").Date2;
}
this will always work, even the first time...
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 12/3/2018(UTC) Posts: 33 Thanks: 13 times
|
Hi epf, thanks for your reply. What if there are many models in my report?
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 12/20/2013(UTC) Posts: 1,209 Thanks: 14 times Was thanked: 206 time(s) in 199 post(s)
|
Code: foreach (var model in report.Models) {
}
|
|
|
|
Seal Report Forum
»
Report Edition
»
Reports
»
Update restrictions at the init script
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.