Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Saied 349 posts 674 karma points
    Sep 02, 2015 @ 18:29
    Saied
    0

    Can't connect to external database using Database(...)

    I added the following connection string in my web.config:

    <add name="productsDb" connectionString="Server=server-name;Database=database;Integrated Security=True;" providerName="System.Data.SqlClient" />
    

    But when I try to access it via:

    private readonly string _connectionString =  
    ConfigurationManager.ConnectionStrings["productsDb"]
                                          .ConnectionString; //It finds the connection string
    
    
    var db = new Database(_connectionString); //This line throws the error
    

    it throws the error:

    Additional information: Can't find a connection string with the name 'Server=server-name;Database=database;Integrated Security=True;'

  • Saied 349 posts 674 karma points
    Sep 02, 2015 @ 18:37
    Saied
    100

    Ah, I got it. There is no need to get the connection string, I just need to pass the name from the web.config into the first overload of the Database object like this:

    var db = new Database("productsDb");
    
Please Sign in or register to post replies

Write your reply to:

Draft