jvcoach23 Posted February 2, 2005 Posted February 2, 2005 I need some help.. I've created a dll that holds a number of functions that makes calls to a sql database. in each function is a connection string. I'd like to be able to populate that connection string from a file. I was thinking that this would make it much easiser to change from dev to production env. Question is.. can I do this and how. thanks for any help shannon Quote JvCoach23 VB.Net newbie MS Sql Vet
qex Posted February 2, 2005 Posted February 2, 2005 maybe I'm over simplifying but couldn't you just use appropriate Stream classes and write your paths to a file that way... The other option would be to use an xml file using XmlDocument class. Quote
jvcoach23 Posted February 2, 2005 Author Posted February 2, 2005 maybe I'm over simplifying but couldn't you just use appropriate Stream classes and write your paths to a file that way... The other option would be to use an xml file using XmlDocument class. not sure what you mean.. I tried writing out my connection string to a file.. that worked fine. I tried reading my connection string in through a stream to a variable... that didn't seem to work. what you are saying may be true.. i'm not very good at this stuff.. still learning.. and so much to learn. Quote JvCoach23 VB.Net newbie MS Sql Vet
qex Posted February 2, 2005 Posted February 2, 2005 not sure what you mean.. I tried writing out my connection string to a file.. that worked fine. I tried reading my connection string in through a stream to a variable... that didn't seem to work. what you are saying may be true.. i'm not very good at this stuff.. still learning.. and so much to learn. I'm not all that familiar with SQL so when you say connection string is it just the path to the SQL database or the code that you use to open the connection. If you are talking about the code to open the source I don't know what to tell you... if you are simply talking about a string path then all you have to do is write it out to a file.... I think I probably over simplified your problem... sorry I couldn't help Quote
twistedm1nd Posted February 7, 2005 Posted February 7, 2005 Add a new app. config file ... Add -> Add new Item,->Application Configuration File this file can contain yer various DB connection strings <configuration> <appsettings> <add key="key1" value="my key1 value"> <add key="key2" value="my key2 value"> <add key="key3" value="12345"> <add key="key4" value="true"> </appsettings> </configuration> To read the app.config u code string s1 = System.Configuration.ConfigurationSettings.AppSettings["key1"]; MessageBox.Show(s1); string s2= System.Configuration.ConfigurationSettings.AppSettings["key2"]; MessageBox.Show(s2) Quote
jvcoach23 Posted February 7, 2005 Author Posted February 7, 2005 Add a new app. config file ... Add -> Add new Item,->Application Configuration File this file can contain yer various DB connection strings <configuration> <appsettings> <add key="key1" value="my key1 value"> <add key="key2" value="my key2 value"> <add key="key3" value="12345"> <add key="key4" value="true"> </appsettings> </configuration> To read the app.config u code string s1 = System.Configuration.ConfigurationSettings.AppSettings["key1"]; MessageBox.Show(s1); string s2= System.Configuration.ConfigurationSettings.AppSettings["key2"]; MessageBox.Show(s2) thanks for the lead.. I created a config file.. <?xml version="1.0" encoding="utf-8" ?> <configuration> <add key="key1" value = "pwd=pmdatagatherer123; UID=pmDataGatherer; server=Ntdts1; database=Monitor"></add> </configuration> and tried plugging it into the dll. Dim configfile As String = System.Configuration.ConfigurationSettings.AppSettings("key1") the configfile is nothing.. so, am i getting close just missing something... thanks Quote JvCoach23 VB.Net newbie MS Sql Vet
qex Posted February 7, 2005 Posted February 7, 2005 thanks for the lead.. I created a config file.. <?xml version="1.0" encoding="utf-8" ?> <configuration> <add key="key1" value = "pwd=pmdatagatherer123; UID=pmDataGatherer; server=Ntdts1; database=Monitor"></add> </configuration> and tried plugging it into the dll. Dim configfile As String = System.Configuration.ConfigurationSettings.AppSettings("key1") the configfile is nothing.. so, am i getting close just missing something... thanks here is how I have done it in the past Dim configurationAppSettings As System.Configuration.AppSettingsReader = new System.Configuration.AppSettingsReader Dim InitialRun As Boolean InitialRun = CType(configurationAppSettings.GetValue("InitialRun", _ GetType(System.Boolean)), String) LabName = CType(configurationAppSettings.GetValue("LabName", _ GetType(System.String)), String) Quote
Optikal Posted February 7, 2005 Posted February 7, 2005 What did is the filename of your config file. It must be the same as your assembly name + the .config. eg. MyClassLibrary.dll MyClassLibrary.dll.config Quote
jvcoach23 Posted February 8, 2005 Author Posted February 8, 2005 here is how I have done it in the past Dim configurationAppSettings As System.Configuration.AppSettingsReader = new System.Configuration.AppSettingsReader Dim InitialRun As Boolean InitialRun = CType(configurationAppSettings.GetValue("InitialRun", _ GetType(System.Boolean)), String) LabName = CType(configurationAppSettings.GetValue("LabName", _ GetType(System.String)), String) I changed the name of the config file to be performance.config.. the name of the dll is performance.dll. I changed it just to try it based on the post. Here is what I have in the code. Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader Dim LabName = CType(configurationAppSettings.GetValue("key1", GetType(System.String)), String) here is the config file info <?xml version="1.0" encoding="utf-8" ?> <configuration> <add key="key1" value = "pwd=password; UID=uid; server=(local); database=Monitor"></add> </configuration> I'm sorry I'm not getting this to work..hope you'll continue the help. When i step throught he code.. the CType(configurationAppSettings.GetValue("key1", GetType(System.String)), String) comes back as nothing Quote JvCoach23 VB.Net newbie MS Sql Vet
twistedm1nd Posted February 9, 2005 Posted February 9, 2005 (edited) <?xml version="1.0" encoding="utf-8" ?> <configuration> <add key="key1" value = "pwd=password; UID=uid; server=(local); database=Monitor"></add> </configuration> an app config file is in the format <?xml version="1.0" encoding="utf-8" ?> <configuration> [b]<appsettings>[/b] <add key="key1" value = "pwd=password; UID=uid; server=(local); database=Monitor"></add> [b]</appsettings>[/b] </configuration> u missed the app settings tag out ALSO as Optical pointed out the config file must be named performance.dll.config BECAUSE the name of yer DLL is perfromance.dll IN general assembly name + ".config" eg. blah.exe -> blah.exe.config mydll.dll ->mydll.dll.config Edited February 9, 2005 by twistedm1nd Quote
jvcoach23 Posted February 9, 2005 Author Posted February 9, 2005 ok.. i've made those changes.. in stepping through it looks like it is trying to read from the performance.dll.config file. however it says that the key key1 does not exist so it can't read from it. The config file looks just like the one you modified now.. Quote JvCoach23 VB.Net newbie MS Sql Vet
twistedm1nd Posted February 9, 2005 Posted February 9, 2005 (edited) my bad coach its appSettings < yep case sensitivity .. which leads me to the question are XML files case sensitive .. i'm too sleepy now i'll look it up morrow and let ya know > //VB.Net [yep it works now ] Dim configfile As String = System.Configuration.ConfigurationSettings.AppSettings("key1")dll1.zip Edited February 9, 2005 by PlausiblyDamp Quote
jvcoach23 Posted February 10, 2005 Author Posted February 10, 2005 Success.. Wow.. thanks so much for the help.. good learning experience... fixing the case did the trick... Thanks again Shannon Quote JvCoach23 VB.Net newbie MS Sql Vet
twistedm1nd Posted February 10, 2005 Posted February 10, 2005 Sorry PlausiblyDamp, I decided to remove them(binaries) and then decided to leave them on... guess i should have removed them. Thanks Diesel. Ty to u too shannon, thats my 1st VB.Net program:p lol Quote
jvcoach23 Posted February 10, 2005 Author Posted February 10, 2005 dog gone if I didn't speak too soon. I was watching the wrong thing. The config file still doesn't seem to be getting populated. My dll troubleshooting skills might be looking in the wrong spots too. but my config perfmon.dll.config file looks like this. <?xml version="1.0" encoding="utf-8" ?> <configuration> <AppSettings> <add key="key1" value = "pwd=pmdatagatherer123; UID=pmDataGatherer; server=(local); database=Monitor"></add> </AppSettings> </configuration> the code I'm using to try to read it to a string is this Dim configfile As String = System.Configuration.ConfigurationSettings.AppSettings("key1") when I set a break point in my code to go out and show me when it tries to read from the dll.. i look at teh configfile and it is always = nothing. Quote JvCoach23 VB.Net newbie MS Sql Vet
jvcoach23 Posted February 10, 2005 Author Posted February 10, 2005 bit more info.. I tried using this Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader Dim configfile = CType(configurationAppSettings.GetValue("key1", GetType(System.String)), String) and the error message I got back said something about couldn't read from the appSettings. Now in the perfmon.dll.config file.. the app setting secition is AppSettings.. not appSettings. in digging around.. I also have a applicaitonconstants.config file.. that does have an applicationSettings section. under that is a <!--database settings--> <DB_CONN> am i trying to work with the config file. Quote JvCoach23 VB.Net newbie MS Sql Vet
Administrators PlausiblyDamp Posted February 10, 2005 Administrators Posted February 10, 2005 IIRC Dlls do not get to use a config file directly - name the config file after the exe that uses the dll - not the dll itself and things should work. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.