Quantcast
Channel: SCN : Discussion List - SAP Crystal Reports, version for Visual Studio
Viewing all articles
Browse latest Browse all 3636

WPF Binding Crystal Reports using MVVM, Null Reference Exception

$
0
0

Hi,

I have a problem hanging with it,

binding crystal report, using MVVM,

The problem is as follows:

I have a user control holding crystalreportsviewer using DependencyProperty

local:ReportSourceChanger.ReportSource="{Binding DataContext.ReportSource, RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}, Mode=FindAncestor}}"

----------------------------

ReportSourceChanger contains

public static class ReportSourceChanger
     {  
         public static readonly DependencyProperty ReportSourceProperty =
             DependencyProperty.RegisterAttached(
             "ReportSource",
             typeof( ReportDocument),
             typeof(ReportSourceChanger),
            new PropertyMetadata(ReportSourceChanged));

         private static void ReportSourceChanged(
             DependencyObject dp,
             DependencyPropertyChangedEventArgs e)
         {          
          var crv = dp as CrystalReportsViewer;             
              if (crv != null && e.NewValue != null)
              {
                  crv.ViewerCore.ReportSource = e.NewValue;       }    }
         public static void SetReportSource(DependencyObject target, object value)
         {
                target.SetValue(ReportSourceProperty, value);     }
         public static object GetReportSource(DependencyObject target)
         {
             return target.GetValue(ReportSourceProperty);       }}

I have a MainWindow handles the user control

<DataTemplate DataType="{x:Type vm:ReportViewModel}" >
         <vw:UCReportViewer  />
     </DataTemplate>

I have e view model named ReportViewModel that handles ReportSource property,
that loads the ReportDocument as follows:

public ReportDocument ReportSource
         {
             get
             {
                 ReportDocument rd = new ReportDocument();
                 rd.Load(Path);
                     rd.SetDataSource(_reportRepository.LoadEmployeesItems().ToList());
                  return  rd;     }}

In the MainWindow I have buttons that handle commands to create tabs as new user controls
at the first tab created, it works perfectly if I leave the tab opened with the report binded,
and create another tab with new different report it shows me null reference exception as follows:

 

    Object reference not set to an instance of an object.  ( nothing else )

If I close tab an try to open another tab with new usercontrol that handles a report, it works.
The case that doesn't work is only with multiple tabs opened.

 

I think the reason that causes the exception is that the crystal report viewer reference the same source from the first tab,

but I don't know how to resolve in that context.


 

 

The method that creates and shows the tab is as follows:

 

void ShowReport(string reportPath, string name)

        {

ReportViewModel workspace =

                this.Workspaces.FirstOrDefault(vm => vm is ReportViewModel && vm.DisplayName == name)

                as ReportViewModel;

            if (workspace == null )

            {                workspace = new ReportViewModel(reportPath,name, _reportRepository);

                this.Workspaces.Add(workspace);

            }            this.SetActiveWorkspace(workspace);

        }

 

 

 

 

I would be grateful if someone can help me regarding to this issue.

  Thank You


Viewing all articles
Browse latest Browse all 3636

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>