<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dot Net Thoughts</title>
	<atom:link href="http://www.dotnetthoughts.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Tue, 09 Mar 2010 18:50:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Developing simple Windows 7 Gadget &#8211; Part 3</title>
		<link>http://www.dotnetthoughts.net/2010/03/09/developing-simple-windows-7-gadget-part-3/</link>
		<comments>http://www.dotnetthoughts.net/2010/03/09/developing-simple-windows-7-gadget-part-3/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 18:50:11 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Windows Vista]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=795</guid>
		<description><![CDATA[In this I am exploring another two features of Gadget API, one is Docking and other is Flyout. The Docking feature is helpful in Windows Vista, because the gadget lives only in Vista Sidebar, but in Windows 7 it can be anywhere in the desktop. The Docking feature helps to change the size of Gadget, [...]]]></description>
			<content:encoded><![CDATA[<p>In this I am exploring another two features of Gadget API, one is Docking and other is Flyout. The Docking feature is helpful in Windows Vista, because the gadget lives only in Vista Sidebar, but in Windows 7 it can be anywhere in the desktop. The Docking feature helps to change the size of Gadget, in Windows Vista, the docking / un-docking events will automatically fired, when we drag the gadget out of the sidebar, but in Windows 7 another button will displayed in the Gadget toolbox. You can query the current state of a gadget with System.Gadget.docked. It returns true if docked, false if undocked.</p>
<div id="attachment_798" class="wp-caption alignnone" style="width: 170px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/03/image.png"><img class="size-full wp-image-798" title="Docking / UnDocking button in Sidebar" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/03/image.png" alt="Docking / UnDocking button in Sidebar" width="160" height="273" /></a><p class="wp-caption-text">Docking / UnDocking button in Sidebar</p></div>
<p>There are also two events that you can monitor, System.Gadget.onDock and System.Gadget.onUndock, to determine when the docking state changes.</p>
<p>You can change the Width of the gadget by calling <em>document.body.style.width = &#8220;130px&#8221;;</em>. And the Docking and Undocking events should be attached on the Page_load.</p>
<pre class="brush: jscript;">
System.Gadget.onDock = dockStateChanged;
System.Gadget.onUndock = dockStateChanged;
</pre>
<p>And the dockStateChanged function is</p>
<pre class="brush: jscript;">
function dockStateChanged() {
//Checks the current state of the Gadget, if Docked state, setting the width to 130px;
//Otherwise changing to 330px;
	if (System.Gadget.docked) {
		document.body.style.width = &quot;130px&quot;;
document.getElementById(&quot;RSSOutput&quot;).style.width = &quot;125px&quot;;
	} else {
		document.body.style.width = &quot;330px&quot;;
document.getElementById(&quot;RSSOutput&quot;).style.width = &quot;325px&quot;;
	}
}
</pre>
<p>Another feature is Flyout. Flyouts used to extend the Gadget UI. The flyouts are system modal—only one can be displayed at a time. And when a gadget loses focus, its flyout will close. API for Flyouts available under System.Gadget.Flyout. As the flyouts is completely a separate window, with its own DOM, helps to add / create controls dynamically in it. Also you need to specify the flyout file, like Settings file.</p>
<p>The System.Gadget.Flyout.document returns the Document object of the flyout window. To display a flyout you need to set Flyout.show to True and to hide, set it to false. This property also returns the current state of flyout. But we cannot control the location of side bar, it is managed by Sidebar or OS.</p>
<pre class="brush: jscript;">
System.Gadget.Flyout.file = &quot;flyout.html&quot;;
</pre>
<p>There are certain events also available for Flyouts</p>
<pre class="brush: jscript;">
System.Gadget.Flyout.onShow = function()
{
//Do something, when flyout is opening, may be a dynamic control creation
}
System.Gadget.Flyout.onHide = function()
{
//Do some code.
}
</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dotnetthoughts.net%2F2010%2F03%2F09%2Fdeveloping-simple-windows-7-gadget-part-3%2F&amp;linkname=Developing%20simple%20Windows%207%20Gadget%20%26%238211%3B%20Part%203">Share/Save</a>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/03/09/developing-simple-windows-7-gadget-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing simple Windows 7 Gadget &#8211; Part 2</title>
		<link>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-2/</link>
		<comments>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-2/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 18:05:39 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Gadget]]></category>
		<category><![CDATA[Gadget API]]></category>
		<category><![CDATA[Sidebar Gadget]]></category>
		<category><![CDATA[Windows Vista]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=787</guid>
		<description><![CDATA[In my last post I have created a simple Windows 7 Gadget for testing Regular expressions. In this post we look into the Gadget Options dialog and Gadget API, which helps to display the options dialog and save the preferences from the User. The options dialog is a nice feature used in Gadgets to manage [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-1/">last post </a>I have created a simple Windows 7 Gadget for testing Regular expressions. In this post we look into the Gadget Options dialog and Gadget API, which helps to display the options dialog and save the preferences from the User. The options dialog is a nice feature used in Gadgets to manage the user preferences. It is also an HTML file. Unlike the main Gadget HTML file, which is specified in the XML Definition file, the Options HTML File is specified in the main gadget file or in the main Javascript file, generally in the gadget initialization area of your script. In the RegEx Tester example, I am using the RegEx function flags as customization options.</p>
<div id="attachment_790" class="wp-caption alignnone" style="width: 240px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/settings_clicked.png"><img class="size-full wp-image-790" title="RegEx Tester Settings " src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/settings_clicked.png" alt="RegEx Tester Settings " width="230" height="327" /></a><p class="wp-caption-text">RegEx Tester Settings</p></div>
<p>So I have created an HTML File with 3 checkboxes and wrote a javascript function to get the user selected options, create the flags variable, and when closing the Options dialog, save the user preferences. To create the settings or options dialog, set the SettingsUI property.  </p>
<pre class="brush: jscript;">
System.Gadget.settingsUI = &quot;settings.html&quot;;
</pre>
<p>After setting the SettingsUI property, the Gadget will display a Options button in the Gadget handle.</p>
<div id="attachment_789" class="wp-caption alignnone" style="width: 240px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/RegExTester_Settings.png"><img src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/RegExTester_Settings.png" alt="Settings Button" title="Settings Button " width="230" height="280" class="size-full wp-image-789" /></a><p class="wp-caption-text">Settings Button </p></div>
<p>The Gadget API also supports various events / callback functions for the Options dialog, like SettingsClosing, SettingsClosed etc, which helps to save the Preferences and Read the preferences in the Gadget.</p>
<pre class="brush: jscript;">
System.Gadget.onSettingsClosed = settingsClosed;
function settingsClosed(event) {
	//OK Button clicked.
if (event.closeAction == event.Action.commit) {
	//Load settings here.
}
}
</pre>
<p>Gadget API supports various methods to read and write settings. For writing setting we can use System.Gadget.Settings.write or System.Gadget.Settings.writeString. Both the functions will expects a key/value pair. And for reading can use System.Gadget.Settings.read or System.Gadget.Settings.readString, both of these functions take a key and return a value. If the key does not exist (for instance if it has never been written) both will return a value of undefined. Use System.Gadget.Settings.xxxxString method, if you are working with string values. All the settings are stored in Settings.ini file. And it is available in the &#8220;C:\Users\\AppData\Local\Microsoft\Windows Sidebar&#8221; location.<br />
Saving the preferences</p>
<pre class="brush: jscript;">
System.Gadget.Settings.write(&quot;RegExOptionsSaved&quot;, &quot;true&quot;);
System.Gadget.Settings.write(&quot;RegExOption&quot;, escape(result));
</pre>
<p>And reading the Preferences </p>
<pre class="brush: jscript;">
if (System.Gadget.Settings.read(&quot;RegExOptionsSaved&quot;)) {
regExOptions = unescape(System.Gadget.Settings.read(&quot;RegExOption&quot;));
}
</pre>
<p><strong>RegEx Object constructor</strong> </p>
<pre class="brush: jscript;">
var re = new RegEx(&quot;Pattern&quot;,&quot;Flags&quot;);
</pre>
<p>And the Flags are</p>
<ol>
<li>
Global Search &#8211; g &#8211; The global search flag makes the RegExp search for a pattern throughout the string, creating an array of all occurrences it can find matching the given pattern.</li>
<li>Ignore Case &#8211; i &#8211; The ignore case flag makes a regular expression case insensitive. For international coders, note that this might not work on extended characters such as å, ü, ñ, æ.</li>
<li>Multiline Input &#8211; m &#8211; This flag makes the beginning of input (^) and end of input ($) codes also catch beginning and end of line respectively.</li>
</ol>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dotnetthoughts.net%2F2010%2F02%2F24%2Fdeveloping-simple-windows-7-gadget-part-2%2F&amp;linkname=Developing%20simple%20Windows%207%20Gadget%20%26%238211%3B%20Part%202">Share/Save</a>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing simple Windows 7 Gadget &#8211; Part 1</title>
		<link>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-1/</link>
		<comments>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-1/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 14:50:19 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Gadget]]></category>
		<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Sidebar Gadget]]></category>
		<category><![CDATA[Windows Vista]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=777</guid>
		<description><![CDATA[Gadgets are simple and lightweight applications, which can be developed using HTML, Javascript and CSS. Gadgets can also include Image files. In Windows Vista, we can create Gadgets by zipping the files, and rename the it with .Gadget extension. In Windows 7 we can create Gadget by creating the .Gadget folder in C:\Program Files\Windows Sidebar\Gadgets\ [...]]]></description>
			<content:encoded><![CDATA[<p>Gadgets are simple and lightweight applications, which can be developed using HTML, Javascript and CSS. Gadgets can also include Image files. In Windows Vista, we can create Gadgets by zipping the files, and rename the it with .Gadget extension. In Windows 7 we can create Gadget by creating the .Gadget folder in C:\Program Files\Windows Sidebar\Gadgets\ folder. This folder contains all the default gadgets from Microsoft.</p>
<p><strong>Creating Simple RegEx Tester Gadget</strong></p>
<p>Regular Expressions are complex but efficient technique for processing text. And the RegEx functions are available in Javascript. So I am to creating simple regex tester gadget using Javascript. You can find a simple RegEx Tester using Javascript here : <a href="http://www.regular-expressions.info/javascriptexample.html" target="_blank">http://www.regular-expressions.info/javascriptexample.html</a>.</p>
<p>For creating a Gadget we require a XML Definition file, which contains the details about the Gadget File, Links to Icons, and Copyright information. Here is skeleton a XML Definition file.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;gadget&gt;
&lt;name&gt;RegExTester&lt;/name&gt;
&lt;namespace&gt;dotnetthoughts&lt;/namespace&gt;
&lt;version&gt;1.0.0.0&lt;/version&gt;
&lt;author name=&quot;dotnetthoughts&quot;&gt;
&lt;info url=&quot;http://dotnetthoughts.net&quot; text=&quot;www.dotnetthoughts.net&quot; /&gt;
&lt;logo src=&quot;logo.png&quot; /&gt;
&lt;/author&gt;
&lt;copyright&gt;&amp;#0169; 2010&lt;/copyright&gt;
&lt;description&gt;For Testing Regular Expressions&lt;/description&gt;
&lt;icons&gt;
&lt;icon width=&quot;64&quot; height=&quot;64&quot; src=&quot;icon.png&quot; /&gt;
&lt;/icons&gt;
&lt;hosts&gt;
&lt;host name=&quot;sidebar&quot;&gt;
&lt;base type=&quot;HTML&quot; apiVersion=&quot;1.0.0&quot; src=&quot;RegExTester.html&quot; /&gt;
&lt;permissions&gt;full&lt;/permissions&gt;
&lt;platform minPlatformVersion=&quot;0.3&quot; /&gt;
&lt;/host&gt;
&lt;/hosts&gt;
&lt;/gadget&gt;
</pre>
<p>And the Tags details are</p>
<p>name: Title of your gadget.<br />
version: Version number of your gadget.<br />
author: Your name or your company’s name.<br />
info url: Web site address.<br />
info text: Friendly name for your Web site.<br />
logo src: Name of company’s logo image file.<br />
copyright: Copyright notice.<br />
description: Description of the gadget.<br />
icon src: Name of icon image file for the gadget.<br />
base src: Name of gadget’s main HTML file.</p>
<div id="attachment_780" class="wp-caption alignnone" style="width: 469px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/details.png"><img class="size-full wp-image-780" title="Gadget Details." src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/details.png" alt="Gadget Details." width="459" height="300" /></a><p class="wp-caption-text">Gadget Details.</p></div>
<p>Save this file as Gadget.xml. Next we need to create the RegExTester.html, which is the main file, nothing but simple HTML file with some UI components.</p>
<pre class="brush: xml;">

&lt;html&gt;
&lt;head&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;style.css&quot; /&gt;
&lt;script type=&quot;text/vbscript&quot; src=&quot;message.vbs&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;regexcheck.js&quot;&gt;&lt;/script&gt;
&lt;title&gt;RegEx Tester&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;1&quot; cellspacing=&quot;5&quot; bordercolor=&quot;red&quot;&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;&lt;u&gt;RegEx Tester&lt;/u&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;label&gt;Regular Expression&lt;/label&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;textarea id=&quot;txtRegExp&quot; cols=&quot;25&quot; rows=&quot;4&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;label&gt;Subject String&lt;/label&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;textarea id=&quot;txtSubject&quot; cols=&quot;25&quot; rows=&quot;4&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;button onclick=&quot;javascript:demoMatchClick();&quot;&gt;Test Match&lt;/button&gt;&amp;nbsp;&amp;nbsp;
&lt;button onclick=&quot;javascript:demoShowMatchClick();&quot;&gt;Show Match&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>You may noticed that I have included a message.vbs file, it helps to display alert() boxes from Javascript. I don&#8217;t know why alert and prompt functions are not available in Gadgets. Also we don&#8217;t need to worry about cross platform compatibility issues in Gadgets, we can write anything supported by Microsoft Internet Explorer.</p>
<p>Here is the code of Message.vbs file.</p>
<pre class="brush: vb;">
sub alert(prompt)
MsgBox prompt, 48 , &quot;RegEx Tester&quot;
end sub
</pre>
<p>And here is the contents of regexcheck.js file.</p>
<pre class="brush: jscript;">
function demoMatchClick() {
var regExCtrl = document.getElementById(&quot;txtRegExp&quot;);
var subjectCtrl = document.getElementById(&quot;txtSubject&quot;);
var re = new RegExp(regExCtrl.value);
if (subjectCtrl.value.match(re)) {
	alert(&quot;Successful match&quot;);
} else {
	alert(&quot;No match&quot;);
}
}
function demoShowMatchClick() {
var regExCtrl = document.getElementById(&quot;txtRegExp&quot;);
var subjectCtrl = document.getElementById(&quot;txtSubject&quot;);
var re = new RegExp(regExCtrl.value);
var m = re.exec(subjectCtrl.value);
if (m == null) {
	alert(&quot;No match&quot;);
} else {
	var s = &quot;Match at position &quot; + m.index + &quot;:\n&quot;;
	for (i = 0; i &lt; m.length; i++) {
		s = s + m[i] + &quot;\n&quot;;
	}
	alert(s);
}
}
</pre>
<p>Also I added a CSS file for styling the components. Also to specify the Height and Width of the Gadget. In Windows Vista, we need to limit the size of the Gadget, because the Gadget can only place in the Sidebar, but in Windows 7 we can place the Gadget anywhere in the desktop. Also I have added icon.png and logo.png files, as specified in the XML definition file.<br />
The current folder structure is like this.</p>
<div id="attachment_781" class="wp-caption alignnone" style="width: 600px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/folder_struct.png"><img class="size-full wp-image-781" title="Folder Structure" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/folder_struct.png" alt="Folder Structure" width="590" height="166" /></a><p class="wp-caption-text">Folder Structure</p></div>
<p>Now using any compression tool, compress the files, and Rename it as RegExTester.Gadget. Double click on the Gadget file, Windows will ask for a confirmation.</p>
<div id="attachment_779" class="wp-caption alignnone" style="width: 424px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/confirm.png"><img class="size-full wp-image-779" title="Gadget install Confirmation" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/confirm.png" alt="Gadget install Confirmation" width="414" height="266" /></a><p class="wp-caption-text">Gadget install Confirmation</p></div>
<p>Click Install. It will start the Gadget. Here is the screenshot of RegExTester gadget running on my Windows 7 machine.</p>
<div id="attachment_782" class="wp-caption alignnone" style="width: 240px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/RegExTester_Running.png"><img class="size-full wp-image-782" title="RegExTester Running on Windows 7" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/RegExTester_Running.png" alt="RegExTester Running on Windows 7" width="230" height="280" /></a><p class="wp-caption-text">RegExTester Running on Windows 7</p></div>
<p>This is an introduction only, I will try to post about Gadget API, Gadget Settings etc in the next post.</p>
<p>Happy Coding <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dotnetthoughts.net%2F2010%2F02%2F24%2Fdeveloping-simple-windows-7-gadget-part-1%2F&amp;linkname=Developing%20simple%20Windows%207%20Gadget%20%26%238211%3B%20Part%201">Share/Save</a>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Import Data from Excel using C# &#8211; Part 2</title>
		<link>http://www.dotnetthoughts.net/2010/02/16/import-data-from-excel-using-c-part-2/</link>
		<comments>http://www.dotnetthoughts.net/2010/02/16/import-data-from-excel-using-c-part-2/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 18:56:16 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Import XSLX]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=770</guid>
		<description><![CDATA[In my web application, we used to import data from MS Excel file. Few days before one of the client representative posted an issue that he can’t import data from XLSX (MS Excel 2007 File Format) files. Previously we were using OLE DB provider to import data from Excel file (Checkout my previous post regarding [...]]]></description>
			<content:encoded><![CDATA[<p>In my web application, we used to import data from MS Excel file. Few days before one of the client representative posted an issue that he can’t import data from XLSX (MS Excel 2007 File Format) files. Previously we were using OLE DB provider to import data from Excel file (Checkout my previous post regarding How to Import / Export from C# :<a href="http://www.dotnetthoughts.net/2009/09/23/import-export-data-in-ms-excel-using-c/"> IMPORT / EXPORT DATA IN MS EXCEL USING C#</a> ), but Microsoft worked on the Office 2007 file formats, and we are not able to connect to Excel using Microsoft.Jet.OLEDB. Yesterday I got a chance to work on this module, and thought of implementing XLSX support. After few searches I found a new provider from Microsoft to connect to Excel 2007 files, called <strong>Microsoft.ACE.OLEDB.12.0</strong>.</p>
<pre class="brush: csharp;">
/// &lt;summary&gt;
/// Imports Data from Microsoft Excel File.
/// &lt;/summary&gt;
/// &lt;param name=&quot;FileName&quot;&gt;Filename from which data need to import&lt;/param&gt;
/// &lt;returns&gt;List of DataTables, based on the number of sheets&lt;/returns&gt;
private List&lt;DataTable&gt; ImportExcel(string FileName)
{
    List&lt;DataTable&gt; _dataTables = new List&lt;DataTable&gt;();
    string _ConnectionString = string.Empty;
    string _Extension = Path.GetExtension(FileName);
    //Checking for the extentions, if XLS connect using Jet OleDB
    if (_Extension.Equals(&quot;.xls&quot;, StringComparison.CurrentCultureIgnoreCase))
    {
        _ConnectionString =
            &quot;Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0&quot;;
    }
    //Use ACE OleDb
    else if (_Extension.Equals(&quot;.xlsx&quot;, StringComparison.CurrentCultureIgnoreCase))
    {
        _ConnectionString =
            &quot;Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0&quot;;
    }

    DataTable dataTable = null;

    using (OleDbConnection oleDbConnection =
        new OleDbConnection(string.Format(_ConnectionString, FileName)))
    {
        oleDbConnection.Open();
        //Getting the meta data information.
        //This DataTable will return the details of Sheets in the Excel File.
        DataTable dbSchema = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null);
        foreach (DataRow item in dbSchema.Rows)
        {
            //reading data from excel to Data Table
            using (OleDbCommand oleDbCommand = new OleDbCommand())
            {
                oleDbCommand.Connection = oleDbConnection;
                oleDbCommand.CommandText = string.Format(&quot;SELECT * FROM [{0}]&quot;,
                    item[&quot;TABLE_NAME&quot;].ToString());
                using (OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter())
                {
                    oleDbDataAdapter.SelectCommand = oleDbCommand;
                    dataTable = new DataTable(item[&quot;TABLE_NAME&quot;].ToString());
                    oleDbDataAdapter.Fill(dataTable);
                    _dataTables.Add(dataTable);
                }
            }
        }
    }
    return _dataTables;
}
</pre>
<p>The connection string also supports HDR attribute, which decides, whether the Excel file first row is header or not. It can be either Yes or No. It can be used with both Excel 2003 and Excel 2007 files.</p>
<p>Update: You can download the Microsoft.ACE.OLEDB.12.0 provider from Microsoft : <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&#038;displaylang=en" target="_blank">2007 Office System Driver: Data Connectivity Components</a></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dotnetthoughts.net%2F2010%2F02%2F16%2Fimport-data-from-excel-using-c-part-2%2F&amp;linkname=Import%20Data%20from%20Excel%20using%20C%23%20%26%238211%3B%20Part%202">Share/Save</a>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/02/16/import-data-from-excel-using-c-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>System.InvalidOperationException – Cross-thread operation not valid</title>
		<link>http://www.dotnetthoughts.net/2010/02/05/system_invalidoperationexception_cross_thread_operation_not_valid/</link>
		<comments>http://www.dotnetthoughts.net/2010/02/05/system_invalidoperationexception_cross_thread_operation_not_valid/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 19:54:58 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Cross thread operation]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=763</guid>
		<description><![CDATA[System.InvalidOperationException &#8211; Cross-thread operation not valid: Control &#8216;xxxxx&#8217; accessed from a thread other than the thread it was created on. Normally we get this exception when we try to modify some control property from another thread. This is because when a program executes the operating system will assign a thread for the creation of UI [...]]]></description>
			<content:encoded><![CDATA[<p><em>System.InvalidOperationException &#8211; Cross-thread operation not valid: Control &#8216;xxxxx&#8217; accessed from a thread other than the thread it was created on</em>. Normally we get this exception when we try to modify some control property from another thread. This is because when a program executes the operating system will assign a thread for the creation of UI elements and for the changes of the UI. Only this thread has got the permission to change or control UI elements created. If you creates other threads with the help of Thread class from System.Threading namespace, doesn’t have enough privileges to change or control the UI elements of the Main thread. To resolve this issue,  we need write delegates and need to invoke the methods from other threads using these delegates.</p>
<p>This sample code is doing some long operation in a separate thread and try to update the UI from the new thread.</p>
<pre class="brush: csharp;">
private void Form1_Load(object sender, EventArgs e)
{
    this.threadStart = new ThreadStart(LongProcess);
    this.thread = new Thread(this.threadStart);
    this.thread.Start();
}

private void LongProcess()
{
    for (int i = 0; i &lt; 100; i++)
    {
        Thread.Sleep(100);

        //This will throw an exception like this
        //System.InvalidOperationException - Cross-thread operation not valid:
        //Control 'textBox1' accessed from a thread other than the thread it was created on.
        this.textBox1.Text = i.ToString();
    }
}
</pre>
<p>And here is the source code which will fix the issue with delegates and Control.Invoke method</p>
<pre class="brush: csharp;">
private ThreadStart threadStart;
private Thread thread;
private delegate void UpdateTextDelegate(string text);

private void Form1_Load(object sender, EventArgs e)
{
    this.threadStart = new ThreadStart(LongProcess);
    this.thread = new Thread(this.threadStart);
    this.thread.Start();
}

private void LongProcess()
{
    for (int i = 0; i &lt; 100; i++)
    {
        Thread.Sleep(100);
        //This will resolve the cross thread invalid operation exception.
        this.UpdateText(i.ToString());
    }
}

private void UpdateText(string text)
{
    //Checks whether the called required to invoke the &quot;Invoke&quot; method.
    if (this.textBox1.InvokeRequired)
    {
        UpdateTextDelegate updateTextDelegate = new UpdateTextDelegate(UpdateText);
        //Calling the invoke method of the control with the parameter.
        this.textBox1.Invoke(updateTextDelegate, new object[] { text });
    }
    else
    {
        this.textBox1.Text = text;
    }
}
</pre>
<p>Thanks to Praveen for helping me out. Happing Coding <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dotnetthoughts.net%2F2010%2F02%2F05%2Fsystem_invalidoperationexception_cross_thread_operation_not_valid%2F&amp;linkname=System.InvalidOperationException%20%E2%80%93%20Cross-thread%20operation%20not%20valid">Share/Save</a>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/02/05/system_invalidoperationexception_cross_thread_operation_not_valid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
