Development and Live web.config?

Discussion in 'ASP.NET / ASP.NET Core' started by xaisoft, Sep 12, 2009.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hello,

    I successfully uploaded an asp.net MVC app using my sql server 2008 database on discountasp.net, however, when I test locally, I need to use my local connection string, but when I deploy to live, I need to change my connection string to the one I use on dasp. Is there a way where I actually don't have to do this? I also saw another post where it mentioned that I could connect remotely to my dasp sql server database?

    Thanks
     
  2. Here's one solution:
    1) Put both your local and live connection strings into the application web.config
    2) Change your code so that when you need to use a connection string you do something like:
    Code:
    string connStr = string.Empty;
    #if DEBUG
    connStr = ConfigurationManager.ConnectionStrings["debugConnStr"].ConnectionString;
    #else
    connStr = ConfigurationManager.ConnectionStrings["liveConnStr"].ConnectionString;
    #endif
    
    3) Now when testing locally you do this within a debug build but when you deploy/publish to DASP you build a release build; this process now allows you to have the behaviour you want.
     
  3. Ok,

    That works, but what if I am just referencing my connection string from web.config and not in code?
     
  4. I think you mean you have your db connection string defined by name and then some other section(s) in the web.config use(s) this connection string by its defined name..examples being the membership provider and role provider sections.

    Can you give more details on how your web.config is setup? The answer to your question depends on what you're doing.
     
  5. I have this in my webconfig for development, for example:

    </connectionstring>
    <add name="conn" datasource="dev;initial catalog="database";user id="dev"; password="dev"/>
    </connectionstrings>

    When I go to dasp, I have to change it to something like:
    <add name="conn" datasource="live;initial catalog="database";user id="live"; password="live"/>

    I don't want to have to do this.
     
  6. Yes I understand that; post #2 suggests a possible solution to this problem.

    Post #4 is asking you to fully explain this one liner because you seem to be saying there is some reason why the suggested solution doesn't work for you. I can think of one scenario where the suggested solution doesn't actually work, but I need you to spell out your situation otherwise I can't (or really understand the need to) come up with an alternative suggestion.
     
  7. I am not using:

    connStr = ConfigurationManager.ConnectionStrings["debugConnStr"].ConnectionString;

    in any code (cs) files. I know I can put two connection strings in my web.config, but is there a way to tell it which one to use in the web.config file, not in my code files.
     
  8. I give up on this one because I'm obviously completely missing the point. Maybe somebody else will be able to understand your problem better and will pitch in after reading this thread.
     
  9. Ok, I will try to explain better.

    I created an ASP.NET MVC Application. I am LINQ to SQL. When I added a Linq to Sql Item by selection Add New Item from the solutions context menu, it automatically added my connection string to my local server and database in my web.config file. When I deploy to dasp, I have to change my connection string in my web.config file to use the server and database setup on dasp instead of my local one. Basically, I want to do something like this.

    if local
    Use local connection string from web.config
    if live
    use live connection string from web.config

    I am not referencing the connection string anywhere in code. It is just in the web.config file.
     
  10. Bruce

    Bruce DiscountASP.NET Staff

    xaisoft.. i suggest you just simply change your connection string on the live site for now.

    Why?? because asp.net 4.0 is going to have this feature built in and it will be released very soon. I wouldn't spend much time building a custom feature to handle using connection strings btw live / dev.
     
  11. Great thanks.
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page