From: GiJeet on
Hello, I have a winforms app with some values in the app.config file I
use to create a connection string to excel. Some of the values
require quotes around the values within the connection string.

Here is an example of the connection string:
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";

Note the quotes around the Extended Properties values.

I have the following String.Format code (which doesn’t work):
sConnStr = String.Format(
"Provider={0};Data Source={1};Extended
Properties={2};HDR={3};",
Provider, FilePath, ExtendedProperties, SheetHeader
);

Where Provider, FilePath, ExtendedProperties, SheetHeader are all vars
holding the values from the app.config. However there are no quotes
around the Extended Properties values.

I tried to escape it but it produces an error:
sConnStr = String.Format(
"Provider={0};Data Source={1};Extended
Properties=/”{2};HDR={3};/”",
Provider, FilePath, ExtendedProperties, SheetHeader
);

I even tried to put the quotes around the values in the app.config but
that threw an error also.
Here’s the app.config section.

<appSettings>
<add key="BSFileName" value="C:\_ExcelReports\BalanceSheet.xlsx"/>
<add key="BSProvider" value="Microsoft.ACE.OLEDB.12.0"/>
<add key="BSExtendedProperties" value="Excel 12.0 Xml"/>
<add key="BSHeader" value="NO"/>
<add key="BSSheetName" value="Sheet1"/>
<add key="BSCell_TotalAssets" value="B4"/>
</appSettings>

So, how to have quotes around values in a String.Format function?
Help is appreciated. You can even combine the ExtendedProperties &
SheetHeader into one variable. Thanks
G
From: Alberto Poblacion on
"GiJeet" <gijeet(a)yahoo.com> wrote in message
news:d45de0b8-23e4-4301-aab5-33fe3cfea736(a)h27g2000yqm.googlegroups.com...
> I tried to escape it but it produces an error:
> sConnStr = String.Format(
> "Provider={0};Data Source={1};Extended
> Properties=/�{2};HDR={3};/�",
> Provider, FilePath, ExtendedProperties, SheetHeader
> );

You have used the wrong escape character. It should be \ rather than /.
Also, be careful with the double-quote character. The text you pasted in the
message contains � instead of " (they may look almost the same, depending on
the font that you are using, but they are different characters).

> I even tried to put the quotes around the values in the app.config but
> that threw an error also.
> [...]
> <add key="BSExtendedProperties" value="Excel 12.0 Xml"/>

If you want to insert quotes in the xml file, you can use &quot;

<add key="BSExtendedProperties" value="&quot;Excel 12.0 Xml&quot;"/>


From: GiJeet on
Thanks, I'll fix the code and try the html quotes.
From: Arne Vajhøj on
On 22-04-2010 10:33, GiJeet wrote:
> I tried to escape it but it produces an error:
> sConnStr = String.Format(
> "Provider={0};Data Source={1};Extended
> Properties=/�{2};HDR={3};/�",
> Provider, FilePath, ExtendedProperties, SheetHeader
> );

Besides using the correct escape \ and the correct quote ",
then you may find using @ more readable.

sConnStr = String.Format("Provider={0};Data Source={1};Extended
Properties=\"{2};HDR={3};\"",
Provider, FilePath, ExtendedProperties, SheetHeader);

vs.

sConnStr = String.Format(@"Provider={0};Data Source={1};Extended
Properties=""{2};HDR={3};""",
Provider, FilePath, ExtendedProperties, SheetHeader);

Arne