Use web.config for sql connection?

14 posts, 0 answers
  1. Will
    Will avatar
    45 posts
    Member since:
    Aug 2007

    Posted 22 Aug 2007 Link to this post

    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
  2. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 23 Aug 2007 Link to this post

    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
  3. DevCraft banner
  4. Will
    Will avatar
    45 posts
    Member since:
    Aug 2007

    Posted 23 Aug 2007 Link to this post

    Thanks for the help.  This looks like it will solve my problem.
     - will
  5. Francisco
    Francisco avatar
    7 posts
    Member since:
    Aug 2008

    Posted 01 Sep 2008 Link to this post

    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

  6. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 01 Sep 2008 Link to this post

    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.
  7. tony
    tony avatar
    22 posts
    Member since:
    Jun 2006

    Posted 11 Oct 2008 Link to this post

    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!!
  8. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 13 Oct 2008 Link to this post

    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.
  9. Francisco
    Francisco avatar
    7 posts
    Member since:
    Aug 2008

    Posted 13 Oct 2008 Link to this post

    HI,

    you don't have the exemple to VS2005 ??
  10. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 14 Oct 2008 Link to this post

    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.
  11. tony
    tony avatar
    22 posts
    Member since:
    Jun 2006

    Posted 14 Oct 2008 Link to this post

    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.   

     

  12. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 14 Oct 2008 Link to this post

    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.
  13. tony
    tony avatar
    22 posts
    Member since:
    Jun 2006

    Posted 14 Oct 2008 Link to this post

    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

  14. Francisco
    Francisco avatar
    7 posts
    Member since:
    Aug 2008

    Posted 14 Oct 2008 Link to this post

    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

     

     

  15. tony
    tony avatar
    22 posts
    Member since:
    Jun 2006

    Posted 19 Oct 2008 Link to this post

    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). 

Back to Top
DevCraft banner