Articles in this section
Category / Section

How to Save and Load Report from Database using “BeforeSaveReport” and “BeforeLoadReport” events

1 min read

The BeforeSaveReport and BeforeLoadReport events has been used to modify the already existing save and load report methods in OlapClient. The reports created in the OlapClient can be saved and retrieved from the database using save report and load report button in the OlapClient Tool Bar. This can be achieved by handling “BeforeSaveReport” and “BeforeLoadReport” events. A WCF service needs to be created in the Web Project which helps to connect with the Database.

 

Steps to create a service for saving and loading the report to database

 

The following are the steps to service for saving and loading the report in Database.

  1. Create a WCF service in the Web Project namely ReportService.svc.
  2. The following codes need to be added in the interface of the created service. (here IReportService.cs)

 

C#

 
 
[ServiceContract]
    public interface IReportService
    {
        [OperationContract]
        void DoWork();
        [OperationContract]
        void DoSaveProcess(string reportName, byte[] reportStream);
        [OperationContract]
        List<string> DoLoadProcess();
        [OperationContract]
        Stream DoLoadStream(string reportName);
        [OperationContract]
        Stream GetReportAsStream(OlapDataManager dataManager);
    }

 

  1. This interface should be implemented in the ReportService.svc.cs. Please refer the following codes.

C#

 
 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class ReportService : IReportService
    {
        SqlCeConnection _con;
        public void DoWork()
        {
            _con = new SqlCeConnection();
            _con.ConnectionString = @"Data Source = C:\Users\niranchana\AppData\Local\Syncfusion\EssentialStudio\12.1.0.43\Common\Data\OfflineCube\ReportsTable.sdf; Persist Security Info = False;";
          
        }
 
 
        public void DoSaveProcess(string reportName, byte[] reportStream)
        {
            _con = new SqlCeConnection();
            _con.ConnectionString = @"Data Source = C:\Users\<user name>\AppData\Local\Syncfusion\EssentialStudio\<version number>\Common\Data\OfflineCube\ReportsTable.sdf; Persist Security Info = False;";
          
            _con.Open();
            SqlCeCommand cmd1 = new SqlCeCommand("insert into ReportsTable Values(@ReportName,@Report)", _con);
            cmd1.Parameters.Add("@ReportName", reportName);
            cmd1.Parameters.Add("@Report", reportStream.ToArray());
            cmd1.ExecuteNonQuery();
            _con.Close();
        }
 
        public List<string> DoLoadProcess()
        {
            List<string> names = new List<string>();
            try
            {
                _con = new SqlCeConnection();
                _con.ConnectionString = @"Data Source = C:\Users\<user name>\AppData\Local\Syncfusion\EssentialStudio\<version number>\Common\Data\OfflineCube\ReportsTable.sdf; Persist Security Info = False;";
          
                _con.Open();
                SqlCeDataAdapter da = new SqlCeDataAdapter("Select * from ReportsTable", _con);
                DataSet dSet = new DataSet();
                da.Fill(dSet);
                DataTable table = dSet.Tables[0];
                foreach (DataRow row in table.Rows)
                {
                    names.Add(row.ItemArray[0] as string);
                }
                _con.Close();
            }
            catch (Exception e)
            {
               
            }
 
            return names;
        }
 
 
 
        public System.IO.Stream DoLoadStream(string reportName)
        {
            string reportname = reportName as string;
            Stream reportStream = null;
            _con = new SqlCeConnection();
            _con.ConnectionString = @"Data Source = C:\Users\user name>\AppData\Local\Syncfusion\EssentialStudio\<version number>\Common\Data\OfflineCube\ReportsTable.sdf; Persist Security Info = False;";
          
            _con.Open();
 
            SqlCeDataAdapter da = new SqlCeDataAdapter("Select * from ReportsTable", _con);
            DataSet dSet = new DataSet();
            da.Fill(dSet);
            DataTable table = dSet.Tables[0];
 
            foreach (DataRow row in table.Rows)
            {
                if ((row.ItemArray[0] as string).Equals(reportname))
                {
                    reportStream = new MemoryStream(row.ItemArray[1] as byte[]);
                    break;
                }
            }
            _con.Close();
            return reportStream;
        }
    }

 

 

Steps to save and load report in OlapClient

The saving and loading the OlapReport from database using the OlapClient Tool Bar buttons has been done with the help of “BeforeSaveReport” and “BeforeLoadReport” events.

This can be achieved by using following steps.

  1. Create the service reference in the Silverlight project for accessing the service which we created for connecting to database.
  2. The following code snippet should be added in Mainpage.cs of OlapClient Silverlight project.

C#

 
 
ReportServiceClient reportService;
        #region Constructor
        /// <summary>
        /// Initialize the MainPage class
        /// </summary>
        public MainPage()
        {
            InitializeComponent();
            InitializeReportService();
            this.DataContext = new ViewModel.ViewModel();
            this.olapClient.BeforeSaveReport += olapClient_BeforeSaveReport;
            this.olapClient.BeforeLoadReport += olapClient_BeforeLoadReport;
        }
 
        #endregion
 
        private void InitializeReportService()
        {
            reportService = new ReportServiceClient();
            reportService.DoWorkAsync();
        }
 
        ComboBox comboload = new ComboBox();
 
        void olapClient_BeforeLoadReport(Syncfusion.Silverlight.Client.Olap.RaiseBeforeLoadReportEventArgs e)
        {
            reportService.DoLoadProcessAsync();
            reportService.DoLoadProcessCompleted += reportService_DoLoadProcessCompleted;
            this.stackproperties.Children.Add(comboload);
            comboload.SelectionChanged += comboload_SelectionChanged;
        }
        void comboload_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox combo = sender as ComboBox;
            reportService.DoLoadStreamAsync(combo.SelectedItem.ToString());
            reportService.DoLoadStreamCompleted += reportService_DoLoadStreamCompleted;
        }
 
        void reportService_DoLoadStreamCompleted(object sender, DoLoadStreamCompletedEventArgs e)
        {
           TextReader strReader = null;
           try
           {
               byte[] reportBytes = e.Result;
               Encoding encoding = new UTF8Encoding(true, true);
               string reportString = encoding.GetString(reportBytes, 0, reportBytes.Length);
               reportString = reportString.Replace('\0', ' ');
               strReader = new StringReader(reportString);
               OlapReportCollection reportCollection = new OlapReportCollection();
               XmlSerializer xmlSerializer = new XmlSerializer(reportCollection.GetType());
               reportCollection = xmlSerializer.Deserialize(strReader) as OlapReportCollection;
               this.olapClient.OlapDataManager.ReportList = reportCollection;
               if (this.olapClient.OlapDataManager.ReportList.Count > 0)
               {
                   this.olapClient.OlapDataManager.SetCurrentReport(reportCollection[0]);
                   this.olapClient.ReportList.ItemsSource = reportCollection;
                   this.olapClient.ReportList.SelectedIndex = 0;
               }
               this.olapClient.OlapDataManager.CurrentReport.ChartSettings.ChartColorPalette = reportCollection[0].ChartSettings.ChartColorPalette;
               this.olapClient.OlapDataManager.CurrentReport.ChartSettings.ChartType = reportCollection[0].ChartSettings.ChartType;
               this.olapClient.OlapDataManager.CurrentReport.GridSettings = reportCollection[0].GridSettings;
           }
           catch (Exception ex) { }       
        }
        private string[] _reportNames;
        public string[] ReportNames
        {
            get
            {               
                return _reportNames;
            }
            set
            {              
                _reportNames = value;
            }
        }
 
        void reportService_DoLoadProcessCompleted(object sender, DoLoadProcessCompletedEventArgs e)
        {
            this.ReportNames = e.Result;
            comboload.ItemsSource = this.ReportNames;
        }
        void olapClient_BeforeSaveReport(Syncfusion.Silverlight.Client.Olap.RaiseBeforSaveReportEventArgs e)
        {
            byte[] tempBuffer = null;
            TextWriter strWriter = null;
            try
            {
                this.olapClient.OlapDataManager.CurrentReport.ChartSettings = this.olapClient.OlapChart.ChartSettings;
                this.olapClient.OlapDataManager.CurrentReport.GridSettings = this.olapClient.OlapGrid.GridSettings;
                OlapReportCollection reportCollection = new OlapReportCollection();
                foreach (var item in this.olapClient.OlapDataManager.ReportList)
                {
                    reportCollection.Add(item);
                }
                
                strWriter = new StringWriter();
                //Getting report stream/string through XmlSerializer
                XmlSerializer xmlSerializer = new XmlSerializer(reportCollection.GetType());
                System.Xml.Serialization.XmlSerializerNamespaces xs = new XmlSerializerNamespaces();
                xs.Add(string.Empty, string.Empty);
                xmlSerializer.Serialize(strWriter, reportCollection, xs);
                Encoding encoding = new UTF8Encoding(true, true);
                tempBuffer = encoding.GetBytes(strWriter.ToString());
 
                string reportName = this.olapClient.OlapDataManager.CurrentReport.Name;
                reportService.DoSaveProcessAsync(reportName, tempBuffer);
                reportService.DoSaveProcessCompleted += reportService_DoSaveProcessCompleted;
            }
            catch(Exception ex) { }
        }
 
        void reportService_DoSaveProcessCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Saved! in database successfully");
        }
 
 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied