[]
In a section report, you can use script to access controls, functions in a class, namespaces, etc. You can also create classes inside the script to call methods or add code to a report's script from a Windows Form. The following sections illustrate simple scripting scenarios with examples.
These steps assume that you have already added a Section Report (code based) template in a Visual Studio project.
To add script to a report to access a textbox named TextBox1 in the detail section and assign the text "Hello" to it:
On the script tab of the report, drop down the Object list and select Detail. This populates the Event drop-down list with section events.
Drop down the Event list and select Format. This creates script stubs for the event.
To access a textbox in the detail section in VB.NET script
Me.TextBox1.Text = "Hello"
Or
CType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = "Hello"
To access a textbox in the detail section in C# script
this.textBox1.Text = "Hello";
Or
((TextBox)rpt.Sections["detail"].Controls["TextBox1"]).Text = "Hello";
Using the AddNamedItem method, you can allow the script to access functions in a class file within your project. This allows you to keep secure information such as a database connection string or a SQL query string in the code instead of saving it in the RPX file.
In the Code View of the Form, add a class to your project named clsMyItem.
To add a class in Visual Basic.NET
Public Class clsMyItem
End Class
To add a class in C#
public partial class clsMyItem
{
}
Add a public function to your class using code like the following:
To create a public function in Visual Basic.NET
Public Function getMyItem() As String
getMyItem = "Hello"
End Function
To create a public function in C#
public string getMyItem()
{
return "Hello";
}
Go to the design view of the report and double-click the gray area around the design surface to create an event-handling method for the ReportStart event.
Add the following code to the handler:
To access the class in Visual Basic.NET
Me.AddNamedItem("myItem", new clsMyItem())
To access the class in C#
this.AddNamedItem("myItem", new clsMyItem());
From the Visual Studio toolbox, drag and drop a TextBox control onto the detail section of the report.
Go to the script tab and drop down the Object list to select Detail. This populates the Event drop-down list with section events.
Drop down the Event list and select Format. This creates script stubs for the event.
Add the following script to the event to access a control on the report and populate it using the named item.
To access the control in VB.NET script
Me.TextBox1.Text = myItem.getMyItem()
Or
CType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = myItem.getMyItem()
To access the control in C# script
this.textBox1.Text = myItem.getMyItem();
Or
((TextBox)rpt.Sections["detail"].Controls["textBox1"]).Text = myItem.getMyItem();
Go to the preview tab to view the result.
By using the AddScriptReference method, you can gain access to .NET or custom namespaces. This is only necessary if you need a reference, such as System.Data.dll, that is not initialized in the project before the script runs.
To access a namespace in Visual Basic.NET
Private Sub runReport()
Dim rpt as new YourReportName
rpt.AddScriptReference("System.Data.dll")
rpt.Run()
End Sub
To access a namespace in C#
private void runReport()
{
YourReportName rpt = new YourReportName;
rpt.AddScriptReference("System.Data.dll");
rpt.Run();
}
type=note
Note: If you are using the custom assemblies, they must have Strong Name and registered to GAC folder.
If you want to use custom assemblies in the Script section of the Designer, then you need to add the assembly reference to a report before loading it into the report designer. To do this, follow these steps:
Create a custom assembly with the strong name and register it to GAC.
Create a new sample with a section report (XML-based).
Add the report designer to the form.
Add the following code to the Form_Load event.
To access a namespace in Visual Basic.NET
Dim rpt As SectionReport = New SectionReport()Dim xtr As System.Xml.XmlTextReader = New System.Xml.XmlTextReader("..\..\SectionReport2.rpx") rpt.LoadLayout(xtr) xtr.Close() rpt.AddScriptReference("ClassLibrary1.dll") designer1.Report = rpt
To access a namespace in C#
SectionReport rpt = new SectionReport(); System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(@"..\..\SectionReport2.rpx"); rpt.LoadLayout(xtr); xtr.Close(); rpt.AddScriptReference(@"ClassLibrary1.dll"); designer1.Report = rpt;
Run the sample.
Open the Script section of the designer.
You can now use the custom assemblies reference in the Script section of the Desginer.
Using the AddCode method in the Code View of the Form, you can add code into the script. The AddCode method allows you to add actual code segments to the script at run time. This is useful for allowing secure information, such as a database connection string or SQL query string, to be used inside the script without saving it in the RPX file.
Go to the Code View of your report and add a public function like the following:
To add code in Visual Basic.NET
Public Function addThisCode() As String
Dim sCode As String = "Public Function ShowACMessage() As String" + Environment.NewLine + "ShowACMessage = ""my Added Code""" + Environment.NewLine + "End Function"
addThisCode = sCode
End Function
To add code in C#
public string addThisCode()
{
string sCode = "public string ShowACMessage(){return \"my Added Code\";}";
return sCode;
}
In the design view of your report double-click the gray area around the design surface to create an event-handling method for the ReportStart event.
Add the following code to the handler:
To access the class in Visual Basic.NET
Me.AddCode(addThisCode())
To access the class in C#
this.AddCode(addThisCode());
Go to the script tab and drop down the Object list to select Detail. This populates the Event drop-down list with section events.
Drop down the Event list and select Format. This creates script stubs for the event.
Add the following script to the event:
To write the script in Visual Basic.NET
Me.TextBox1.Text = ShowACMessage()
Or
CType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = ShowACMessage()
To write the script in C#
this.textBox1.Text = ShowACMessage();
Or
((TextBox)rpt.Sections["detail"].Controls["textBox1"]).Text = ShowACMessage();
If the script requires a method to be called, you can construct a class inside the script.
Go to the script tab and add the following code at the top:
To create a class inside the script in VB.NET script
Public Class MyFuncs
Public Sub New()
End Sub
Public Function ShowMyString() As String
Return "This is my string"
End Function
End Class
To create a class inside the script in C#
public class MyFuncs
{
public MyFuncs()
{
}
public string ShowMyString()
{
return "This is my string";
}
}
On the script tab, now drop down the Object list and select Detail. This populates the Event drop-down list with section events.
Drop down the Event list and select Format. This creates script stubs for the event.
Add the following script to the event:
To create a class inside the script in VB.NET script
Dim f As MyFuncs = New MyFuncs()
Me.TextBox1.Text = f.ShowMyString
Or
Dim f As MyFuncs = New MyFuncs()
CType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = f.ShowMyString
To create a class inside the script in C#
MyFuncs f = new MyFuncs();
this.textBox1.Text = f.ShowMyString();
Or
MyFuncs f = new MyFuncs();
((TextBox)rpt.Sections["detail"].Controls["textBox1"]).Text = f.ShowMyString();
type=note
Note: Use the examples with the "this" (C#) and "Me"(Visual Basic.NET) keywords, as they are recommended rather than the ones with "rpt".