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

Use web.config for sql connection?

13 Answers 998 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Will
Top achievements
Rank 1
Will asked on 22 Aug 2007, 09:57 PM
Hello

Is there a way to use the sql connection from my web app's web.config rather than always having to code/input the connection string with every report?

thanks
 - will

13 Answers, 1 is accepted

Sort by
0
Chavdar
Telerik team
answered on 23 Aug 2007, 05:16 PM
Hi,

There is no problem to load the connection string from the web.config file. You can use the following pattern:

web.config:
    <connectionStrings>
        <add name="SampleDB" connectionString="Server=(local)\SQLEXPRESS;Integrated     Security=true;Database=AdventureWorks" />
      </connectionStrings>


report.vb:
    Public Sub New()       
        InitializeComponent()

        If ConfigurationManager.ConnectionStrings("
SampleDB") IsNot Nothing Then
            Me.conn.ConnectionString = ConfigurationManager.ConnectionStrings("
SampleDB").ConnectionString
        End If
    End Sub


where Me.conn is the SqlConnection object.

Kind regards,
Chavdar
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Will
Top achievements
Rank 1
answered on 23 Aug 2007, 05:31 PM
Thanks for the help.  This looks like it will solve my problem.
 - will
0
Francisco
Top achievements
Rank 1
answered on 01 Sep 2008, 04:14 PM

Hi,
I use that metod, but visual studio indicates

"  'cnn' is not a member of  'Report1'

I need to Import any namespace?



Public
Sub New()

InitializeComponent()

If ConfigurationManager.ConnectionStrings("cnConnection") IsNot Nothing Then

Me.conn.ConnectionString = ConfigurationManager.ConnectionStrings("cnConnection").ConnectionString

End If

End Sub

0
Steve
Telerik team
answered on 01 Sep 2008, 04:29 PM
Hello Francisco,

You have to either define the conn as local variable or set the datasource of the report in order to have it as member of Report1. I've attached a sample app showing one of the approaches.

All the best,
Steve
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
tony
Top achievements
Rank 1
answered on 11 Oct 2008, 08:10 PM
I need to use connection string from web.config which same as Will,
After I download parameterconnectstring.zip , i can not view content because I am using VS 2005 and use C# , can you please post how this solution works? ( need sample syntax for connection string from web.config)
thanks!!
0
Steve
Telerik team
answered on 13 Oct 2008, 09:02 AM
Hello Tony,

We've created a code library elaborating on this and containing C# code sample. For future reference, please note that you can always use our or any other code converter on the net to convert code.

Sincerely yours,
Steve
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Francisco
Top achievements
Rank 1
answered on 13 Oct 2008, 04:35 PM
HI,

you don't have the exemple to VS2005 ??
0
Steve
Telerik team
answered on 14 Oct 2008, 07:33 AM
Hi Francisco,

The Visual Studio version is not relevant in this case, but in fact the attached project is for VS2005. Please review the explanation and code provided to understand how it works.

Kind regards,
Steve
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
tony
Top achievements
Rank 1
answered on 14 Oct 2008, 12:39 PM
Thanks for example. I follow this example and still have problem for a very simple report. If i add on below code:

InitializeComponent();
// add below 2 line for use connection from web.config
this.myDataSetTableAdapter1.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

 

 

this.myDataSetTableAdapter1.Fill(this.myDataSet.myDataSetTable);


After add above last 2 line for try to use connection string from web.config ,
when i click preview or HTML preview , then this simple report will display
no any data except heading. (it was good if i remove last 2 line)

Can you please advice and help? I already read menu & samples again and again
but did not solve this 'using web.config for connection' for my simple report.   

 

0
Steve
Telerik team
answered on 14 Oct 2008, 01:27 PM
Hi Tony,

Have you tried debugging, so that you could see that the ConnectionString is properly overridden and that you have data in your DataSet? Please try isolating the issue as much as possible and provide more info, as "not working" does not help us much in identifying the problem.

Greetings,
Steve
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
tony
Top achievements
Rank 1
answered on 14 Oct 2008, 02:51 PM
It finally solved. thanks for your support!
Just in case someone else need this solutions as below:

...

using

System.Configuration;

 

 

using System.Data.SqlClient;
...

try
{

 

ConnectionStringSettings

connSettings = ConfigurationManager.ConnectionStrings["myConnectionString"];

 

 

if ((connSettings != null) && (connSettings.ConnectionString != null))

 

{

 

this.ds1TableAdapter1.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

 

}

 

this.ds1TableAdapter1.Fill(this.ds1.dsWip1Table);

 

(above line  may different wheich already exist before add from web.config)


*** really appreciate tech support's help

0
Francisco
Top achievements
Rank 1
answered on 14 Oct 2008, 02:58 PM

I, can solve to.

to VB
need to reference the System.Configuration on referecies

 

Imports

System.Data.SqlClient
Imports System.Configuration

 

 

 

 

Public Sub New()

 

InitializeComponent()

 

 

Dim

sqlconnection1 As New SqlConnection()
Dim sqlCOmmand1 As New SqlCommand
Dim sqldataadaptater As New SqlDataAdapter

 

 

 

sqlconnection1.ConnectionString = ConfigurationManager.ConnectionStrings(0).ConnectionString

sqlconnection1.ConnectionString = ConfigurationManager.ConnectionStrings(

"ConnectiosONWebINI").ConnectionString

 

 

 sqlCOmmand1.CommandText =

"SELECT * from companys"

 

 

 sqlCOmmand1.Connection = sqlconnection1

sqldataadaptater.SelectCommand = sqlCOmmand1

 

Me.DataSource = sqldataadaptater

 

 

 End Sub

 

 

0
tony
Top achievements
Rank 1
answered on 19 Oct 2008, 01:49 PM

I add report parameter which use Data source for a dropdown list
by select single value of 'state' (key value), Now the problem come
again, 'How can i use web.config for SQL connection string ?'

Just like my last post that this report can use web.config for SQL connection
for adding line '

ConnectionStringSettings

connSettings = ConfigurationManager.ConnectionStrings["myConnectionString"];
...

Please advice that how to implement this web.config into report parameter section
which is UI --- available value (a datasource). 

Tags
General Discussions
Asked by
Will
Top achievements
Rank 1
Answers by
Chavdar
Telerik team
Will
Top achievements
Rank 1
Francisco
Top achievements
Rank 1
Steve
Telerik team
tony
Top achievements
Rank 1
Share this question
or