0

Trying to read the database name but it's not working in the site. Using an asp.net web project with a solution file. I put this in the web.config. This will change depending on development database or production database PRODDB via the web configuration transform files and scripts.

    <appSettings>
        <add key="databaseName" value="DEVELDB"/>
        <add key="operatingEnvironment" value="dev"/>
    </appSettings>

Within the Global.asax.cs file:

  void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
           
            var operatingEnvironment = WebConfigurationManager.AppSettings["operatingEnvironment"].ToString();

            if (operatingEnvironment == "dev")
            {
                Application["DBE"] = WebConfigurationManager.AppSettings["databaseName"].ToString();
            }
        }

Within the login.cs class file:

 internal static DatabaseConnection GetDatabaseConnection()
            {

                return

                    new DatabaseConnection(
                        new ConnectionProperties
                        {
                            User = Shared.User,
                            Application = Shared.Application,
                            DatabaseName = (string)HttpContext.Current.Application["DBE"]
                        });
            }

and within the Web.Development.config file there is:

    <appSettings>
        <add key="databaseName" value="DEVELDB" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
        <add key="operatingEnvironment" value="dev" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    </appSettings>

However, whenever I debug after login, it goes to the code below which is good (the same as coded above) but says Database name was not specified? I have it specified as DEVELDB and using "DBE" to reference it. However, it's not like it debugs to another area and to there, just straight to there and kills it?

...
  {
                            User = Shared.User,
                            Application = Shared.Application,
                            DatabaseName = (string)HttpContext.Current.Application["DBE"]
                        });
PaulR
  • 23
  • 1
  • 7

1 Answers1

0

Connections to databases go inside the "connectionstrings" tags in you "webconfig" file. If you need SqlServer it is shown below

 <configuration>
  <connectionStrings>
    <add name="TwoWayConn" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\TwoWayData.accdb"
      providerName="System.Data.OleDb" />
    <add name="TWOWAYDATASQL" connectionString="Data Source=TBIRD\SQLEXPRESS1;Initial Catalog=TWOWAYDATASQL.MDF;Integrated Security=True"
  providerName="System.Data.SqlClient" />
 </connectionStrings>
....

    </configuration>

Am I misunderstanding what you are trying to do?

GailV
  • 11
  • 2
  • Hi @GailV, Actually, we do things differently here for the DB connection and this even more of a different way within a web project. However, I decided to do it the original way to reference the database in the Global.asax file and add the connection data in a class. Thank you! – PaulR Jan 05 '22 at 16:28