Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:17:35+00:00 2026-06-15T07:17:35+00:00

Some quick questions: So I have this LogInWindow dialog that I use to collect

  • 0

Some quick questions:

So I have this LogInWindow dialog that I use to collect all the SQL login info and then pass the constructed connection string to the ServiceManager.InitializeContext() method, which initializes my entities context with a specific provider connection.

It’s all working fine. But I would like to initialize the LogInWindow dialog with the values that are defined in the App.config configuration file.

Is there a preferred method for doing this initial setup? i.e., should I resort to instantiating a dummy EntityContext using the default constructor just for the sheer purpose of fetching the default provider connection? Is there a “cleaner” way?

By the way, would you consider it a “safe” practice to do this type of Form.Close() invocation inside a handler to the Form.Shown event? I read on MSDN that it is not advisable to call Form.Close() inside a handler to the Form.Load event.

public partial class MainWindow : Form {

    private void MainWindow_Shown(object sender, EventArgs e) {
        using (var logInWindow = new LogInWindow()) {
            if (logInWindow.ShowDialog(this) == DialogResult.OK) {
                this.serviceManager.InitializeContext(logInWindow.ConnectionString);
            } else {
                this.Close();
            }
        }
    }
}


public sealed class ServiceManager : IDisposable {

    public void InitializeContext(string connectionString) {
        if (this.EntityContext != null)
            throw new InvalidOperationException("EntityContext cannot be initialized multiple times.");

        var entityConnectionString = new EntityConnectionStringBuilder();
        entityConnectionString.ProviderConnectionString = connectionString;
        entityConnectionString.Provider = "System.Data.SqlClient";
        entityConnectionString.Metadata = "res://*/EntityModel.EntityModel.csdl|res://*/EntityModel.EntityModel.ssdl|res://*/EntityModel.EntityModel.msl";

        this.EntityContext = new EntityContext(entityConnectionString.ConnectionString);
        this.EntityContext.ObjectMaterialized += EntityContext_ObjectMaterialized;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:17:36+00:00Added an answer on June 15, 2026 at 7:17 am

    You can access all parts of your App.config using System.Configuration class

    Example:

    var connectString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    
    var stringBuilder = new SqlConnectionStringBuilder(connectString);
    
    string UserID = stringBuilder.UserID;
    string Password = stringBuilder.Password;
    string catalog = stringBuilder.InitialCatalog;
    

    App.config

    <configuration>
       <connectionStrings>
         <add name="MyConnectionString" connectionString="metadata=res://*/...........
       </connectionStrings>
    </configuration>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A quick technical question- I have two queries that output some of the same
Quick question about include/requre_once . I have some code that is common to a
Question simple and quick: I have started to use Netbeans to write some code
For those of you that are quick to answer some questions, with code snippets.
I have a few quick questions about the iPhone software development. I did some
I have some quick questions on windows dll. Basically I am using the ifdefs
After a quick scan of related questions on SO, I have deduced that there's
Couple of quick questions. I have a DMX king USB lighting controller that I'm
----- PHP and mySQL ----- I have two quick questions need some advice. On
Okay, I am stumped on this one and need some quick help. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.