Solved

ConfigurationSettings - what's wrong?

Posted on 2003-11-12
6
574 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fomat Json objects 6 28
C# Offline Apllication 5 54
Anyway to make "All" the default in the dropdown? 6 34
C# Json POSt to Rest API 4 36
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

912 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

25 Experts available now in Live!

Get 1:1 Help Now