0

I have a eventhandler like this code below :

viewer.LocalReport.SubreportProcessing += new Microsoft.Reporting.WebForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

And this method as a parameter for event handler above :

private static void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e) {

        DateTime movementDate = Convert.ToDateTime(e.Parameters[0].Values[0]);

        TourTransactionsController controller = new TourTransactionsController();

        var movement = controller.Movements();

        List<Movement> movementList = new List<Movement>();
        movementList.Add(new Movement {
            Destination = "TEST",
            MovementDescription = "TEST",
            DateTime = Convert.ToDateTime("2017-09-25")
        });

        e.DataSources.Clear();

        e.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource() {
            Name = "DSMovements",
            Value = movementList
        });

        //throw new NotImplementedException();
    }

Both of those method is written in a WEB API Controller. The eventhandler is hitting while debugging, but after I press F11 (step into) while debugging the LocalReport_SubreportProcessing method is not hitting. Why is LocalReport_SubreportProcessing method not hitting ?

Any help or answer is really appreciated.

Satria Janaka
  • 463
  • 4
  • 15
  • You only `+=` / `add` the event-handler. Are you sure the event is fired from `LocalReport` ? – Blacktempel Sep 12 '17 at 09:24
  • I think because the code is executed when I hit the Web API, the event is fire from Web API. Is that possible to fired a event handler when hitting the web api ? – Satria Janaka Sep 12 '17 at 09:29
  • Sorry it's hard to understand what you want. `The eventhandler is hitting while debugging, but after I press F11 (step into)` - Do you expect that the event-handler is called when you `+=` / `add` it ? – Blacktempel Sep 12 '17 at 09:36
  • Yes. I expect that the event handler is called when I += it. To be honest, I'm not sure that I understand how event handler works. Is my understanding that the event handler is called when I += / add it correct ? – Satria Janaka Sep 12 '17 at 12:51

1 Answers1

0

Event's are not called when you register / add += them.

Events are called when the owning class invokes it.

public class EventTest
{
    void SomeOperation()
    {
        //Do something
    }

    public void Run()
    {
        SomeOperation();
        RunFinished?.Invoke(this, EventArgs.Empty); //Invoke the event, indicating that something has happened or finished
    }

    //The event itself
    public event EventHandler RunFinished;
}

public class EventSubscriber
{
    EventTest _ET = new EventTest();

    public EventSubscriber()
    {
        _ET.RunFinished += ETRunFinished; //Register my method, called when the event occurs (is invoked)
    }

    public void DoSomething()
    {
        _ET.Run();
        Console.WriteLine("Something completed.");
    }

    void ETRunFinished(object sender, EventArgs e)
    {
        Console.WriteLine("My event handler was executed.");
    }
}

Your handler will be called when the event fires.

When registering += or -= unregistering (add / remove) they will NOT be invoked.

For a more detailed tutorial please refer to MSDN.

Blacktempel
  • 3,935
  • 3
  • 29
  • 53
  • Ah, I get it. But what I used as `EventHandler` in my code is `SubreportProcessingEventHandler` from `Microsoft.Reporting.Webforms`. I try to Invoke the `EventHandler` with `viewer.LocalReport.SubreportProcessing?.Invoke(LocalReport_SubreportProcessing)` but I got an error message `The event 'LocalReport.SubreportProcessing' can only appear on the left hand side of += or -=` and `There is no argument given that corresponds to the required formal parameter 'e' of 'SubreportProcessingEventHandler.Invoke(object, SubreportProcessingEventArgs)'` Is there any other way to invoke the `EventHandler` ? – Satria Janaka Sep 13 '17 at 02:33
  • @SatriaJanaka You can only invoke an `event` within the declaring class. Deriving from it won't help you either. It is possible to [invoke it via reflection,](https://stackoverflow.com/questions/198543/how-do-i-raise-an-event-via-reflection-in-net-c) but that's surely not what you want here – Blacktempel Sep 13 '17 at 04:42
  • @SatriaJanaka If you want to call/invoke a method, just do it. Register it on an event to receive a notification when the event occurs and you want to do something. – Blacktempel Sep 13 '17 at 04:43