By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,806 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,806 IT Pros & Developers. It's quick & easy.

Accessing web.config during unit tests

P: n/a
I am a relative newbie to .NET - two weeks, using everyday. My problem
is this. I have my asp.net application where the web.config contains
the database connection string. Normally i access this fine with
Application[ "connectionString" ].

However, I am now trying to use a unit testing framework (csunit
1.9.2) and when I try and run the tests I have created it is saying
"The ConnectionString property has not been initialized". The piece
of code it is using runs fine if I access it by clicking a button
that triggers it in one of my aspx pages, but not when it is
initiated by the unit test class.

Potential troubles - the class I am running the method in is a
standalone utility class ie it doesnt have a aspx page associated.
Dont know if this matters or not....
public string getXmlCommandByName( string commandName )
{
SqlConnection conn;
using (conn = new SqlConnection(
ConfigurationSettings.AppSettings[ "connectionString"
] ) )
{
conn.Open();

string sqlString = "SELECT xml FROM PDSXML WHERE command =
" + commandName;
SqlCommand request = new SqlCommand( sqlString, conn );
SqlDataReader results =
request.ExecuteReader();

return results.GetValue( 0 ).ToString();
}
}

As you can see i have been trying using different ways of accessing
the web.config

Help!

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
If you are storing values in web.config, you access them using
ConfigurationSettings.AppSettings, which will pull values from the
appSettings portion of the web.config.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"ChrisC" <ch************@hotmail-dot-com.no-spam.invalid> wrote in message
news:40**********@127.0.0.1...
I am a relative newbie to .NET - two weeks, using everyday. My problem
is this. I have my asp.net application where the web.config contains
the database connection string. Normally i access this fine with
Application[ "connectionString" ].

However, I am now trying to use a unit testing framework (csunit
1.9.2) and when I try and run the tests I have created it is saying
"The ConnectionString property has not been initialized". The piece
of code it is using runs fine if I access it by clicking a button
that triggers it in one of my aspx pages, but not when it is
initiated by the unit test class.

Potential troubles - the class I am running the method in is a
standalone utility class ie it doesnt have a aspx page associated.
Dont know if this matters or not....
public string getXmlCommandByName( string commandName )
{
SqlConnection conn;
using (conn = new SqlConnection(
ConfigurationSettings.AppSettings[ "connectionString"
] ) )
{
conn.Open();

string sqlString = "SELECT xml FROM PDSXML WHERE command =
" + commandName;
SqlCommand request = new SqlCommand( sqlString, conn );
SqlDataReader results =
request.ExecuteReader();

return results.GetValue( 0 ).ToString();
}
}

As you can see i have been trying using different ways of accessing
the web.config

Help!

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet
News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000
Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---

Nov 18 '05 #2

P: n/a
> using (conn = new SqlConnection(
ConfigurationSettings.AppSettings[ "connectionString"
] ) )


Like that? :) (from the code i quoted)

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 18 '05 #3

P: n/a
My bad - I just noticed that in the top text, where you were looking at
Application.

If you are not actively inside of a web application, you won't find the
web.config. However, you can put an app.config in that directory and put
sections in there, which is what you would do for a WinForms app. I haven't
tried this exact scenario specifically, but that's why it isn't working, and
what you should be able to do in order to fix things up. Sorry I wasn't
reading closely enough before. :-S

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"ChrisC" <ch************@hotmail-dot-com.no-spam.invalid> wrote in message
news:40**********@127.0.0.1...
using (conn = new SqlConnection(
ConfigurationSettings.AppSettings[ "connectionString"
] ) )


Like that? :) (from the code i quoted)

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet
News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000
Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---

Nov 18 '05 #4

P: n/a
No worries. It seems to be a bit of a pain this. Its a web application
and i dont want to create a effectively duplicate config file just
for the tests as that would be silly as it wouldnt be using the
actual config settings used by the application. I tried writing a
class to access the web.config directly but it wasnt having any of it
- in use by another process (i assume the web app keeps it constantly
open).

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.