locked
Connectionstring at Runtime RRS feed

  • Question

  • I have a WinForms application written in Visual Studio 2008 C#. the application has an app.config in its directory that I store connectionstring in the connectionstrings section. What I want to do it this:

    1. Application starts and checks the app.config file to see if there are connectionstrings in the connectionstrings section
    2. If there are not any a dialog will come open that will allow them to select a computer when the database is installed and I will create a connection string based from this. I want to then save this connectionstring to the app.config file for future use.
    3. If there are connectionstrings in this section I want to present the user with a dropdown of all the connectionstring names so they can select which one they want.

    I have the part working if there are connection strings and presenting a dropdown with the names of then in it. What I really need help is how to write a connectionstring to the app.config file from the application? Any help on writing to this area of the app.config file would be appreciated. Also, if this is not the way to do it and there is a better way please let me know about that too. Thank you very much for any help that I can get with this problem.
    Friday, December 4, 2009 12:14 AM

Answers

  • Hi LandsharkDaddy,

    We can load the config file to a XmlDocument object, find the related node, modify the value of the connection string and then call the Save method of the XmlDocument to write the altered value the config file. The thread below shows the details:
    http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/cb9b4823-1dc4-4721-9797-f65bd0de6135

    Let me know if this does not help.
    Aland Li
    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Aland Li Friday, December 11, 2009 6:01 AM
    Wednesday, December 9, 2009 8:23 AM

All replies

  • Hi,
    have a look at this link
    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/4e87767e-1e36-4413-86cf-7ba8fc1c6884/
    http://www.ryanfarley.com/blog/archive/2004/07/13/879.aspx
    hope it helps


    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    Friday, December 4, 2009 4:15 AM
  • I have seen these articles and while they have great information they are for the app.setting section of the app.config file. I want to edit the connectionstrings section of the file to allow the user to store multiple connectionstring in the section.
    Friday, December 4, 2009 10:29 PM
  • I have seen these articles and while they have great information they are for the app.setting section of the app.config file. I want to edit the connectionstrings section of the file to allow the user to store multiple connectionstring in the section.

    you can create a datatable Fields (ID, UserID, ConnectionString) or hashtable etc...

    Then

    Build your connection in the Project Settings

    - Click on Project from the VS Menu Bar
    - Click on your Project Name Properties
    - Project Properties open click Settings Tab from the Left
    - In the Name Field type a name for your Connection myConnection
    - In the Type Field select (Connection String)
    - Put your cursor in the Value Field then click the ellipsis button to build your connection string.
    - Change your datasource
    - Follow the wizard

    Now you can call your connection from the application settings

    So each user will have their own settings


    I've done something in the web based on the app.setting maybe you can apply it to windows form

    using System.Configuration; 
    using System.Web.Configuration;
    
    
    
    private void saveApp()
    {
        // Save new key and value.
        string newKey = txtObject.Text;
        string newValue = txtObject2.Text;
        
        Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        
        config.AppSettings.Settings.Add(newKey, newValue);
        
        // Save modified section of Web.config.
        config.Save();
    }






    John
    • Proposed as answer by Codernater Wednesday, December 9, 2009 1:15 PM
    Saturday, December 5, 2009 12:40 AM
  • Hi LandsharkDaddy,

    We can load the config file to a XmlDocument object, find the related node, modify the value of the connection string and then call the Save method of the XmlDocument to write the altered value the config file. The thread below shows the details:
    http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/cb9b4823-1dc4-4721-9797-f65bd0de6135

    Let me know if this does not help.
    Aland Li
    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Aland Li Friday, December 11, 2009 6:01 AM
    Wednesday, December 9, 2009 8:23 AM