CSharpNewBee
CSharpNewBee

Reputation: 1981

Custom c# code in a Report

I am populating a report using an Object. Everything works fine, if my members are string, however, I have a few members, which are List<string>.

When I add these fields to the report (in designer, visual studio 2012) and run the report, they show as

'#Error'

Viewing the properties of the report, I can see that there is a 'custom code' tab, for which I've entered the following method to convert the list of strings into a array of string.

public string[] GetListItems(List<string> intList)
{
    var s = intList.ToArray();
    return s;
}

Then when I try and replace the field with an expression and enter the following:

=Join(Code.GetListItems(Fields!Aka.Value),",")

and then run the report, VS fails to build with the following:

Error 2 The Value expression for the textrun ‘Aka.Paragraphs[0].TextRuns[0]’ contains an error: [BC30456] 'GetListItems' is not a member of 'ReportExprHostImpl.CustomCodeProxy'.

Error 1 There is an error on line 0 of custom code: [BC30183] Keyword is not valid as an identifier.

Can anyone shed any light why this is?

Upvotes: 2

Views: 2373

Answers (3)

Tyler
Tyler

Reputation: 986

For those who can't create go through the difficulties of implementing a third-party DLL. A simpler; but, a messier solution is as follows.

To summarize solution, you can create properties that returns a value that the reports can easily work with.

In my scenario, I had 2 classes:

public class Parent {
    public Child { get; set; }
}

public class Child {
    public string Name { get; set; }
}

The type that I was binding to my Report was Parent; however, I needed to access the Name property inside Child. In otherwords I needed to do:

Parent.Child.Name

This where I ran into the same problem that the OP ran into. Due to the scope of my project (a college project), I can't bother with third-party dlls or writing my own dll...

So what I did is add more properties to the Parent class.

public class Parent {
    public Child { get; set; }
    public string ChildName { get => Child.Name; }
}

public class Child {
    public string Name { get; set; }
}

By adding an additional property to Parent that has an type that Reports can work with, I can now easily access the Child's Name through my Parentclass.

Of course this is messy; but, I'm in a situation where I can't go with the more complicated/time consuming dlls solution.

I just hope my instructors will allow this solution without penalty...

Upvotes: 0

sdrzymala
sdrzymala

Reputation: 387

That is C# code. Directly in the report you can only use Visual Basic. C# is avaliable as an external DLL.

Upvotes: 4

Nitin Singh Parmar
Nitin Singh Parmar

Reputation: 1

If you just created a report and showing an error code like BC30016 labels are not valid outside of the method. The reson in my case was that i have insert the random numerical value n the code property of a report1.rdlc Hence to solve this type of error, goto report properties and remove the value from the code.

Upvotes: -1

Related Questions