tags:

views:

424

answers:

6

Using C#, I need to build a connection string from a few AppSettings. If I do this:

Connection = string.Format("Data Source={0};Initial Catalog={1);User Id={2};Password={3};",
  ConfigurationManager.AppSettings.Get("CartServer"),
  ConfigurationManager.AppSettings.Get("CartDatabase"),
  ConfigurationManager.AppSettings.Get("CartUserName"),
  ConfigurationManager.AppSettings.Get("CartPassword"));

I get an invalid format string exception. I narrowed it down to the "Password=" part of the format string (ie, "Passwork=" works). There's an easy enough work-around:

Connection = string.Format("Data Source={0};Initial Catalog={1);User Id={2};{3}={4};",
  ConfigurationManager.AppSettings.Get("CartServer"),
  ConfigurationManager.AppSettings.Get("CartDatabase"),
  ConfigurationManager.AppSettings.Get("CartUserName"),
  "Password",ConfigurationManager.AppSettings.Get("CartPassword")); // Lame!!!

But what's the real story with the "Password"? I checked MSDN and a few other sites but came up empty. Oh, if it matters, this is a WCF service.

+14  A: 

Your problem is the {1) in the format string; it should be {1} (you have closed the brace with a parenthesis)

This is causing your FormatException because your format string is now invalid. Why your second entry works is beyond me though.

Edit: I agree with the other Richard that you should consider using a connection string builder object.

Richard Szalay
+7  A: 

Have you considered the DbConnectionStringBuilder class (or one of its subtypes)?

Richard
+3  A: 

Obvious.... your problem is because this: "(".

Why don't you use StringBuider class, is easier :)

rpf
Don't you mean this: ")" ?
Richard Szalay
gets my vote, but I'd like to see how StringBuilder is easier. string.Format (once you get used to the curly brackets) seems about as easy as easy gets. I know there are performance advantages to StringBuilders, but I haven't seen anyone say it's easier.
dnord
Yes it was! How embarrassing... In my defense, } and ) look very similar in VS2008's default font.
Marc Bernier
@dnord - out of interest, why did it get your vote? He answered last, and specified the wrong parenthesis as being the cause. It's possible I've completely missed something.
Richard Szalay
Why this is the accepted answer? last posted, and Richard already has answered it.
Sunny
I just switched it, they all show up as '4 hrs ago' and this one was at the top, I assumed it was the 1st answer given.
Marc Bernier
Sorry about this change of parenthesis....... @Richard- I did answer this question before see your answer. StringBuilder is, for me, the best way to concatenate strings... the best way and the easier. Just because :P And stringbuilder is the performante, but not so much
rpf
A: 

I just Google'd and I had no idea a "DbConnectionStringBuilder" even existed. Wow, you learn something new everyday.

Also, for other connection strings, check this site out: http://www.connectionstrings.com/

Chris
A: 

Why stringbuilder? String.Format() is entirely appropriate here.

Cheeso
A: 

Why StringBuilder is better than string.Format?

Please check this http://stackoverflow.com/questions/6785/is-string-format-as-efficient-as-stringbuilder to see why...

rpf