Solved

ConfigurationSettings - what's wrong?

Posted on 2003-11-12
6
573 Views
Last Modified: 2010-04-16
This works:

String connString="localhost\\DBserver; database=db; uid=sa; password=;";
 .....


This doesn't:

in web.config:
<configuration>
  <appSettings>
     <add key="connString" value="localhost\\DBserver; database=db; uid=sa; password=;" /add>
  </appSettings>

in .cs:
using System.Configuration;
  ......
String connString=ConfigurationSettings.AppSettings["connString"];
  ......

Error: "SQL server does not exist or access denied."

So the problem might be because IIS grabs another web.config file.
I find 1 web.config under wwwroot\projectName. Others are under C:\program Files\MS Visual Studio .NET\....
How do I know if the program is reading from another web.config?

Thanks.

0
Comment
Question by:samliam
6 Comments
 
LVL 1

Expert Comment

by:keshavap
Comment Utility
Hello,

you have made one small mistake in the configuration file.

<configuration>
  <appSettings>
     <add key="connString" value="localhost\\DBserver; database=db; uid=sa; password=;" /add>
  </appSettings>

Replace the add tag with with & try.
<add key="connString" value="localhost\\DBserver; database=db; uid=sa; password=;" />

cheers!
Keshav.
0
 

Author Comment

by:samliam
Comment Utility
Sorry that was just a type here. I had it right in my code, i.e.e "/>" at the end. didn't work.
0
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
Replace "localhost\\DBserver" with "localhost\DBserver"
In the String \\ is an escaped \
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Expert Comment

by:sedmans
Comment Utility
I normallu have connection strings in the following format

Server=localhost\DBserver; database=db; User Id=sa; pwd=;

I am not sure whether or not you need the escaped \ in the config file.
0
 

Author Comment

by:samliam
Comment Utility
it works.
However, if the String is defined in the c# code, one '\' does not work, it has to be '\\', while in web.config, it's the other way around.

explain?
0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 50 total points
Comment Utility
You have two options in string parsing in C#:
Standard is C++ string parsing where escaped characters like <CR> and <LF> are written as \r and \n. If you want to write a backslash then you have to double it.
When you set an "@" in front of the string you switch to VB string parsing where you need to double the quotes place a single quote in the string else the string is terminated.

So you can write either:
String connString=@"localhost\DBserver; database=db; uid=sa; password=;";
or
String connString="localhost\\DBserver; database=db; uid=sa; password=;";

The result is the same. It depends on the string if it's easier to use the VB-style or C++-style. I prefer VB-style for filepaths.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to read XML file attributes... 17 39
optimize  c# code 7 48
.NET Error 7 40
C# Application Local DB Connection String 23 57
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

7 Experts available now in Live!

Get 1:1 Help Now