Solved

changing a connection string in a library class at run time vb.net

Posted on 2014-03-06
4
1,146 Views
Last Modified: 2014-03-07
So I have a solution split into several projects.  The main project being a windows forms project.  The problem is that one of my class libraries contains a connection string that is pointing to a sqlite database in the application directory.  Currently it is an absolute path but I would like to change it to based on what the current application directory is.  I am able to construct the path using my.application.info.directorypath but setting the application.setting won't work from what I understand due to it being a Class Library. and no dll.config file.   The actual connection string is used by multiple table adapters as defined in the .xsd files and the code visual studio has created for the basic functionality.

Hopefully there is a way around this.  At the moment every change of install path requires updating the settings pre-build which won't work moving forward when we pull together an installer for this.

Thanks in advance!
0
Comment
Question by:KJLC
4 Comments
 
LVL 22

Expert Comment

by:Snarf0001
ID: 39910978
You can still use application settings for what you want.
If you go into Properties -> Settings of the ClassLibrary, click the Create settings option and put in the setting you want.

In the app.config of the winform application, you add the reference to those settings in there.  You need to add the section in <configSections> and in <applicationSettings>.

Once it's in there, the class library will pull it's values from the main config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="MyClassLibrary.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      <section name="MyWinfowsForm.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <MyClassLibrary.Properties.Settings>
      <setting name="ClassLibSettings" serializeAs="String">
        <value>fff</value>
      </setting>
    </MyClassLibrary.Properties.Settings>
    <MyWinfowsForm.Properties.Settings>
      <setting name="WinFormSetting1" serializeAs="String">
        <value>eee</value>
      </setting>
    </MyWinfowsForm.Properties.Settings>
  </applicationSettings>
</configuration>

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39912121
One option is to pass the connection string from the winforms to the DLL when calling the data load methods.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39912463
Or, you can find the entry point assembly from your class library, and the access its connection strings directly:
    Public Function GetConnString(ByVal connectionName As String)

        Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetEntryAssembly()
        Dim config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(asm.Location)

        Return config.ConnectionStrings().ConnectionStrings(connectionName).ConnectionString

    End Function

Open in new window

0
 

Author Closing Comment

by:KJLC
ID: 39912903
I'm not sure why I didn't think of that but it is working well for my needs.  I ended up passing the updated connection strings into a sub in the dll class that runs through the local setting updates.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question