0

I am new to ASP.Net web app development and I am trying to learn.

I have a ASP.NET web app where a specific connection string is used when debugged (pressing F5). Below are the connection strings and app settings in Web.config file:

<appSettings>
    <add key="Environment" value="TEST"/>
</appSettings>  
<connectionStrings>
    <add name="TEST" connectionString="Server=mySQLExpressServerAddress;Database=myDataBase;Trusted_Connection=True;" />
    <add name="DEV"  connectionString="a connection to the test server db" />
    <add name="PROD" connectionString="a connection to the prod server db" />
</connectionStrings>

Till here, when I debug (pressing F5). The execution is smooth.

When I comment out the TEST from connection string, and add a secondary connection string like this:

<appSettings>
    <add key="Environment" value="TEST"/>
</appSettings>  
<connectionStrings>
    <!-- <add name="TEST" connectionString="Server=mySQLExpressServerAddress;Database=myDataBase;Trusted_Connection=True;" /> -->
    <add name="TEST_TWO" connectionString="Server=mySQLExpressServerAddress;Database=myDataBase;Trusted_Connection=True;" />
    <add name="DEV"  connectionString="a connection to the test server db" />
    <add name="PROD" connectionString="a connection to the prod server db" />
</connectionStrings>

and debug the app (pressing F5), there is a null pointer exception. The execution detail is as follows:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.

Now, if I change the value of environment in app settings from test to test_two (while leaving the commented TEST connection string as it is) like this:

<appSettings>
    <add key="Environment" value="TEST_TWO"/>
</appSettings>  
<connectionStrings>
    <!-- <add name="TEST" connectionString="Server=mySQLExpressServerAddress;Database=myDataBase;Trusted_Connection=True;" /> -->
    <add name="TEST_TWO" connectionString="Server=mySQLExpressServerAddress;Database=myDataBase;Trusted_Connection=True;" />
    <add name="DEV"  connectionString="a connection to the test server db" />
    <add name="PROD" connectionString="a connection to the prod server db" />
</connectionStrings>

and debug the app (pressing F5), the execution is smooth.

I believe, the "Environment" key from app settings is used to identify the connection string while debugging. I want to know if I am correct or wrong here.

If I am wrong, how can I assign a particular connection string for debugging.

Thank You

1 Answer 1

2

Looking at what you are trying to do I think you need to be looking into using web.config transformations.

Have a look at this really nice guide on the subject Transform Web.Config

You are correct that the name reference is what app settings is looking for so if you change it in your web.config and not your code it is going to shout at you! ;-)

ConfigurationMananager.Connections["Test_Two"].ConnectionString

This is what you are looking for in your code. but if you want a different DB connection string for debug vs release then that is where your web.config transform comes in.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.