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.
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.