Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 713
  • Last Modified:

ASP.net AppSettings & Connection Strings

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="http://schemas.microsoft.com/.NetConfiguration/v2.0">
      <appSettings>
            <add key="odbcConnectionString" value= "Data Source=servername; Initial Catalog=dbsystemodbc; Persist Security Info=True;User ID=sa;Password=MyPassword"/>
      </appSettings>

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.

Thanks
0
josephbarron
Asked:
josephbarron
  • 2
  • 2
2 Solutions
 
ayha1999Commented:
hi
try this

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

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

ayha
0
 
ayha1999Commented:
You have to import the fllowing name space.

system.configuration


ayha
0
 
YZlatCommented:
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:

try{
      sqlConenctionDatabase.Open();

}catch (Exception e) {
       Console.Write(e);
}
0
 
YZlatCommented:
I got it! you misspelled sqlConenctionDatabase
0
 
SathyaPrabhaCommented:
Hai,


Try this.



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

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)
        con.Open()

End Sub

Sathya
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now