0

Below is code from VB.NET script to set DataSource in RS.exe script file. Could you please help me to convert it into C#?

The whole code is as below.

Dim dataSources(0) As DataSource
Dim dsr0 As New DataSourceReference
dsr0.Reference = "/Aroh/Data Sources/VIPDataSource"
Dim ds0 As New DataSource
ds0.Item = CType(dsr0, DataSourceDefinitionOrReference) ' <=== This line here
ds0.Name = "DataSource1"
dataSources(0) = ds0
ds.SetItemDataSources("/Aroh/TestMe", dataSources)

I tried to convert using ds0.Item = (DataSourceDefinitionOrReference)dsr0 but that gives me an error.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
  • Please add appropriate tags to your question, assuming you are working with RS, this may help:http://phil-austin.blogspot.com/2009/02/deploying-reporting-services.html – NoChance Jan 06 '15 at 01:09

2 Answers2

2

Either

ds0.Item = (DataSourceDefinitionOrReference)dsr0; 

or

ds0.Item = dsr0 as DataSourceDefinitionOrReference; 

Should be used

Eckd
  • 358
  • 1
  • 10
  • Please note that the C# casting operator (parenthesis) is stricter than CType(). There is no exact equivalent in C# to either DirectCast (which is stricter than C# casting) or CType. This will only make a difference on fringe cases however, but it might happen that a direct conversion might not work. – Drunken Code Monkey Jan 06 '15 at 02:26
0

In cases where direct conversion is not possible, you can add a factory method or constructor overload to the DataSourceDefinitionOrReference class that takes in a DataSourceReference and returns a properly constructed instance.

Drunken Code Monkey
  • 1,796
  • 1
  • 14
  • 18