getting the connection string

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
97
Programming Experience
10+
I am beginning the trek of incorporating an SQL database into an application that I wrote which is operational. As part of that trek I am trying to read the connection string from the app.config file in order to connect to the database. I have tried a number of different constructs which either produce "no constructor" errors or the pair that I have in the following example. With regards to the code below what "object" are they referring to. Any help would be greatly appreciated.

'form 1
Dim SQLCnn As SqlConnection
SQLCnn = New SqlConnection(ConfigurationManager.ConnectionStrings _
("WindowsApplication1.My.MySettings.invgenConnectionString").ConnectionString)

'form 2
Dim SQLCnn As New SqlConnection(ConfigurationManager.ConnectionStrings _
("WindowsApplication1.My.MySettings.invgenConnectionString").ConnectionString)

The above two constructs produce the following error

System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsApplication1.Form1.Form1_Load(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)

The "WindowsApplicatoin1" verbiage is the "name" specification that came from the app.config file which is the parameter data for the ConnectionStrings() property.

Respectfully,
 
Use the SqlConnectionStringBuilder object, makes everyone's life much easier:

' Pass the string-form connection string to the SqlConnectionStringBuilder constructor so it will parse the many different syntaxes..
Dim ConnectionString As New SqlConnectionStringBuilder(strConnectionString)

' Then you can modify some connection string settings before opening the connection...
With ConnectionString
     .Pooling = False
     .AsynchronousProcessing = False
End With

Dim SQLConn As New SqlConnection(ConnectionString.ToString)
 
Last edited:
Thank you Herman I will indeed try this construct. After many hours of research I finally was able to "create" a "hook" to a connection string by using the ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) methodology only to find out that the connection string that I retrieved was not the one from the app.config file but from the "settings.settings" file. This of course has produced all kinds of other interesting problems like inserting "default" data base references, etc. I need to get a good architectural picture of this choreography. I will assume that the SqlSonncectionsStringBuilder is going to retrieve from the app.config connectionstrings section. Again thanks for your help.

Respectfully,
Gordon Haas
 
Hi Herman

Just wanted to let you know that the class you suggested did indeed do the trick. All started to move forward once I realized that the strConnectionString argument was constructed and not retrieved. And that was fine with me for then I could specify the data base location explicitly. Of course it took another 4 hours of figuring out the syntax and architecture of the table access and so forth but I am now reading data from my local SQL database. Even a blind chicken gets a little corn once in awhile!!

Again, Thanks.
Respectfully,
Gordon Haas
 
Hi Gordon,

Glad it could help. strConnectionString is really just a string containing a connection string. If you had it in resources or in a file, it's easy enough to extract it from there and use it for the sqlConnectionStringBuilder. The class reads it, and parses it to more usable object with properties and methods. Then you convert the object to string when you pass it back to the connection.
 
Take a read of the DW3 link in my signature, section Creating a Simple Data App

at the end of that tutorial you'll have working code that has achieved a lot, and you'll have an app that reads its conn string from the settings file
The best part; it will take you around 5 minutes to do (many times faster than this problem you've been banging your hed on) and it will work ;)
 
Hi cjard
I will take a look at what you have at the dw3 link. My need is not just code so I am hoping some concepts will flow from the exercise. Being a mathematician by degree I have an insatiable need to "see" what the symbols are holding.
Thanks
 
Back
Top