telerik-reporting

Telerik Reporting Error: sqlDataSource does not exist in the current context


I am trying to create a SqlDataSource using Telerik Reporting and am getting errors using the exact code in the documentation attempting to connect to AdventureWorks database:

    SqlDataSource sqlDataSource = new SqlDataSource();
    sqlDataSource.ProviderName = "System.Data.SqlClient";
    sqlDataSource.ConnectionString = "Data Source=(local)\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True";
    sqlDataSource.SelectCommand = "SELECT * FROM Production.Product";

enter image description here


Solution

  • The code is placed inside of the body of the class, but it should be placed in a method instead. Use object initializer like this:

    SqlDataSource sqlDataSource = new SqlDataSource
    {
        ProviderName = "System.Data.SqlClient",
        ConnectionString = "Data Source=(local)\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True",
        SelectCommand = "SELECT * FROM Production.Product"
    }
    

    or move the initialization code in a separate method.