Can't read web.config file ConnectionStrings from ASP.NET Script with my ISP

Posted on 2007-10-19
Last Modified: 2012-06-27
I'm actually having a problem.  I can't find a way to read the connectionstring with my script for some reasons when i upload them at my ISP.  Locally, my code work perfectly.   Here's my code...
Dim oConfig As System.Configuration.Configuration
Dim sSQLConnection As String
Dim dbBYS As SqlConnection
oConfig = Configuration.WebConfigurationManager.OpenWebConfiguration("/.")
sSQLConnection = oConfig.ConnectionStrings.ConnectionStrings("MyDb").ConnectionString
dbBYS = New SqlConnection(sSQLConnection)
dbBYS.Open() ================================================================================

Here's the result when i run my script from when its hosted by my ISP:
"Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. "

If i replace the first lines by sSQLConnection = "my parameters to connect to my DB", it work.

Any idea how i could be able to read theses parameters to connect  to our DB without throwing an exception?

Is that a common thing to secure a web server this way?

Question by:cdebel
    LVL 5

    Accepted Solution


    the web app probably fails because it's running under medium trust and not full trust like when you try it locally.
    You'd get the same error locally if you add <trust level="Medium" /> inside <system.web>

    You can easily avoid this problem by using the following instead:

    Dim sSQLConnection As String
    Dim dbBYS As SqlConnection

    sSQLConnection = ConfigurationManager.ConnectionStrings("MyDb").ConnectionString;
    dbBYS = New SqlConnection(sSQLConnection)
    LVL 10

    Author Comment

    Thanks!  It worked!

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now