0

I have the following problem. I have a project A (Windows forms) and project B (MVC Application). In Project A, I am referencing Project B, because I want to use some classes from it. At one point I have to get, the modificated connection string, from the Project B and connect to database in Project A.

Code in Project B, that is manipulation the connection string:

public static string ConnectionString = "";
public TheClassThatManipulateTheConnectionString (){
     ConnectionString = ManageConnectionString();
}



     private string ManageConnectionString(){
             WriteToFile.WriteToTextFile("0");
             string ConfigurationString = ConfigurationManager.ConnectionStrings["DbConnectionString"].ConnectionString;
             WriteToFile.WriteToTextFile("1");
             EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(ConfigurationString);
             WriteToFile.WriteToTextFile("2");
             DbProviderFactory factory = DbProviderFactories.GetFactory(entityBuilder.Provider);
             WriteToFile.WriteToTextFile("3");
             DbConnectionStringBuilder providerBuilder = factory.CreateConnectionStringBuilder();
             WriteToFile.WriteToTextFile("4");
             providerBuilder.ConnectionString = entityBuilder.ProviderConnectionString;
             WriteToFile.WriteToTextFile("5");
             providerBuilder.Add("Password", "ThePassword");
             WriteToFile.WriteToTextFile("6");
             entityBuilder.ProviderConnectionString = providerBuilder.ToString();
             WriteToFile.WriteToTextFile("7");
             return entityBuilder.ToString();
        }

Code in Project A, that is invoking Project B method:

public Form1(){
    InitializeComponent();
    ProjectB.Folder.Folder.TheClassThatManipulateTheConnectionString TheClassThatManipulateTheConnectionString = new ProjectB.Folder.Folder.TheClassThatManipulateTheConnectionString ();
    using (ProjectB.Folder.Folder.Context db = new ProjectB.Folder.Folder.Context TheClassThatManipulateTheConnectionString.TheConnectionString))
                    { }
}

Exceoption writen to the file along with the track code.

'08/06/2014 21:29:35: 0
 08/06/2014 21:29:35:  Exception: System.NullReferenceException: Object reference not set to an instance of an object.'

Exception from Project A

at System.Data.Entity.Utilities.Check.NotEmpty(String value, String parameterName)
   at System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
   at ProjectB.Folder. Folder.Context..ctor(String ConnectionString) in c:\Users\Administrator\Documents\Visual Studio 2013\Projects\ ProjectB \ ProjectB \ Folder \ Folder \DBEntities.Context.cs:line 20
   at ProjectA.Form1.FillUsers() in c:\Users\Administrator\Documents\Visual Studio 2013\Projects\ ProjectA\ ProjectA \Form1.cs:line 51

On the text file, I can see 0, but not 1. My question is, why the configuration manager is not working properly, when invoked it into Project A.

user2831001
  • 115
  • 1
  • 1
  • 9
  • Make sure you've connection string with name "DbConnectionString" in Project A app.config/web.config file – malkam Jun 08 '14 at 19:52
  • 1
    The problem is not in the exception. My question is, why the configuration manager is not working properly, when I invoked it into Project A. – user2831001 Jun 08 '14 at 19:54
  • @malkam - everything in project A works properly. It has been tested. – user2831001 Jun 08 '14 at 19:54
  • 1
    I disagree with the close votes here. This appears to be a case of figuring out how to read a config file from one project in another. The NRE is a symptom, but it's not the actual issue. – Anthony Pegram Jun 08 '14 at 21:12
  • 1
    To the OP, I have a feeling the answer is simply that you'll need to replicate some config settings into the project that's actively running, or more ideally have each project pull config settings from a shared source. An answer from a more knowledgeable expert may confirm and give pointers. – Anthony Pegram Jun 08 '14 at 21:13
  • Break the code down where you say, "It gives me a nullreference exception", put as much code as you can on separate lines. Give us a stack trace and have that stacktrace line up to the exact line that appears in the NRE. As written, it's not at all clear *what* is causing your NRE, so we can't really help you solve a problem when there is this much ambiguity. – George Stocker Jun 08 '14 at 21:57
  • @George Stocker - The necessary information that you asked for is added to the question – user2831001 Jun 09 '14 at 08:46

0 Answers0