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

Access Connection string from web.config file in reportLibrary Project

10 Answers 295 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
rajesh kumar
Top achievements
Rank 1
rajesh kumar asked on 09 Dec 2009, 08:55 AM
Hi,

I am using telerik for reporting in my web application. I have 2 projects
1. ReportLibrary
2. Web application

I have my connection string in web.config file of my web application project.
As I have used reporing wizard to create my reports.....it saved new connection string in app.config file.
I want to use the same connection string which is there in the web.config file.

So How do we do that?
any answer would be appreciated.

10 Answers, 1 is accepted

Sort by
0
sofi sofi
Top achievements
Rank 1
answered on 09 Dec 2009, 10:59 AM
Hi, Rajesh! May be ths will help you:

DatabaseDataSetTableAdapter1.Connection.ConnectionString =  

WebConfigurationManager

 

.ConnectionStrings["MyConnectionStringName"].ConnectionString;

 

 

 

 

0
Muhammad
Top achievements
Rank 1
answered on 08 Aug 2012, 11:28 AM
Hi,

I have the same problem, can anybody from Telerik help?

Thanks
Awais
0
Steve
Telerik team
answered on 08 Aug 2012, 11:34 AM
Hello Awais,

You'll have to copy the connectionString from the app.config file of the class library to the web.config of the web application project in order to use it.

Kind regards,
Steve
the Telerik team

BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >

0
Muhammad
Top achievements
Rank 1
answered on 08 Aug 2012, 11:46 AM
Thanks for the reply, actually our web application uses connection-string in the <appsettings> like given below:

web-application(web.config)

--
  <appSettings>
    <!--Connection strings-->
    <add key="Main.Connectionstring" value="Persist Security Info=True;User ID={0};Password={1};Initial Catalog={2};Data Source={3}" />
    <add key="DB" value="datastore2" />
    <add key="SERVER" value="machine89\sql2008" />
    <add key="USER" value="admin" />
    <add key="PASSWORD" value="zFxRTQ6vbj+KRJVVbJANuQ==" />
    <!--End Connection strings-->
--
Report Library(app.config)
--
<connectionStrings>
    <add name="CS.ReportLibrary.Properties.Settings.TestDatabase"
      connectionString="Data Source=machine89\SQL2008;Initial Catalog=TestDatabase;Persist Security Info=True;User ID=admin;Password=newt0n$sigm@"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
--

How do i move the connection string from app.config and put it in web.config, by making sure i don't have to change the way(format) connection-string is set in the web.config?

Thanks
0
Muhammad
Top achievements
Rank 1
answered on 09 Aug 2012, 11:30 AM
Telerik team,  any help on this would be appreciated?
0
Squall
Top achievements
Rank 1
answered on 13 Aug 2012, 07:19 AM
Have you tried to just copy and paste it. This works perfectly for my configuration files.

SN
0
Muhammad
Top achievements
Rank 1
answered on 13 Aug 2012, 08:59 AM
Well it works if i copy the connection string from Report Library to my web application. But i actually do not want to change the way i have the connection string setup in my web application. I want to keep the format, just change the name of the connection string.
0
Peter
Telerik team
answered on 13 Aug 2012, 04:30 PM
Hello Muhammad,

Our suggestion is to use the ConfigurationManager class to read your configuration settings and to assembly a connection string according to the .NET provider requirements. Then in the report's constructor you can set the connection string to the SqlDataSource component instances.

However if you prefer to set the connection string from the client application check out the following code snippet:

static Report GetReport()
    {
        Report report = //retrieve and deserialize the report definition
        //Set the new connection string to all data items
        SetConnectionString(connectionString, report);
     
        return report;
    }
     
    static void SetConnectionString(string connectionString, ReportItemBase reportItemBase)
    {
        if (reportItemBase is Report)
        {
            var report = (Report)reportItemBase;
     
            if (report.DataSource is SqlDataSource)
            {
                var sqlDataSource = (SqlDataSource)report.DataSource;
                sqlDataSource.ConnectionString = connectionString;
            }
            foreach (var parameter in report.ReportParameters)
            {
                if (parameter.AvailableValues.DataSource is SqlDataSource)
                {
                    var sqlDataSource = (SqlDataSource)parameter.AvailableValues.DataSource;
                    sqlDataSource.ConnectionString = connectionString;
                }
            }
        }
     
        foreach (var item in reportItemBase.Items)
        {
            //recursively set the connection string to the items from the Items collection
            SetConnectionString(connectionString, item);
     
            //Covers Crosstab, Table and List data items
            if (item is Table)
            {
                var table = (Table)item;
                if (table.DataSource is SqlDataSource)
                {
                    var sqlDataSource = (SqlDataSource)table.DataSource;
                    sqlDataSource.ConnectionString = connectionString;
                    continue;
                }
            }
            if (item is Chart)
            {
                var chart = (Chart)item;
                if (chart.DataSource is SqlDataSource)
                {
                    var sqlDataSource = (SqlDataSource)chart.DataSource;
                    sqlDataSource.ConnectionString = connectionString;
                    continue;
                }
            }
            if (item is SubReport)
            {
                var subReport = (SubReport)item;
                if (subReport.ReportSource.DataSource is SqlDataSource)
                {
                    var sqlDataSource = (SqlDataSource)subReport.ReportSource.DataSource;
                    sqlDataSource.ConnectionString = connectionString;
                    continue;
                }
            }
        }
    }
}
Regards,
Peter
the Telerik team

BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >

0
Bryan Hughes
Top achievements
Rank 1
answered on 14 Aug 2012, 06:41 PM
"Our suggestion is to use the ConfigurationManager class to read your configuration settings and to assembly a connection string according to the .NET provider requirements. Then in the report's constructor you can set the connection string to the SqlDataSource component instances."

Do you have code samples to accomplish this?  VB.NET is preferred.  Just getting my legs in C#.  I have my report library created in separate project.  The website has not been converted to web app (inherited project).  I need to have the connection string work for local dev machine and use web site web.config connection string in live site.  Can you point me in the right direction for code examples to accomplish this?

Bryan.
0
Peter
Telerik team
answered on 17 Aug 2012, 10:26 AM
Hi Bryan,

If you don't keep the connection string information in the application settings (Properties/Settings.settings), our suggestion is to copy the app.config connection strings to the web.config connectionStrings section with the necessary changes for the live site. The only part of the connection string that should stay the same is its name. 

Greetings,
Peter
the Telerik team

BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >

Tags
General Discussions
Asked by
rajesh kumar
Top achievements
Rank 1
Answers by
sofi sofi
Top achievements
Rank 1
Muhammad
Top achievements
Rank 1
Steve
Telerik team
Squall
Top achievements
Rank 1
Peter
Telerik team
Bryan Hughes
Top achievements
Rank 1
Share this question
or