This is a migrated thread and some comments may be shown as answers.

LINQ in an object data source

2 Answers 213 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ron
Top achievements
Rank 1
Ron asked on 10 Aug 2011, 02:03 AM
I'm very new to telerik reporting, I'm sure I'm missing something;

I have a simple report that has a text box
I have the following classes in the report.cs
class DealerDataSource
{
    public IEnumerable<CalDealer> GetDealerInfo()
    {
        DAdbDataContext xxx = new DAdbDataContext();
 
   //     Dealer deal = db.Dealers.First(d => d.DealerId == 2);
        CalDealer cd = new CalDealer();
        cd.Title = "Test";
        yield return cd;
    }
}
 
public class CalDealer
{
    public string Title { get; set; }
}

 

 

When I add the data context and go to preview the report it gives the error of
"object reference not set to an instance of an object". 
It compiles OK -

If I take out DAdbDataContext xxx = new DAdbDataContext(); the report previews fine.


I know the data context is good as I'm using it in the code behind of a page.

 

 

 Thanks!!

Ron

 


2 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 10 Aug 2011, 04:06 PM
Hi Ron,

Check out the attached sample project that illustrates how to utilize LINQ to SQL with an ObjectDataSource component.

However have in mind that in the case of report designer/preview the current application is devenv.exe so it looks for the connectionString in the devenv.exe.config file (for more info refer to Using connectionStrings from configuration file KB article). SqlDataSource works, because we have a design time support, while if you are using external DAL they may not be able to resolve the ConnectionString for the devenv.exe runtime. In order to avoid this behavior you have several options:

  1. Hard code the ConnectionString;
  2. use a helper method as the following one:
    public static string ResolveConnectionString()
    {
        // Retrieve the connection string settings from the configuration file.
        ConnectionStringSettings connectionSettings = ConfigurationManager.ConnectionStrings["MyConnectionString"];
     
        // If a valid connection string setting exist, then we are at run-time,
        // so we should use this connection string to connect to the database.
        if(null!= connectionSettings)
        {
            return connectionSettings.ConnectionString;
        }
     
        // If no valid connection string setting exist, then we are at design-time,
        // so return a default connection string for preview and testing purposes.
     
        return "Data Source=...;Initial Catalog=...";
    }
  3.    
  4. simply use an actual application to view your reports in runtime instead of the Previews.

Regards,
Peter
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Ron
Top achievements
Rank 1
answered on 10 Aug 2011, 05:46 PM
Thanks!!!!!

I just simply hard coded the connect string and it works like a champ for my purposes.  I appreciate the explanation and help
Cheers

Ron


Tags
General Discussions
Asked by
Ron
Top achievements
Rank 1
Answers by
Peter
Telerik team
Ron
Top achievements
Rank 1
Share this question
or