Solved AppSettings & Connection Strings

Posted on 2006-06-26
Last Modified: 2012-06-27
Here is a very strange problem I'm having. I built an application that is working fine. The connection string works and now I want to move the connection strings to the web.config file.

Before in my InitializeComponent Method (which Works):

this.sqlConnectionDatabase.ConnectionString = "Data Source=servername; Initial Catalog=dbsystemodbc; Persist Security Info=True;User ID=sa;Password=MyPassword";

Now I changed it and placed the following in my webconfig:

<configuration xmlns="">
            <add key="odbcConnectionString" value= "Data Source=servername; Initial Catalog=dbsystemodbc; Persist Security Info=True;User ID=sa;Password=MyPassword"/>

I then changed my InitializeComponent to the following

this.sqlConnectionDatabase.ConnectionString = WebConfigurationManager.AppSettings["odbcConnectionString"];

But this does not work. It fails on the sqlConenctionDatabase.Open();

I have looked at what is returned by the WebConfigurationManager.AppSettings and it looks exactly like the direct string I placed in the connnectionstring property. But if I compare the two strings, the system returns that they are not the same. Also used the ConfigurationSettings.AppSettings["odbcConnectionString"], and got the same results.

Why is this not working? Are there control characters placed in the output of the AppSettings?

My next step is to encrypt, but I cant even get the clear text version from AppSettings to work.

Question by:josephbarron
  • 2
  • 2

Accepted Solution

ayha1999 earned 250 total points
ID: 16987299
try this

<add key="odbcConnectionString" value="server=servername;database=databsename;User ID=sa;password=mypassword;trusted_connection=False;"/>

this.sqlConnectionDatabase.ConnectionString = ConfigurationSettings.AppSettings["odbcConnectionString"];


Expert Comment

ID: 16987311
You have to import the fllowing name space.


LVL 35

Assisted Solution

YZlat earned 250 total points
ID: 16987405
ayha1999, in .NET 2.0 ConfigurationSettings has been replaced by WebConfigurationManager

 josephbarron, did you try debugging?
What value do you get in this.sqlConnectionDatabase.ConnectionString?

use try...catch blocks:


}catch (Exception e) {
LVL 35

Expert Comment

ID: 16987532
I got it! you misspelled sqlConenctionDatabase

Expert Comment

ID: 16999781

Try this.

<add key = "ConnectionInfo" value = "Server=UrServer;uid=sa;password=Urpwd;database=Northwind;"/>

Dim constr As String
Dim con As SqlConnection

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        constr = ConfigurationSettings.AppSettings("ConnectionInfo")
        con = New SqlConnection(constr)

End Sub


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to picture file 3 67
SQL best practice question 3 36
Need a complete list of ASP.NET page load events 3 26
Receiving a string from a WebService Push 21 36
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 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

15 Experts available now in Live!

Get 1:1 Help Now