Solved

ConfigurationSettings - what's wrong?

Posted on 2003-11-12
6
575 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
ID: 9737850
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
ID: 9738248
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
ID: 9740540
Replace "localhost\\DBserver" with "localhost\DBserver"
In the String \\ is an escaped \
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:sedmans
ID: 9741087
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
ID: 9741608
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
ID: 9747240
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 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