1

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.

Marc Bernier
  • 2,928
  • 27
  • 45

5 Answers5

15

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
  • 83,269
  • 19
  • 178
  • 237
8

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

Richard
  • 106,783
  • 21
  • 203
  • 265
3

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

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

rpf
  • 3,612
  • 10
  • 38
  • 47
  • 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 Mar 04 '09 at 15:43
  • 1
    Yes it was! How embarrassing... In my defense, } and ) look very similar in VS2008's default font. – Marc Bernier Mar 04 '09 at 15:44
  • @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 Mar 04 '09 at 15:45
  • Why this is the accepted answer? last posted, and Richard already has answered it. – Sunny Milenov Mar 04 '09 at 16:43
  • 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 Mar 04 '09 at 20:29
  • 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 Mar 04 '09 at 20:30
0

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
  • 6,702
  • 8
  • 44
  • 60
0

Why StringBuilder is better than string.Format?

Please check this Is String.Format as efficient as StringBuilder to see why...

Community
  • 1
  • 1
rpf
  • 3,612
  • 10
  • 38
  • 47