Problem when trying to preview reports

4 posts, 1 answers
  1. Ashraf Sabry
    Ashraf Sabry avatar
    88 posts
    Member since:
    Jan 2011

    Posted 18 Aug 2014 Link to this post

    Greetings,

    I'm using Telerik Reporting version 8.1.14.804 (Q2 2014 SP1) in an ASP.Net Web Forms application, inside Visual Studio 2012 (Ultimate 2012 Version 11.0.61030.00  Update 4).
    I created a simple report that fetches data from an entity data source that uses a DbContext class in the project.

    The report works as expected, but in design time, when I try to preview it, an error message is displayed saying:
    An error has occurred while processing Table 'table1': An error occurred while
    invoking data retrieval method. Try restarting Visual Studio. -------------
    InnerException ------------- No connection string named 'DbContext' could be
    found in the application config file.


    Where DbContext is the name of my code first context class.

    I uploaded a demo web application that demonstrates the problem here:
    http://www.wikifortio.com/825288/ReportsSample.zip
    I removed the bin directory but Nuget should settle this.

    How to resolve this issue?
  2. Answer
    Stef
    Admin
    Stef avatar
    3036 posts

    Posted 20 Aug 2014 Link to this post

    Hi Ashraf,

    In the DbContext you need to add a second constructor accepting a string parameter, which will be used by the EntityDataSource component to pass the connection string from the component configuration.
    namespace TelerikReportsPlayground.Data
    {
        using System;
        using System.Data.Entity;
        using System.ComponentModel.DataAnnotations.Schema;
        using System.Linq;
     
        public partial class DbContext : System.Data.Entity.DbContext
        {
            public DbContext()
                : base("name=DbContext")
            {
            }
     
            public DbContext(string connstr)
                : base(connstr)
            {
            }
     
            public virtual DbSet<Department> Departments { get; set; }
     
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
            }
        }
    }


    For more details please go through the EntityDataSource Component overview article.


    Regards,
    Stef
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft banner
  4. Ashraf Sabry
    Ashraf Sabry avatar
    88 posts
    Member since:
    Jan 2011

    Posted 21 Aug 2014 in reply to Stef Link to this post

    This resolves the problem. Thank you.

    But I have a question: The documentation page you pointed me to says "If Code First is used there is no need for a constructor with string parameter" and I use code first in my project. I wonder what's the need for a connection string? Why not the designer just instantiate the DbContext and fetch data from it? Specially that the report works at run time without providing a connection string.
  5. Stef
    Admin
    Stef avatar
    3036 posts

    Posted 26 Aug 2014 Link to this post

    Hello Ashraf,

    If you set your model as follows you can omit the second constructor, and specify the already existing model's connection string by name:
    <!-- connectin string saved during creating the data model -->
     <connectionStrings>
        <add name="DbContext" connectionString="data source=.\SQLEXPRESS;initial catalog=AdventureWorks;Integrated Security=SSPI;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
      </connectionStrings>
    ......................
    //the model settings
    namespace TelerikReportsPlayground.Data
    {
        using System;
        using System.Data.Entity;
        using System.ComponentModel.DataAnnotations.Schema;
        using System.Linq;
     
        public partial class DbContext : System.Data.Entity.DbContext
        {
            public DbContext()
                : base("DbContext")
            {
            }     
    ................................
    //the EntityDataSource component settings
                //
                // entityDataSource1
                //
                this.entityDataSource1.ConnectionString = "DbContext";
                this.entityDataSource1.Context = typeof(TelerikReportsPlayground.Data.DbContext);
                this.entityDataSource1.ContextMember = "Departments";
                this.entityDataSource1.Name = "entityDataSource1";
            public virtual DbSet<Department> Departments { get; set; }
     
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
            }
        }
    }



    Regards,
    Stef
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top