Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-03-06
4
Medium Priority
?
1,275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Suggested Courses

610 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