Saving location of database in the app, not the database

How can I save the location (Path) of the MS Access database I want to connect to in my app without having a second database local to the VB Client. Obviously, the client needs to know the path to the database in order to connect, but where can this path be stored so as to not be hardcoded

Answer this question

Saving location of database in the app, not the database

  • yjoe3

    You can either put it there manually yourself or just right mouse click on your project and click on Add -> Add New Item, then select Application Configuration File and leave the default name and now you'll have an App.config file in your project.  When you compile, it will name the config file appropriately and put it in the bin folder.
  • hwg_Maarten

    Seems to me that the app.exe.config file is the right place to store this information.
  • Danny Tykon

    Okay,

    I did a little investigation about the config files. I think it would work, but how  I understand a little about XML and I can sort of follow the examples in the online help, but like all topics, they are very limited. 

    I need to have one line stored with the physical location of the database that the client should connect to. Obviously, the first time the client is run after installation, there will be no value for this entry. After inputting one (on a startup form) the config file should be updated and then the client can access that updated path and save it to a public variable for the session. All ADO.Net connections will use this variable in the connection string.

    I need to know how to read this path from the config file and write changes to it in the config file  Can you help

  • Howard555

    HumanCompiler,

    I am having some issues with these config files as well... I did what you said above (adding an app config file to the project) but when I run the code below (called from Form_Load) it loads the settings from app.config in the project folder rather than "app".exe.config in the Bin folder... how do I tell the app to use the proper config file
    <Code>
      Private Sub LoadSettings()

        'Load the settings from the XML config file.
        Dim colValues As System.Collections.Specialized.NameValueCollection
        colValues = Configuration.ConfigurationSettings.AppSettings()

        txtDefaultFolder.Text = colValues.Get("default_folder")
        arrFileExtensions = Split(colValues.Get("extensions"), ",")

      End Sub

    I also did what Jacob suggested above to write out the settings to the config file and that works fine, but I can't seem to get the app to read it in from the same file... can you see what I'm doing wrong here   

    Cheers,

    Mike.

  • ADPerkin

    I tried to use this code but got an error saying that the config file does not exist. Sure enough, there is no "app".exe.config file in my Solution directory or the bin directory. Any ideas
  • McCloud

    Reading from it is easy, writing to it, unfortunately, is not ...

    To read:

    System.Configuration.ConfigurationSettings.AppSettings("ConnectionString").ToString


    To write:

            m_DefaultConnectionString = "server=" & ServerValue & ";database=" & DatabaseValue & ";Trusted_Connection=true"
            Try
                Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
                Dim FileInfo As System.IO.FileInfo = New System.IO.FileInfo(Asm.Location + ".config")

                '' Load the config file into the XML DOM.
                Dim XmlDocument As New System.Xml.XmlDocument()
                XmlDocument.Load(FileInfo.FullName)

                Dim Node As System.Xml.XmlNode
                Dim FoundIt As Boolean = False

                ' Find the right node and change it to the new value.
                For Each Node In XmlDocument.Item("configuration").Item("appSettings")
                    If Node.Name = "add" Then ' skip any comments
                        If Node.Attributes.GetNamedItem("key").Value = "ConnectionString" Then
                            Node.Attributes.GetNamedItem("value").Value = "server=" & ServerValue & ";database=" & DatabaseValue & ";Trusted_Connection=true"
                            FoundIt = True
                        End If
                    End If
                Next

                If Not FoundIt Then
                    Dim a As Xml.XmlElement = XmlDocument.CreateElement("add")
                    Dim b As Xml.XmlAttribute = XmlDocument.CreateAttribute("key")
                    Dim c As Xml.XmlAttribute = XmlDocument.CreateAttribute("value")
                    a.Attributes.Append(b).Value = "ConnectionString"
                    a.Attributes.Append(c).Value = "server=" & ServerValue & ";database=" & DatabaseValue & ";Trusted_Connection=true"
                    XmlDocument.Item("configuration").Item("appSettings").AppendChild(a)
                End If

                ' Write out the new config file.
                XmlDocument.Save(FileInfo.FullName)

            Catch ex As SystemException
                ExceptionManager.Publish(ex)
                MsgBox(ex.Message & "<-->" & ex.Source & "  " & ex.TargetSite.Name)
            End Try


    At least, that's how I've done it in the past...  Perhaps someone else can provide an alternative.  Actually, you may want to check this out <a href="http://staff.develop.com/candera/weblog/stories/2003/02/20/theLastConfigurationSectionHandlerIllEverNeed.html">here</a>.

  • IMBack

    Also you can check out my <a href="http://www.gotdotnet.com/Community/UserSamples/Details.aspx SampleGuid=12e78a3c-ae9d-4996-8b0b-c6a4ddef1b59">Config Editor</a> that I wrote a long time ago.  It's simple and has a test app with it.  It's for if you need to write to the config file.
  • Niran

    A further note to my post above... I decided to replace my code with similiar code to what Jacob listed above, this time to read the config file in... it worked just fine but every time I compile the project the copy of app.config in the project folder is copied to the Bin folder over the top of the existing "<app>".exe.config - thus losing any changes made in the routine that saved the config file!  And if I remove the app.config from the project it then deletes the .config file in the Bin folder when it compiles.... so it appears I can't win.

    What I ended up doing was not using a '.config' extension but a '.ini' one - that way I can still use the same code but my changes don't get wiped out when I compile.

    Or I might abandon using that sort of config file and do my own from a straight XML file as I found at George Shepherd's (completely brilliant!) Windows Forms FAQ's <a href="http://www.syncfusion.com/faq/winforms/search/910.asp">here</a>.  Then I can just call it "<app>".ini and simply include in my distribution package.  (There's still a lot to be said for the KISS principle!!)

    Would still like to get any feedback if anyone can see I'm doing something daft!

    Mike.

  • Saving location of database in the app, not the database