André Krijnen

Tag: csharp


by on Apr.02, 2008, under Programming

Well I know that for VS 2005 or VS 2008 can maintain your web or app configuration file with alot of easy. Well adding shouldn’t be a harsh, but updating still is a somekind of problem. Allright, normally you have a static connectionstring for your (web)application. Well the company that I work for has an application that isn’t static at all.

Everything is dynamic, even the connectionstring. You know why? Well the application that someone made was used for multiple databases. And it is used for importing data. You can change the connectionstring by editing the web or app config. But that wasn’t an option. The people that use this application should change the connectionstring by the application itself.

Allright, the fellow that created this nice application wasn’t aware of the connectionstring configsection. So he didn’t used that part, but I said, well if you change a connectionstring, you can maintain it by ease with code. So I had to write two simple functions. Adding and updating of connectionstrings.

The first part of the code is adding the connectionstring. Well I used this code the Windows Application so I didn’t include any for web applications.

Well opening of the configuration file is used by the configurationmanager. The ConfigurationManager.OpenExeConfiguration is used for opening the app.config, with ConfigurationUserLevel none. So no user level is needed.

Then you take out the ConnectionString section.

/// <summary>
  1.         /// Add a connection string to the connection
  2.         /// strings section and store it in the
  3.         /// configuration file.
  4.         /// &lt;/summary&gt;
  5.         /// &lt;param name="csName"&gt;The name of the property.&lt;/param&gt;
  6.         /// &lt;param name="connectionString"&gt;The connectionstring as specified.&lt;/param&gt;
  7.         public static void AddConnectionStrings(string csName, string connectionString)
  8.         {
  10.             // Get the configuration file.
  11.             System.Configuration.Configuration config =
  12.                 ConfigurationManager.OpenExeConfiguration(
  13.                 ConfigurationUserLevel.None);
  15.             // Add the connection string.
  16.             ConnectionStringsSection csSection = config.ConnectionStrings;
  17.             csSection.ConnectionStrings.Add(
  18.                 new ConnectionStringSettings(csName,
  19.                     connectionString, "System.Data.SqlClient"));
  21.             // Save the configuration file.
  22.             config.Save(ConfigurationSaveMode.Full);
  23.         }

Well for updating it was hard to find any usefull code to update a connectionstring. It wasn’t possible to update a connectionstring, so I had the remove the connectionstring, and add a new one. Well isn’t that odd?? Microsoft left something undone… Well maybe it isn’t usefull to change your app.config, but well, I had to make it anyhow. So dynamic app.config or web.config files is little bit strange, because you can’t dynamically update it. No you have to remove it first by the configurationmanager.

<pre lang=”csharp”>

/// <summary>
  1.         /// First remove the old connectionstring and after that
  2.         /// add a connection string to the connectionstrings
  3.         /// section and store it in the configuration file.
  4.         /// &lt;/summary&gt;
  5.         /// &lt;param name="csName"&gt;The name of the property.&lt;/param&gt;
  6.         /// &lt;param name="connectionString"&gt;The connectionstring as specified.&lt;/param&gt;
  7.         public static void UpdateConnectionStrings(string csName, string connectionString)
  8.         {
  9.             // Get the configuration file
  10.             System.Configuration.Configuration config =
  11.                 ConfigurationManager.OpenExeConfiguration(
  12.                 ConfigurationUserLevel.None);
  14.             // Remove the existing connectionstring.
  15.             config.ConnectionStrings.ConnectionStrings.Remove(csName);
  16.             // Add the connectionstring
  17.             ConnectionStringsSection csSection = config.ConnectionStrings;
  18.             csSection.ConnectionStrings.Add(
  19.                 new ConnectionStringSettings(csName,
  20.                 connectionString, "System.Data.SqlClient"));
  22.             // Save the configuration file
  23.             config.Save(ConfigurationSaveMode.Full);
  24.         }
  25. &lt;/pre&gt;
Leave a Comment :, , , , , , , , more...

Generating Excel (XmlSpreadSheet) in C# Part 1.

by on Nov.09, 2007, under Programming

If you want to export data with VB.NET of C# you can do it with the Office API’s delivered with your Office API’s, but I wasn’t to happy with the Excel Object. After doing some research on the internet I discovered alot of ways to write your own manner to Generate an Excel document with the XmlSpreadsheet. Well after doing some tryouts with Excel I discovered that Excel has alot of options how to save your data.

  • Excel Workbook (*.xlsx) Office 2007 Format
  • Excel Macro-Enabled Workbook (*.xlsm) Office 2007 Format
  • Excel Binary Workbook (*.xlsb) Office 2007 Format
  • Excel 97-2003 Workbook (*.xls)
  • XML Data (*.xml)
  • Single File Web Page (*.mht; *.mhtml)
  • Web Page (*.htm; *.html)
  • Excel Template (*.xltx) Office 2007 Format
  • Excel Macro-Enabled Template (*.xltm) Office 2007 Format
  • Excel 97-2003 Template (*.xlt)
  • Text (Tab Delimited) (*.txt)
  • Unicode Text (*.txt)
  • XML Spreadsheet (*.xml)
  • Microsoft Excel 5.0/95 Workbook (*.xls)
  • CSV (Comma Delimited) (*.csv)
  • Formatted Text (Space Delimited) (*.prn)
  • Text (Macintosh) (*.txt)
  • Text (MS-DOS) (*.txt)
  • CSV (Macintosh) (*.csv)
  • CSV (MS-DOS) (*.csv)
  • DIF (Data Interchange Format) (*.dif)
  • SYLK (Symbolic Link) (*.slk)
  • Excel Add-In (*.xlam) Office 2007 Format
  • Excel 97-2003 Add-In (*.xla)

You’ll see that the XML Spreadsheet normally would be saved in the Extension of *.xml, however you can save this also as *.xls. Why is that? You would ask? Well Excel has alot of manners how to open or save data. If you would have a CSV file delimited in Comma’s or Tab delimited files you can also open this with Excel. And you can see that the data is perfectly rendered in the Spreadsheet.

Let’s see where a XML SpreadSheet starts with:

  1. < ?xml version=“1.0”?>
  2. < ?mso-application progid=“Excel.Sheet”?>
  3. <workbook xmlns=“urn:schemas-microsoft-com:office:spreadsheet”
  4. xmlns:o=“urn:schemas-microsoft-com:office:office”
  5. xmlns:x=“urn:schemas-microsoft-com:office:excel”
  6. xmlns:ss=“urn:schemas-microsoft-com:office:spreadsheet”
  7. xmlns:html=“”>
  8. <documentproperties xmlns=“urn:schemas-microsoft-com:office:office”>
  9. <author>Author</author>
  10. <lastauthor>LastAuthor</lastauthor>
  11. <created>11-09-2007</created>
  12. <version>12.00</version>
  13. </documentproperties>
  14. <excelworkbook xmlns=“urn:schemas-microsoft-com:office:excel”>
  15. <protectstructure>False</protectstructure>
  16. <protectwindows>False</protectwindows>
  17. </excelworkbook>
  18. </workbook>

After this section you will include you styles. When writing your styles you can build a Excel Workbook manually and make some different styles init. After that you can save this Workbook to a XML Spreadsheet. When you finally saved a Workbook it will generate for example the following styles:

  1. <styles>
  2. <style ss:ID=“Default” ss:Name=“Normal”>
  3. <alignment ss:Vertical=“Bottom”/>
  4. <borders />
  5. <font ss:FontName=“Calibri” x:Family=“Swiss” ss:Size=“11” ss:Color=“#000000”/>
  6. <interior />
  7. <numberformat />
  8. <protection />
  9. </style>
  10. <style ss:ID=“s62”>
  11. <numberformat ss:Format=“Fixed”/>
  12. </style>
  13. <style ss:ID=“s63”>
  14. <numberformat ss:Format=“&quot;€&quot;#,##0.00”/>
  15. </style>
  16. <style ss:ID=“s65”>
  17. <numberformat ss:Format=“0%”/>
  18. </style>
  19. <style ss:ID=“s66”>
  20. <numberformat ss:Format=“Short Date”/>
  21. </style>
  22. <style ss:ID=“s67”>
  23. <numberformat ss:Format=“[$-F400]h:mm:ss\ AM/PM”/>
  24. </style>
  25. <style ss:ID=“s68”>
  26. <numberformat ss:Format=“Percent”/>
  27. </style>
  28. <style ss:ID=“s69”>
  29. <numberformat ss:Format=“#\ ?/?”/>
  30. </style>
  31. <style ss:ID=“s70”>
  32. <numberformat ss:Format=“Scientific”/>
  33. </style>
  34. <style ss:ID=“s71”>
  35. <numberformat ss:Format=“@”/>
  36. </style>
  37. <style ss:ID=“s72”>
  38. <numberformat ss:Format=“”/>
  39. </style>
  40. <style ss:ID=“s73”>
  41. <numberformat ss:Format=“Standard”/>
  42. </style>
  43. </styles>

What you can do is giving alot of extra options to your excel sheet. Every above style has a particular function. You can specify a column and give it a specific style. So that the column will be formatted by for example Scientific functions. After the styles there will be the data for your spreadsheet.

First we start with a new Worksheet. Every worksheet has a name . Let’s start with that:

  1. <worksheet ss:Name=“ExampleSheet”>
  2. </worksheet>

When we have done that we can add our table with data in our Worksheet. It will look like this.

  1. <worksheet ss:Name=“ExampleSheet”>
  2. <table ss:ExpandedColumnCount=“12” ss:ExpandedRowCount=“1” x:FullColumns=“1”
  3. x:FullRows=“1” ss:DefaultRowHeight=“15”>
  4. <column ss:Index=“2” ss:StyleID=“s62” ss:AutoFitWidth=“0”/>
  5. <column ss:StyleID=“s63” ss:Width=“53.25”/>
  6. <column ss:StyleID=“s65” ss:AutoFitWidth=“0”/>
  7. <column ss:StyleID=“s66” ss:AutoFitWidth=“0” ss:Width=“67.5”/>
  8. <column ss:StyleID=“s67” ss:Width=“54.75”/>
  9. <column ss:StyleID=“s68” ss:AutoFitWidth=“0”/>
  10. <column ss:StyleID=“s69” ss:AutoFitWidth=“0”/>
  11. <column ss:StyleID=“s70” ss:AutoFitWidth=“0”/>
  12. <column ss:StyleID=“s71” ss:AutoFitWidth=“0”/>
  13. <column ss:StyleID=“s72” ss:AutoFitWidth=“0”/>
  14. <column ss:StyleID=“s73” ss:AutoFitWidth=“0”/>
  15. <row>
  16. <cell><data ss:Type=“String”>General</data></cell>
  17. <cell><data ss:Type=“Number”></data></cell>
  18. <cell><data ss:Type=“Number”>12345</data></cell>
  19. <cell><data ss:Type=“String”>€</data></cell>
  20. <cell><data ss:Type=“DateTime”>2007-03-14T00:00:00.000</data></cell>
  21. <cell><data ss:Type=“DateTime”>1899-12-31T13:30:00.000</data></cell>
  22. <cell><data ss:Type=“Number”>1</data></cell>
  23. <cell><data ss:Type=“Number”>0.33333333333333331</data></cell>
  24. <cell><data ss:Type=“Number”>0.33333333333333331</data></cell>
  25. <cell><data ss:Type=“String”>text</data></cell>
  26. <cell><data ss:Type=“String”>Breda</data></cell>
  27. <cell><data ss:Type=“Number”>12345</data></cell>
  28. </row>
  29. </table>
  30. </worksheet>
Leave a Comment :, , , , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!


A few highly recommended websites...