SA Developer .NET

Welcome to SA Developer .NET Sign in | Join | Help
in Search

Database connection with external config file?

Last post 07-25-2008, 16:04 by Raithlin. 10 replies.
Sort Posts: Previous Next
  •  06-13-2008, 11:36 12796

    Database connection with external config file?

    Hi,

    I have searched and searched the net...but just cant get my code to work. I have an external "ConnectionStrings.config" file that has the database connection string. Im trying to connect to the database, but the code just does not work. I have no idea what it is that Im doing wrong. I have included the code in the ConnectionStrings.config file, and for the DABasics.cs file. Please can someone tell me what Im doing wrong?

    ConnectionStrings.config
    <connectionStrings>
       <clear />
       <add name="TimeManagement" connectionString="server=DEVSERVER;database=TimeManagement;uid=sa;pwd=XXX;" providerName="System.Data.SqlClient" />
    </connectionStrings>


    DABasics.cs
    using System;
    using System.Web.Configuration;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;

    namespace TimeManagement
    {
    /// <summary>
    /// This is the super class for Data Access Classes
    /// </summary>

    class DABasis
    {

            protected static string strConnect;
    public DABasis()
    {

    }
    /// <summary>
    /// Please see the web.config file
    /// </summary>

    static DABasis()
    {
                Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("ConnectionStrings.config");
    ConnectionStringSettings connString = rootWebConfig.ConnectionStrings.ConnectionStrings["connectionStrings"];
                strConnect = connString.ConnectionString;
    }
    /// <summary>
    /// Gets a SqlConnection to the local sqlserver
    /// </summary>
    /// <returns>SqlConnection</returns>

    protected SqlConnection GetConnection()
    {
    SqlConnection oConnection = new SqlConnection(strConnect);
    return oConnection;
    }

    }
    }

     

     

    ERROR MESSAGES I GET:
    Error 1 The type or namespace name 'Configuration' could not be found (are you missing a using directive or an assembly reference?) C:\_ASPNetWebsites\TimeManagement\DABasis.cs 25 13 TimeManagement

    Error 2 The type 'System.Configuration.Configuration' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. C:\_ASPNetWebsites\TimeManagement\DABasis.cs 25 13 TimeManagement

    Error 3 The type or namespace name 'ConnectionStringSettings' could not be found (are you missing a using directive or an assembly reference?) C:\_ASPNetWebsites\TimeManagement\DABasis.cs 26 13 TimeManagement

  •  06-13-2008, 11:45 12797 in reply to 12796

    Re: Database connection with external config file?

    Forgive my ignorance, but should this:

     Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("ConnectionStrings.config");

    Be this:

     Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("db_connection.config");


    "I would love to change the world, but they won't give me the source code"
    Meeting Place - chat online with anyone, anytime, no downloads or plugins required
  •  06-13-2008, 11:50 12798 in reply to 12797

    Re: Database connection with external config file?

    Yep, thats correct. Sorry, I made a typo and fixed my first post. Embarrassed
  •  06-13-2008, 11:57 12799 in reply to 12798

    Re: Database connection with external config file?

    Error 1 The type or namespace name 'Configuration' could not be found (are you missing a using directive or an assembly reference?) C:\_ASPNetWebsites\TimeManagement\DABasis.cs 25 13 TimeManagement

    Error 2 The type 'System.Configuration.Configuration' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. C:\_ASPNetWebsites\TimeManagement\DABasis.cs 25 13 TimeManagement

    Error 3 The type or namespace name 'ConnectionStringSettings' could not be found (are you missing a using directive or an assembly reference?) C:\_ASPNetWebsites\TimeManagement\DABasis.cs 26 13 TimeManagement

    For all three errors all you have to do is add a reference to System.Configuration to your project 


    "I would love to change the world, but they won't give me the source code"
    Meeting Place - chat online with anyone, anytime, no downloads or plugins required
  •  06-13-2008, 12:02 12800 in reply to 12799

    Re: Database connection with external config file?

    Hi Heat_Rash,

    EDIT: ok...I see it was not in the project references. I added it and the project runs now...but throws some other exception error. Dont think its with the db...will check anyways. On that note...please explain why it needs to be added to the project references as well as the .cs page? Whats the difference btw the 2 and why are both needed and no just the one in the .cs file? Thanks

  •  06-13-2008, 12:17 12801 in reply to 12800

    Re: Database connection with external config file?

    CrazyFig:

    Hi Heat_Rash,

    EDIT: ok...I see it was not in the project references. I added it and the project runs now...but throws some other exception error. Dont think its with the db...will check anyways. On that note...please explain why it needs to be added to the project references as well as the .cs page? Whats the difference btw the 2 and why are both needed and no just the one in the .cs file? Thanks

    "using System.Configuration" has limited classes (don't have better wording) while the reference actually contains the ConfigurationManager class. I have no idea why Microsoft made it that way, they just did... Stick out tongue


    "I would love to change the world, but they won't give me the source code"
    Meeting Place - chat online with anyone, anytime, no downloads or plugins required
  •  06-13-2008, 12:32 12802 in reply to 12801

    Re: Database connection with external config file?

    Well that seems reall dumb to me? Wonder why its like that? Thanks for the info. Does this only happen in System.Configuration or are there other classes this kinda thing happens in too?
  •  06-13-2008, 12:36 12803 in reply to 12802

    Re: Database connection with external config file?

    In my experience I've only found this problem with that namespace & assembly, I don't know if anyone else knows of another such scenario... Hmm

    "I would love to change the world, but they won't give me the source code"
    Meeting Place - chat online with anyone, anytime, no downloads or plugins required
  •  06-13-2008, 12:48 12804 in reply to 12803

    Re: Database connection with external config file?

    Ok kewl, thanks for the info ;)

    Another error has come up, think it has to do with the db connection:

    The type initializer for 'TimeManagement.DABasis' threw an exception. System.NullReferenceException: Object reference not set to an instance of an object. at TimeManagement.DABasis..cctor() in C:\_ASPNetWebsites\TimeManagement\DABasis.cs:line 27

    And here I thought all was fine! Eish!

    using System;
    using System.Web.Configuration;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;

    namespace TimeManagement
    {
     /// <summary>
     /// This is the super class for Data Access Classes
     /// </summary>
     class DABasis
     {
     
            protected static string strConnect;
      public DABasis()
      {
       
      }
      /// <summary>
      /// Please see the web.config file
      /// </summary>
       static DABasis()
      {
                Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/ConnectionStrings.config");
                ConnectionStringSettings connString = rootWebConfig.ConnectionStrings.ConnectionStrings["TimeManagement"];
                strConnect = connString.ConnectionString;                    //line 27
      }
      /// <summary>
      /// Gets a SqlConnection to the local sqlserver
      /// </summary>
      /// <returns>SqlConnection</returns>
      protected SqlConnection GetConnection()
      {
       SqlConnection oConnection = new SqlConnection(strConnect);
       return oConnection;
      }
     
     }
    }

  •  06-13-2008, 13:45 12806 in reply to 12804

    Re: Database connection with external config file?

    Hey Heat_Rash

    I manage dto figure this one out myself (trial and error). I noticed it wanted a "virtual path", I changed this line:

    Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/ConnectionStrings.config");

    To:

    Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/TimeManagement/ConnectionStrings.config");

    And it works now. Why do I have to add in the name of the virtual directory? All the examples I saw on the net, not one had a virutal folder added to the front??? .NET can seem so complex at times?

  •  07-25-2008, 16:04 13701 in reply to 12806

    Re: Database connection with external config file?

    CrazyFig:

    Hey Heat_Rash

    I manage dto figure this one out myself (trial and error). I noticed it wanted a "virtual path", I changed this line:

    Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/ConnectionStrings.config");

    To:

    Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/TimeManagement/ConnectionStrings.config");

    And it works now. Why do I have to add in the name of the virtual directory? All the examples I saw on the net, not one had a virutal folder added to the front??? .NET can seem so complex at times?

    Forgive my ignorance, but would this not suffice?

     

    Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("~/ConnectionStrings.config");

     

View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems