Solved

ASP.net AppSettings & Connection Strings

Posted on 2006-06-26
5
662 Views
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="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
Comment
Question by:josephbarron
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
ayha1999 earned 250 total points
ID: 16987299
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
 
LVL 7

Expert Comment

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

system.configuration


ayha
0
 
LVL 35

Assisted Solution

by:YZlat
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:

try{
      sqlConenctionDatabase.Open();

}catch (Exception e) {
       Console.Write(e);
}
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16987532
I got it! you misspelled sqlConenctionDatabase
0
 

Expert Comment

by:SathyaPrabha
ID: 16999781
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

13 Experts available now in Live!

Get 1:1 Help Now