Changing DataSource with C#


  • So I'm trying to run through a directory of reports and change the data source of every report from whatever it has now, to a shared data source.  I'm using C#.

    If I understand the process correctly, I want to .GetItemDataSources on the reports service and store it in an array of datasources.  Then I want to Change the name and reference of the datasource to match something that I alredy have elsewhere.  Here is the code...

    foreach (CatalogItem item in _reportsService.ListChildren("/" + reportingServicesRoot, true))
                                if (item.Type == ItemTypeEnum.Report)
                                        DataSource[] Sources = new DataSource[1];
                                        Sources = _reportsService.GetItemDataSources(item.Path);
                                        DataSourceDefinitionOrReference Reference = _reportsService.GetDataSourceContents(@"/Data Sources/" + reportingServicesRoot);
          Sources[0].Item = Reference;
                                        Sources[0].Name = reportingServicesRoot;
                                        _reportsService.SetItemDataSources(item.Path, Sources);

                                    catch (Exception ex)
                                        DataSource[] s = _reportsService.GetItemDataSources(item.Path);
                                        throw ex;

    Here is the error I get....

    System.Web.Services.Protocols.SoapException: The data source 'SummerOf70' cannot be found. ---> Microsoft.ReportingServices.Diagnostics.Utilities.DataSourceNotFoundException: The data source 'SummerOf70' cannot be found. at Microsoft.ReportingServices.DataExtensions.DataSourceInfoCollection.CombineOnSetDataSources(DataSourceInfoCollection newDataSources) at Microsoft.ReportingServices.Library.SetItemDataSourcesAction.SetReportDataSources(CatalogItem item, DataSource[] dataSources) at Microsoft.ReportingServices.Library.SetItemDataSourcesAction.PerformActionNow() at Microsoft.ReportingServices.Library.RSSoapAction`1.Execute() at Microsoft.ReportingServices.WebServer.ReportingService2005Impl.SetItemDataSources(String Item, DataSource[] DataSources) --- End of inner exception stack trace --- at Microsoft.ReportingServices.WebServer.ReportingService2005Impl.SetItemDataSources(String Item, DataSource[] DataSources) at Microsoft.ReportingServices.WebServer.ReportingService2005.SetItemDataSources(String Item, DataSource[] DataSources)

    I know the DataSource is there.

    Am I going about this all wrong?  Should I delete the datasource first and then create a new one?

    I look forward to clarifying my poor grammar.  Thanks in advance!

    Tuesday, May 06, 2008 3:43 PM


  • Thanks to a coworker, I report the following...

    I cannot change Sources[0].Name the way I am above.  I can only change the name in the .rdl file.  The error I get that says the datasource cannot be found is because the name of the datasource in the .rdl file is not the name of the datasource I am trying to save.  Repeat: "The data source 'SummerOf70' cannot be found" is refering to the datasource name listed in the .rdl file.  I am leaving the name in the .rdl file the way it is.  It will still work.

    Here is the fix...

                                        DataSourceReference dsRef = new DataSourceReference();
                                        dsRef.Reference = @"/Data Sources/SummerOf70";
                                        Sources[0].Item = dsRef;
                                        _reportsService.SetItemDataSources(item.Path, Sources);

    It's hardcoded, of course, but this is it.  Just change the .Item to be a reference rather than a definition and away we go.
    Wednesday, May 07, 2008 3:12 PM