Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now