Solved

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

Posted on 2014-03-06
4
1,174 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 23

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

730 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