Thank you!!

Dear all,

Me and my friend are new to visual studio 2005 express edition. We are trying to build a simple desktop database application. Now both of us have different connectionString to the database in our computers (which is quite obvious).At first, we used hardcoded connection strings in our apps. So it was a huge task to replace all those lines as we shifted our computers to share our works.

For compatibality, we were trying to use the app.config file for the connectionString, which looks like : (in my computer)

<?xmlversion="1.0"encoding="utf-8" ?>

<configuration>

<configSections>

</configSections>

<connectionStrings>

<addname="Prototype1.Properties.Settings.North_Pole_1_1SQLConnectionString"

connectionString="Data Source=ISHTIAQUE\SQLEXPRESS;Initial Catalog=&quot;North Pole 1.1SQL&quot;;Integrated Security=True"

providerName="System.Data.SqlClient" />

<addname="Prototype1.Properties.Settings.dbConnection"connectionString="Data Source=ISHTIAQUE\SQLEXPRESS;Initial Catalog=&quot;North Pole 1.1SQL&quot;;Integrated Security=True" />

</connectionStrings>

</configuration>

I was trying to replace my connectionStrings. Instead of writing the following code block:

/*

string connectionString ="Integrated Security=SSPI;Persist Security Info=False;" +

"Initial Catalog=North Pole 1.1SQL;Data Source=ishtiaque\\sqlexpress";

SqlConnection Conn =newSqlConnection(connectionString);

Conn.Open();

*/

I wrote:

SqlConnection Conn =newSqlConnection(ConfigurationSettings.AppSettings["Prototype1.Properties.Settings.North_Pole_1_1SQLConnectionString"].ToString());

Conn.Open();

But it doesn't work. I ger one warning message at the compile time and one error message at run time.

Warning : 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!

error message:"NullReferenceException was unhandled"

After that my app stops executing.

Can anyone please help me out this problem?

[6008 byte] By [IshtiaqueHussain] at [2007-12-22]
# 1
That was the way to do it in .NET 1.1

In 2.0, add a project reference to System.Configuration.dll

you can get the connection string like this:

System.Configuration.ConfigurationManager.ConnectionStrings("dbname").ConnectionString;

subdigital at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

Thank you subdigital.

I used the follwoing lines in my app.config file:

<appSettings>

<add key="dbConn" value="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=North Pole 1.1SQL;Data Source=ishtiaque\SQLEXPRESS"/>

</appSettings>

Then in my code Used the following lines to connect:

private SqlConnection Conn = new SqlConnection(ConfigurationSettings.AppSettings["dbConn"]);

It works pretty fine. But I get the Following Error message:

'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'

But unfortunately I could not find any use of 'System.Configuration.ConfigurationManager.AppSettings'...So kept using the above line though it shows Warning Message for every use!!

Take care.

-Ishtiaque

IshtiaqueHussain at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

Add the System.Configuration as Reference to your project. You will get all the classes unded Configuration including ConfigurationManager!

Thanks,

Dhileep
Dhileep at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
Hi Dhileep,

I had used "using System.Configuration" at the top of my project's files where ever I used the 'dbConn'. But still was getting the same warning message.

I suppose that is what you meant by saying "Add the System.Configuration as Reference to your project".

Thanks
-Ishtiaque

IshtiaqueHussain at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5
You can also use My.Settings.MyConnectionString (obviously rename "MyConnectionString" to the name stored in your config file). i would also recommend using a name not quite so complex

JeffWharton at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...