<?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"
	>

<channel>
	<title>Fransiscus Setiawan .NET Blog &#187; .NET</title>
	<atom:link href="http://fransiscuss.com/category/dotnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://fransiscuss.com</link>
	<description>My notepad to share knowledge with others</description>
	<pubDate>Wed, 06 Jan 2010 05:44:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>Compression in Silverlight Isolated Storage</title>
		<link>http://fransiscuss.com/compression-in-silverlight-isolated-storage/</link>
		<comments>http://fransiscuss.com/compression-in-silverlight-isolated-storage/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 04:34:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[VB.NET]]></category>

		<category><![CDATA[Isolated Storage]]></category>

		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=240</guid>
		<description><![CDATA[Isolated storage in silverlight is used to store information or object therefore we don&#8217;t need to go to database to get all the information over and over again but again to use isolated storage or not should be based on case by case. Based on the implementation of the code below, I&#8217;ve found that it [...]]]></description>
			<content:encoded><![CDATA[<p>Isolated storage in silverlight is used to store information or object therefore we don&#8217;t need to go to database to get all the information over and over again but again to use isolated storage or not should be based on case by case. Based on the implementation of the code below, I&#8217;ve found that it can compress the isolated storage file from <strong><span style="color:red">0.8Mb becoming 0.1 MB</span></strong> which is great enough for me since the quota limit is 1 mb and we try not to exceed that limit.</p>
<p>I&#8217;ve got this compression method from <a href="http://www.eggheadcafe.com/tutorials/aspnet/d566463d-83bd-486a-8633-53aa54f405bf/silverlight-2-beta-2-doi.aspx" target="_blank">Peter Bromberg blog</a>. </p>
<p>This compression method is a wrapper to SharpZip Library for Silverlight, you can download it from <a href="" target="_blank">here</a>, you need this library before using the code below. You can download the code below from <a href="" target="_blank">here</a>. </p>
<p>I&#8217;ve also created a Isolated Storage File with the assembly version as the file name to make sure that we have clean isolated storage file assuming the uncompressed version of isolated storage is already in production and we need to clean it up.  <strong>&#8220;AssemblyVersion&#8221;</strong> properties will return the property of the current version no of the running assembly. <strong>CheckIsolatedStorageFileVersion</strong> will make sure that we always have the clean isolated storage for the new assembly.</p>
<p><strong>Sample of usage</strong><br />
<strong>1. How to read/deserialize object from isolated storage</strong></p>

<div class="wp_syntax"><div class="code"><pre class="vb vb" style="font-family:monospace;"> <span style="color: #b1b100;">Dim</span> objMessageCodes <span style="color: #b1b100;">As</span> MessageCodes = GetIsolatedStorage<span style="color: #66cc66;">&#40;</span>Of MessageCodes<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;MyFile.txt&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p><strong>2. How to write/serialize object to isolated storage</strong></p>

<div class="wp_syntax"><div class="code"><pre class="vb vb" style="font-family:monospace;">      <span style="color: #b1b100;">Dim</span> _messageCodes <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> MessageCodes<span style="color: #66cc66;">&#40;</span>mListMessage<span style="color: #66cc66;">&#41;</span>
      WriteIsolatedStorage<span style="color: #66cc66;">&#40;</span>_messageCodes, <span style="color: #ff0000;">&quot;myfile.txt&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>These are the definition of the above function to serialize/deserialize object from your silverlight, this function is created based on Generic so it&#8217;s flexible enough to accept anything</p>

<div class="wp_syntax"><div class="code"><pre class="vb vb" style="font-family:monospace;">Imports System.<span style="color: #66cc66;">Collections</span>.<span style="color: #66cc66;">Generic</span>
Imports System.<span style="color: #66cc66;">ServiceModel</span>
Imports System.<span style="color: #66cc66;">Threading</span>
Imports System.<span style="color: #66cc66;">IO</span>
Imports System.<span style="color: #66cc66;">IO</span>.<span style="color: #66cc66;">IsolatedStorage</span>
Imports System.<span style="color: #66cc66;">Xml</span>.<span style="color: #66cc66;">Serialization</span>
Imports System.<span style="color: #66cc66;">Runtime</span>.<span style="color: #66cc66;">Serialization</span>
Imports System.<span style="color: #66cc66;">Reflection</span>
&nbsp;
<span style="color: #b1b100;">Public</span> Class MySL
 <span style="color: #b1b100;">Private</span> mAppStorage <span style="color: #b1b100;">As</span> IsolatedStorageFile
&nbsp;
    <span style="color: #808080;">''' &lt;summary&gt;</span>
    <span style="color: #808080;">''' This is used to write the compressed object to Isolated Storage</span>
    <span style="color: #808080;">''' &lt;/summary&gt;</span>
    <span style="color: #808080;">''' &lt;param name=&quot;filename&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #808080;">''' &lt;remarks&gt;&lt;/remarks&gt;</span>
    <span style="color: #b1b100;">Private</span> <span style="color: #b1b100;">Sub</span> WriteIsolatedStorage<span style="color: #66cc66;">&#40;</span>Of T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>ByVal obj <span style="color: #b1b100;">As</span> T, ByVal filename <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span><span style="color: #66cc66;">&#41;</span>
        Try
            <span style="color: #b1b100;">Dim</span> xmlByte <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = Compression.<span style="color: #66cc66;">SerializeAndCompress</span><span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span>
&nbsp;
            Using _stream <span style="color: #b1b100;">As</span> IsolatedStorageFileStream = mAppStorage.<span style="color: #66cc66;">CreateFile</span><span style="color: #66cc66;">&#40;</span>filename<span style="color: #66cc66;">&#41;</span>
                _stream.<span style="color: #b1b100;">Write</span><span style="color: #66cc66;">&#40;</span>xmlByte, 0, xmlByte.<span style="color: #66cc66;">Length</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">End</span> Using
        Catch ex <span style="color: #b1b100;">As</span> Exception
            Throw ex
        <span style="color: #b1b100;">End</span> Try
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Sub</span>
&nbsp;
    <span style="color: #808080;">''' &lt;summary&gt;</span>
    <span style="color: #808080;">''' this is used get the compressed object from Isolated storage</span>
    <span style="color: #808080;">''' &lt;/summary&gt;</span>
    <span style="color: #808080;">''' &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #808080;">''' &lt;param name=&quot;fileName&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #808080;">''' &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #808080;">''' &lt;remarks&gt;&lt;/remarks&gt;</span>
    <span style="color: #b1b100;">Private</span> <span style="color: #b1b100;">Function</span> GetIsolatedStorage<span style="color: #66cc66;">&#40;</span>Of T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>ByVal fileName <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> T
        Try
            Using _stream <span style="color: #b1b100;">As</span> IsolatedStorageFileStream = mAppStorage.<span style="color: #66cc66;">OpenFile</span><span style="color: #66cc66;">&#40;</span>fileName, FileMode.<span style="color: #b1b100;">Open</span><span style="color: #66cc66;">&#41;</span>
                Using _reader <span style="color: #b1b100;">As</span> BinaryReader = <span style="color: #b1b100;">New</span> BinaryReader<span style="color: #66cc66;">&#40;</span>_stream<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #b1b100;">Dim</span> tmpBytes <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #b1b100;">ReDim</span> tmpBytes<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #b1b100;">Dim</span> fullBytes <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = <span style="color: #b1b100;">Nothing</span>
                    <span style="color: #b1b100;">Dim</span> xmlStream <span style="color: #b1b100;">As</span> MemoryStream = <span style="color: #b1b100;">New</span> MemoryStream<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
                    <span style="color: #b1b100;">While</span> <span style="color: #b1b100;">True</span>
                        <span style="color: #b1b100;">Dim</span> read <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span> = _reader.<span style="color: #66cc66;">Read</span><span style="color: #66cc66;">&#40;</span>tmpBytes, 0, tmpBytes.<span style="color: #66cc66;">Length</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
                        <span style="color: #b1b100;">If</span> <span style="color: #66cc66;">&#40;</span>read &lt;= 0<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">Then</span>
                            fullBytes = xmlStream.<span style="color: #66cc66;">ToArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                            <span style="color: #b1b100;">Exit</span> <span style="color: #b1b100;">While</span>
                        <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
&nbsp;
                        xmlStream.<span style="color: #b1b100;">Write</span><span style="color: #66cc66;">&#40;</span>tmpBytes, 0, read<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">While</span>
&nbsp;
                    <span style="color: #b1b100;">Dim</span> xmlTempbyte <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = xmlStream.<span style="color: #66cc66;">ToArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                     <span style="color: #b1b100;">Return</span> Compression.<span style="color: #66cc66;">DecompressAndDeserialize</span><span style="color: #66cc66;">&#40;</span>Of T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>xmlTempbyte<span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">End</span> Using
&nbsp;
            <span style="color: #b1b100;">End</span> Using
&nbsp;
            <span style="color: #b1b100;">Return</span> <span style="color: #b1b100;">Nothing</span>
&nbsp;
        Catch ex <span style="color: #b1b100;">As</span> Exception
            Throw ex
        <span style="color: #b1b100;">End</span> Try
&nbsp;
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
<span style="color: #b1b100;">Private</span> _compression <span style="color: #b1b100;">As</span> Compression = <span style="color: #b1b100;">Nothing</span>
&nbsp;
    <span style="color: #b1b100;">Private</span> ReadOnly <span style="color: #b1b100;">Property</span> Compression<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> Compression
        <span style="color: #b1b100;">Get</span>
            <span style="color: #b1b100;">If</span> <span style="color: #66cc66;">&#40;</span>_compression Is <span style="color: #b1b100;">Nothing</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">Then</span>
                _compression = <span style="color: #b1b100;">New</span> Compression<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
&nbsp;
            <span style="color: #b1b100;">Return</span> _compression
        <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Get</span>
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Property</span>
&nbsp;
    <span style="color: #808080;">''' &lt;summary&gt;</span>
    <span style="color: #808080;">''' get the assembly version</span>
    <span style="color: #808080;">''' &lt;/summary&gt;</span>
    <span style="color: #808080;">''' &lt;value&gt;&lt;/value&gt;</span>
    <span style="color: #808080;">''' &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #808080;">''' &lt;remarks&gt;&lt;/remarks&gt;</span>
    <span style="color: #b1b100;">Private</span> ReadOnly <span style="color: #b1b100;">Property</span> AssemblyVersion<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span>
        <span style="color: #b1b100;">Get</span>
            <span style="color: #b1b100;">Dim</span> <span style="color: #b1b100;">name</span> <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span> = Assembly.<span style="color: #66cc66;">GetExecutingAssembly</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #66cc66;">FullName</span>
            <span style="color: #b1b100;">Dim</span> asmName <span style="color: #b1b100;">As</span> AssemblyName = <span style="color: #b1b100;">New</span> AssemblyName<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">Return</span> asmName.<span style="color: #66cc66;">Version</span>.<span style="color: #66cc66;">ToString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #66cc66;">Replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;.&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Get</span>
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Property</span>
&nbsp;
<span style="color: #808080;">''' &lt;summary&gt;</span>
    <span style="color: #808080;">''' try to clear isolated storage</span>
    <span style="color: #808080;">''' &lt;/summary&gt;</span>
    <span style="color: #808080;">''' &lt;remarks&gt;&lt;/remarks&gt;</span>
    <span style="color: #b1b100;">Private</span> <span style="color: #b1b100;">Sub</span> CheckIsolatedStorageFileVersion<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        Try
            <span style="color: #b1b100;">Dim</span> isoStorage <span style="color: #b1b100;">As</span> IsolatedStorageFile = IsolatedStorageFile.<span style="color: #66cc66;">GetUserStoreForSite</span>
&nbsp;
            <span style="color: #b1b100;">If</span> <span style="color: #b1b100;">Not</span> mAppStorage.<span style="color: #66cc66;">FileExists</span><span style="color: #66cc66;">&#40;</span>AssemblyVersion + <span style="color: #ff0000;">&quot;.txt&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">Then</span>
                <span style="color: #808080;">'clear all the isolated storage</span>
                isoStorage.<span style="color: #66cc66;">Remove</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                mAppStorage.<span style="color: #66cc66;">Remove</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
                mAppStorage = IsolatedStorageFile.<span style="color: #66cc66;">GetUserStoreForApplication</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
                Using _stream <span style="color: #b1b100;">As</span> IsolatedStorageFileStream = mAppStorage.<span style="color: #66cc66;">CreateFile</span><span style="color: #66cc66;">&#40;</span>AssemblyVersion + <span style="color: #ff0000;">&quot;.txt&quot;</span><span style="color: #66cc66;">&#41;</span>
                    Using sw <span style="color: #b1b100;">As</span> StreamWriter = <span style="color: #b1b100;">New</span> StreamWriter<span style="color: #66cc66;">&#40;</span>_stream<span style="color: #66cc66;">&#41;</span>
                        sw.<span style="color: #b1b100;">Write</span><span style="color: #66cc66;">&#40;</span>AssemblyVersion<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #b1b100;">End</span> Using
                <span style="color: #b1b100;">End</span> Using
                <span style="color: #808080;">'Throw New Exception(&quot;Clearing!!!&quot;)</span>
            <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
&nbsp;
        Catch ex <span style="color: #b1b100;">As</span> Exception
            Throw ex
        <span style="color: #b1b100;">End</span> Try
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Sub</span>
&nbsp;
<span style="color: #b1b100;">End</span> Class</pre></div></div>

<p><strong>Wrapper Class to SL SharpZipLib</strong></p>

<div class="wp_syntax"><div class="code"><pre class="vb vb" style="font-family:monospace;">Imports System
Imports System.<span style="color: #66cc66;">Text</span>
Imports System.<span style="color: #66cc66;">IO</span>
Imports System.<span style="color: #66cc66;">Collections</span>
Imports System.<span style="color: #66cc66;">Diagnostics</span>
Imports System.<span style="color: #66cc66;">Collections</span>.<span style="color: #66cc66;">Generic</span>
Imports System.<span style="color: #66cc66;">Runtime</span>.<span style="color: #66cc66;">Serialization</span>
Imports ICSharpCode.<span style="color: #66cc66;">SharpZipLib</span>.<span style="color: #66cc66;">Zip</span>.<span style="color: #66cc66;">Compression</span>
Imports System.<span style="color: #66cc66;">Xml</span>.<span style="color: #66cc66;">Serialization</span>
Imports System.<span style="color: #66cc66;">Text</span>.<span style="color: #66cc66;">RegularExpressions</span>
&nbsp;
<span style="color: #b1b100;">Public</span> Class Compression
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Sub</span> <span style="color: #b1b100;">New</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Sub</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> Serialize<span style="color: #66cc66;">&#40;</span>Of T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>ByVal inst <span style="color: #b1b100;">As</span> T<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">Dim</span> dcs <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> DataContractSerializer<span style="color: #66cc66;">&#40;</span>GetType<span style="color: #66cc66;">&#40;</span>T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        Using ms <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> MemoryStream
            dcs.<span style="color: #66cc66;">WriteObject</span><span style="color: #66cc66;">&#40;</span>ms, inst<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">Return</span> ms.<span style="color: #66cc66;">ToArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">End</span> Using
&nbsp;
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> Deserialize<span style="color: #66cc66;">&#40;</span>Of T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>ByVal objectData <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> T
        <span style="color: #b1b100;">Dim</span> dcs <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> DataContractSerializer<span style="color: #66cc66;">&#40;</span>GetType<span style="color: #66cc66;">&#40;</span>T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        Using ms <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> MemoryStream<span style="color: #66cc66;">&#40;</span>objectData<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">Return</span> CType<span style="color: #66cc66;">&#40;</span>dcs.<span style="color: #66cc66;">ReadObject</span><span style="color: #66cc66;">&#40;</span>ms<span style="color: #66cc66;">&#41;</span>, T<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">End</span> Using
&nbsp;
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> SerializeAndCompress<span style="color: #66cc66;">&#40;</span>Of T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>ByVal inst <span style="color: #b1b100;">As</span> T<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">Dim</span> b <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = Serialize<span style="color: #66cc66;">&#40;</span>Of T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>inst<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">Return</span> Compress<span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> DecompressAndDeserialize<span style="color: #66cc66;">&#40;</span>Of T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>ByVal bytData <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> T
        <span style="color: #b1b100;">Dim</span> bytes <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = Decompress<span style="color: #66cc66;">&#40;</span>bytData<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">Return</span> Deserialize<span style="color: #66cc66;">&#40;</span>Of T<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> Compress<span style="color: #66cc66;">&#40;</span>ByVal strInput <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        Try
            <span style="color: #b1b100;">Dim</span> bytData <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = System.<span style="color: #66cc66;">Text</span>.<span style="color: #66cc66;">Encoding</span>.<span style="color: #66cc66;">UTF8</span>.<span style="color: #66cc66;">GetBytes</span><span style="color: #66cc66;">&#40;</span>strInput<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">Dim</span> ms <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> MemoryStream<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">Dim</span> defl <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> Deflater<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span>, <span style="color: #b1b100;">False</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
            Using s <span style="color: #b1b100;">As</span> Stream = <span style="color: #b1b100;">New</span> Streams.<span style="color: #66cc66;">DeflaterOutputStream</span><span style="color: #66cc66;">&#40;</span>ms, defl<span style="color: #66cc66;">&#41;</span>
                s.<span style="color: #b1b100;">Write</span><span style="color: #66cc66;">&#40;</span>bytData, 0, bytData.<span style="color: #66cc66;">Length</span><span style="color: #66cc66;">&#41;</span>
                s.<span style="color: #b1b100;">Close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">End</span> Using
&nbsp;
            <span style="color: #b1b100;">Return</span> DirectCast<span style="color: #66cc66;">&#40;</span>ms.<span style="color: #66cc66;">ToArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        Catch
            Throw
&nbsp;
        <span style="color: #b1b100;">End</span> Try
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> Compress<span style="color: #66cc66;">&#40;</span>ByVal bytData <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        Try
&nbsp;
            <span style="color: #b1b100;">Dim</span> ms <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> MemoryStream<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">Dim</span> defl <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> Deflater<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span>, <span style="color: #b1b100;">False</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
            Using s <span style="color: #b1b100;">As</span> Stream = <span style="color: #b1b100;">New</span> Streams.<span style="color: #66cc66;">DeflaterOutputStream</span><span style="color: #66cc66;">&#40;</span>ms, defl<span style="color: #66cc66;">&#41;</span>
                s.<span style="color: #b1b100;">Write</span><span style="color: #66cc66;">&#40;</span>bytData, 0, bytData.<span style="color: #66cc66;">Length</span><span style="color: #66cc66;">&#41;</span>
                s.<span style="color: #b1b100;">Close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">End</span> Using
&nbsp;
            <span style="color: #b1b100;">Return</span> DirectCast<span style="color: #66cc66;">&#40;</span>ms.<span style="color: #66cc66;">ToArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        Catch
            Throw
&nbsp;
        <span style="color: #b1b100;">End</span> Try
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> Compress<span style="color: #66cc66;">&#40;</span>ByVal bytData <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, ByVal ParamArray ratio <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #b1b100;">Dim</span> compRatio <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span> = <span style="color: #cc66cc;">9</span>
        Try
            <span style="color: #b1b100;">If</span> ratio<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span> &gt; 0 <span style="color: #b1b100;">Then</span>
                compRatio = ratio<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
        Catch
&nbsp;
        <span style="color: #b1b100;">End</span> Try
&nbsp;
        Try
            <span style="color: #b1b100;">Dim</span> ms <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> MemoryStream<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">Dim</span> defl <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> Deflater<span style="color: #66cc66;">&#40;</span>compRatio, <span style="color: #b1b100;">False</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
            Using s <span style="color: #b1b100;">As</span> Stream = <span style="color: #b1b100;">New</span> Streams.<span style="color: #66cc66;">DeflaterOutputStream</span><span style="color: #66cc66;">&#40;</span>ms, defl<span style="color: #66cc66;">&#41;</span>
                s.<span style="color: #b1b100;">Write</span><span style="color: #66cc66;">&#40;</span>bytData, 0, bytData.<span style="color: #66cc66;">Length</span><span style="color: #66cc66;">&#41;</span>
                s.<span style="color: #b1b100;">Close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">End</span> Using
&nbsp;
            <span style="color: #b1b100;">Return</span> DirectCast<span style="color: #66cc66;">&#40;</span>ms.<span style="color: #66cc66;">ToArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        Catch
            Throw
        <span style="color: #b1b100;">End</span> Try
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> Decompress<span style="color: #66cc66;">&#40;</span>ByVal bytInput <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        Try
&nbsp;
            <span style="color: #b1b100;">Dim</span> ms <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> MemoryStream<span style="color: #66cc66;">&#40;</span>bytInput, 0, bytInput.<span style="color: #66cc66;">Length</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">Dim</span> bytResult <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = <span style="color: #b1b100;">Nothing</span>
            <span style="color: #b1b100;">Dim</span> strResult <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">String</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #66cc66;">Empty</span>
            <span style="color: #b1b100;">Dim</span> writeData <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = <span style="color: #b1b100;">New</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4095</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
            Using s2 <span style="color: #b1b100;">As</span> Stream = <span style="color: #b1b100;">New</span> Streams.<span style="color: #66cc66;">InflaterInputStream</span><span style="color: #66cc66;">&#40;</span>ms<span style="color: #66cc66;">&#41;</span>
                bytResult = ReadFullStream<span style="color: #66cc66;">&#40;</span>s2<span style="color: #66cc66;">&#41;</span>
                s2.<span style="color: #b1b100;">Close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">End</span> Using
&nbsp;
            <span style="color: #b1b100;">Return</span> bytResult
        Catch
            Throw
        <span style="color: #b1b100;">End</span> Try
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> ReadFullStream<span style="color: #66cc66;">&#40;</span>ByVal stream <span style="color: #b1b100;">As</span> Stream<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">Dim</span> buffer <span style="color: #b1b100;">As</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = <span style="color: #b1b100;">New</span> Byte<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32767</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
        Using ms <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> MemoryStream<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">While</span> <span style="color: #b1b100;">True</span>
                <span style="color: #b1b100;">Dim</span> read <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span> = stream.<span style="color: #66cc66;">Read</span><span style="color: #66cc66;">&#40;</span>buffer, 0, buffer.<span style="color: #66cc66;">Length</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">If</span> read &lt;= 0 <span style="color: #b1b100;">Then</span>
                    <span style="color: #b1b100;">Return</span> ms.<span style="color: #66cc66;">ToArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
                ms.<span style="color: #b1b100;">Write</span><span style="color: #66cc66;">&#40;</span>buffer, 0, read<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">While</span>
        <span style="color: #b1b100;">End</span> Using
&nbsp;
        <span style="color: #b1b100;">Return</span> buffer
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
<span style="color: #b1b100;">End</span> Class</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/compression-in-silverlight-isolated-storage/feed/</wfw:commentRss>
		</item>
		<item>
		<title>FormsAuthentication.GetRedirectUrl only get the first parameter of querystring</title>
		<link>http://fransiscuss.com/formsauthenticationgetredirecturl-only-get-the-first-parameter-of-querystring/</link>
		<comments>http://fransiscuss.com/formsauthenticationgetredirecturl-only-get-the-first-parameter-of-querystring/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 05:53:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[VB.NET]]></category>

		<category><![CDATA[Forms Authentication]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=236</guid>
		<description><![CDATA[I found the issue with FormsAuthentication.GetRedirectUrl when it redirects then it redirects with the first querystring that you have while in fact you might have more than one querystring
e.g http://localhost/myweb/login.aspx?returnurl=myview.aspx?viewID=123&#38;viewname=abc&#38;viewall=false then the standard FormsAuthentication will redirect to http://localhost/myweb/myview.aspx?viewID=123
but where&#8217;s the remaining viewname querystring and viewall querystring??to fix this, just use the code below to pick [...]]]></description>
			<content:encoded><![CDATA[<p>I found the issue with FormsAuthentication.GetRedirectUrl when it redirects then it redirects with the first querystring that you have while in fact you might have more than one querystring</p>
<p>e.g <a href="http://localhost/myweb/login.aspx?returnurl=myview.aspx?viewID=123&amp;viewname=abc&amp;viewall=false">http://localhost/myweb/login.aspx?returnurl=myview.aspx?viewID=123&amp;viewname=abc&amp;viewall=false</a> then the standard FormsAuthentication will redirect to <a href="http://localhost/myweb/myview.aspx?viewID=123">http://localhost/myweb/myview.aspx?viewID=123</a></p>
<p>but where&#8217;s the remaining viewname querystring and viewall querystring??to fix this, just use the code below to pick the remaining querystring</p>
<p><strong>Methods:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="vb vb" style="font-family:monospace;">  <span style="color: #808080;">'this is used to fix the issue with FormsAuthentication.GetRedirectUrl only pick the first query string</span>
            <span style="color: #808080;">'get the original Redirect URL</span>
            <span style="color: #b1b100;">Dim</span> redirectUrl <span style="color: #b1b100;">As</span> StringBuilder = <span style="color: #b1b100;">New</span> StringBuilder<span style="color: #66cc66;">&#40;</span>FormsAuthentication.<span style="color: #66cc66;">GetRedirectUrl</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</span>, <span style="color: #b1b100;">True</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">Dim</span> coll <span style="color: #b1b100;">As</span> NameValueCollection = objRequest.<span style="color: #66cc66;">QueryString</span>
&nbsp;
            <span style="color: #808080;">'iterate through every key in query string</span>
            <span style="color: #808080;">'add the missing query string</span>
            <span style="color: #b1b100;">For</span> Each key <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span> In coll.<span style="color: #66cc66;">AllKeys</span>
                <span style="color: #b1b100;">If</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">String</span>.<span style="color: #66cc66;">Compare</span><span style="color: #66cc66;">&#40;</span>key, <span style="color: #ff0000;">&quot;returnurl&quot;</span>, <span style="color: #b1b100;">True</span><span style="color: #66cc66;">&#41;</span> &lt;&gt; 0<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">Then</span>
                    <span style="color: #b1b100;">Dim</span> values <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = coll.<span style="color: #66cc66;">GetValues</span><span style="color: #66cc66;">&#40;</span>key<span style="color: #66cc66;">&#41;</span>
&nbsp;
                    <span style="color: #b1b100;">If</span> <span style="color: #66cc66;">&#40;</span>values.<span style="color: #66cc66;">Length</span> &gt; 0<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">Then</span>
                        <span style="color: #b1b100;">Dim</span> pair <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span> = <span style="color: #b1b100;">String</span>.<span style="color: #b1b100;">Format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;{0}={1}&quot;</span>, key, values<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
                        <span style="color: #b1b100;">If</span> <span style="color: #66cc66;">&#40;</span>redirectUrl.<span style="color: #66cc66;">ToString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #66cc66;">IndexOf</span><span style="color: #66cc66;">&#40;</span>pair<span style="color: #66cc66;">&#41;</span> &lt; 0<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">Then</span>
                            redirectUrl.<span style="color: #66cc66;">Append</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&amp;&quot;</span> + pair<span style="color: #66cc66;">&#41;</span>
                        <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
                    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
                <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
            <span style="color: #b1b100;">Next</span>
&nbsp;
            <span style="color: #808080;">'this is to retain the original URL as in the query string</span>
            objResponse.<span style="color: #66cc66;">Redirect</span><span style="color: #66cc66;">&#40;</span>redirectUrl.<span style="color: #66cc66;">ToString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/formsauthenticationgetredirecturl-only-get-the-first-parameter-of-querystring/feed/</wfw:commentRss>
		</item>
		<item>
		<title>an activex control on this page might be unsafe to interact with other parts of the page. do you want to allow this interaction?</title>
		<link>http://fransiscuss.com/an-activex-control-on-this-page-might-be-unsafe-to-interact-with-other-parts-of-the-page-do-you-want-to-allow-this-interaction/</link>
		<comments>http://fransiscuss.com/an-activex-control-on-this-page-might-be-unsafe-to-interact-with-other-parts-of-the-page-do-you-want-to-allow-this-interaction/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 06:18:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=233</guid>
		<description><![CDATA[I keep getting this message &#8220;an activex control on this page might be unsafe to interact with other parts of the page. do you want to allow this interaction&#8221; when I try to access a page that use Javascript to access ActiveX Code. I&#8217;ve spent quite sometime to get rid of this error message and [...]]]></description>
			<content:encoded><![CDATA[<p>I keep getting this message &#8220;an activex control on this page might be unsafe to interact with other parts of the page. do you want to allow this interaction&#8221; when I try to access a page that use Javascript to access ActiveX Code. I&#8217;ve spent quite sometime to get rid of this error message and I found out that</p>
<ol>
<li>The code need implement IObjectSafety Interface</li>
<li>You need to digitally sign the code with valid certificate</li>
</ol>
<p><strong>Step 1 :</strong> Detail on how to implement IObjectSafety<br />
-Create an interface file called as IObjectSafety.vb/IObjectSafety.cs</p>
<p>CSharp (IObjectSafety.cs) :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>ComImport<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#91;</span>Guid<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;CB5BDC81-93C1-11CF-8F20-00805F2CD064&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#91;</span>InterfaceType<span style="color: #000000;">&#40;</span>ComInterfaceType.<span style="color: #0000FF;">InterfaceIsIUnknown</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #FF0000;">interface</span> IObjectSafety <span style="color: #000000;">&#123;</span>
   <span style="color: #000000;">&#91;</span>PreserveSig<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
   <span style="color: #FF0000;">int</span> GetInterfaceSafetyOptions<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> Guid riid, <span style="color: #0600FF;">out</span> <span style="color: #FF0000;">int</span> pdwSupportedOptions, <span style="color: #0600FF;">out</span> <span style="color: #FF0000;">int</span> pdwEnabledOptions<span style="color: #000000;">&#41;</span>;
&nbsp;
   <span style="color: #000000;">&#91;</span>PreserveSig<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
   <span style="color: #FF0000;">int</span> SetInterfaceSafetyOptions<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> Guid riid, <span style="color: #FF0000;">int</span> dwOptionSetMask, <span style="color: #FF0000;">int</span> dwEnabledOptions<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="vb vb" style="font-family:monospace;">Imports System.<span style="color: #66cc66;">Runtime</span>.<span style="color: #66cc66;">InteropServices</span>
&nbsp;
&lt;ComImport<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>&gt; _
&lt;Guid<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;CB5BDC81-93C1-11CF-8F20-00805F2CD064&quot;</span><span style="color: #66cc66;">&#41;</span>&gt; _
&lt;InterfaceType<span style="color: #66cc66;">&#40;</span>ComInterfaceType.<span style="color: #66cc66;">InterfaceIsIUnknown</span><span style="color: #66cc66;">&#41;</span>&gt; _
Interface IObjectSafety
    &lt;PreserveSig<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>&gt; _
    <span style="color: #b1b100;">Function</span> GetInterfaceSafetyOptions<span style="color: #66cc66;">&#40;</span>ByRef riid <span style="color: #b1b100;">As</span> Guid, ByRef pdwSupportedOptions <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span>, ByRef pdwEnabledOptions <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span>
&nbsp;
    &lt;PreserveSig<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>&gt; _
    <span style="color: #b1b100;">Function</span> SetInterfaceSafetyOptions<span style="color: #66cc66;">&#40;</span>ByRef riid <span style="color: #b1b100;">As</span> Guid, ByVal dwOptionSetMask <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span>, ByVal dwEnabledOptions <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span>
<span style="color: #b1b100;">End</span> Interface</pre></div></div>

<p>in your ActiveX code you need to implement IObjectSafety</p>
<p>CSharp:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">&nbsp;</pre></div></div>

<p>VB.NET:</p>

<div class="wp_syntax"><div class="code"><pre class="vb vb" style="font-family:monospace;">&lt;ProgId<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Fransiscus.Authentication.ClientUtility&quot;</span><span style="color: #66cc66;">&#41;</span>&gt; _
&lt;ClassInterface<span style="color: #66cc66;">&#40;</span>ClassInterfaceType.<span style="color: #66cc66;">AutoDual</span><span style="color: #66cc66;">&#41;</span>, ComSourceInterfaces<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ControlEvents&quot;</span><span style="color: #66cc66;">&#41;</span>&gt; _
&lt;Guid<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;0577147B-6941-4f15-9EFB-2551FEB3D6CC&quot;</span><span style="color: #66cc66;">&#41;</span>&gt; _
&lt;ComVisible<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">True</span><span style="color: #66cc66;">&#41;</span>&gt; _
<span style="color: #b1b100;">Public</span> NotInheritable Class ClientUtility
    <span style="color: #b1b100;">Implements</span> IObjectSafety
&nbsp;
&nbsp;
#Region <span style="color: #ff0000;">&quot;IObjectSafety Constants&quot;</span>
&nbsp;
    <span style="color: #b1b100;">Private</span> <span style="color: #b1b100;">Const</span> INTERFACESAFE_FOR_UNTRUSTED_CALLER <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span> = &amp;H1
    <span style="color: #b1b100;">Private</span> <span style="color: #b1b100;">Const</span> INTERFACESAFE_FOR_UNTRUSTED_DATA <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span> = &amp;H2
    <span style="color: #b1b100;">Private</span> <span style="color: #b1b100;">Const</span> S_OK <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span> = 0
&nbsp;
#End Region
&nbsp;
#Region <span style="color: #ff0000;">&quot;IObjectSafety Methods&quot;</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> GetInterfaceSafetyOptions<span style="color: #66cc66;">&#40;</span>ByRef riid <span style="color: #b1b100;">As</span> System.<span style="color: #66cc66;">Guid</span>, ByRef pdwSupportedOptions <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span>, ByRef pdwEnabledOptions <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span> <span style="color: #b1b100;">Implements</span> IObjectSafety.<span style="color: #66cc66;">GetInterfaceSafetyOptions</span>
        pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER <span style="color: #b1b100;">Or</span> INTERFACESAFE_FOR_UNTRUSTED_DATA
        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER <span style="color: #b1b100;">Or</span> INTERFACESAFE_FOR_UNTRUSTED_DATA
        <span style="color: #b1b100;">Return</span> S_OK
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
    <span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> SetInterfaceSafetyOptions<span style="color: #66cc66;">&#40;</span>ByRef riid <span style="color: #b1b100;">As</span> System.<span style="color: #66cc66;">Guid</span>, ByVal dwOptionSetMask <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span>, ByVal dwEnabledOptions <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Integer</span> <span style="color: #b1b100;">Implements</span> IObjectSafety.<span style="color: #66cc66;">SetInterfaceSafetyOptions</span>
        <span style="color: #b1b100;">Return</span> S_OK
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
#End Region</pre></div></div>

<p>CSharp:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>ProgId<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Fransiscus.Authentication.ClientUtility&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #000000;">&#91;</span>ClassInterface<span style="color: #000000;">&#40;</span>ClassInterfaceType.<span style="color: #0000FF;">AutoDual</span><span style="color: #000000;">&#41;</span>, ComSourceInterfaces<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ControlEvents&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #000000;">&#91;</span>Guid<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;0577147B-6941-4f15-9EFB-2551FEB3D6CC&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #000000;">&#91;</span>ComVisible<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">sealed</span> <span style="color: #FF0000;">class</span> ClientUtility <span style="color: #008000;">:</span> IObjectSafety 
<span style="color: #000000;">&#123;</span> 
&nbsp;
&nbsp;
    <span style="color: #008080;">#region &quot;IObjectSafety Constants&quot; </span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> INTERFACESAFE_FOR_UNTRUSTED_CALLER <span style="color: #008000;">=</span> 0x1; 
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> INTERFACESAFE_FOR_UNTRUSTED_DATA <span style="color: #008000;">=</span> 0x2; 
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> S_OK <span style="color: #008000;">=</span> 0; 
&nbsp;
    <span style="color: #008080;">#endregion </span>
&nbsp;
    <span style="color: #008080;">#region &quot;IObjectSafety Methods&quot; </span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> GetInterfaceSafetyOptions<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Guid</span> riid, <span style="color: #0600FF;">ref</span> <span style="color: #FF0000;">int</span> pdwSupportedOptions, <span style="color: #0600FF;">ref</span> <span style="color: #FF0000;">int</span> pdwEnabledOptions<span style="color: #000000;">&#41;</span> 
    <span style="color: #000000;">&#123;</span> 
        pdwSupportedOptions <span style="color: #008000;">=</span> INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; 
        pdwEnabledOptions <span style="color: #008000;">=</span> INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; 
        <span style="color: #0600FF;">return</span> S_OK; 
    <span style="color: #000000;">&#125;</span> 
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> SetInterfaceSafetyOptions<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Guid</span> riid, <span style="color: #FF0000;">int</span> dwOptionSetMask, <span style="color: #FF0000;">int</span> dwEnabledOptions<span style="color: #000000;">&#41;</span> 
    <span style="color: #000000;">&#123;</span> 
        <span style="color: #0600FF;">return</span> S_OK; 
    <span style="color: #000000;">&#125;</span> 
<span style="color: #008080;">#endregion</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><Strong>Step 2:</strong><br />
You need to obtain certificate and sign it, You also need Windows Server 2008 SDK to sign your code using SignTool.exe (type Signtool SignWizard in command prompt to follow the wizard and sign your DLL)<br />For more detail please open verisign website (http://www.verisign.com/support/code-signing-support/code-signing/identity-authentication.html) or click <a href="http://www.verisign.com/support/code-signing-support/code-signing/identity-authentication.html" target="_blank">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/an-activex-control-on-this-page-might-be-unsafe-to-interact-with-other-parts-of-the-page-do-you-want-to-allow-this-interaction/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Resolve URL functions</title>
		<link>http://fransiscuss.com/resolve-url-functions/</link>
		<comments>http://fransiscuss.com/resolve-url-functions/#comments</comments>
		<pubDate>Sun, 31 May 2009 11:39:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[Resolve URL methods]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=231</guid>
		<description><![CDATA[This snippet code is credited to Scott&#8217;s Hanselman, This Resolve URL function is used where you want to implement it on your business layer.
Methods:

&#160;
        #region &#34;Image URL helpers&#34;
&#160;
        public static string ResolveUrl&#40;string originalUrl&#41;
        &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>This snippet code is credited to Scott&#8217;s Hanselman, This Resolve URL function is used where you want to implement it on your business layer.</p>
<p><strong>Methods:</strong><br/></p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">&nbsp;
        <span style="color: #008080;">#region &quot;Image URL helpers&quot;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> ResolveUrl<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> originalUrl<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>    
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>originalUrl <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>        
                <span style="color: #0600FF;">return</span> null;     
            <span style="color: #008080; font-style: italic;">// *** Absolute path - just return    </span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>originalUrl.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;://&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>        
                <span style="color: #0600FF;">return</span> originalUrl;    
            <span style="color: #008080; font-style: italic;">// *** Fix up image path for ~ root app dir directory    </span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>originalUrl.<span style="color: #0000FF;">StartsWith</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>    
            <span style="color: #000000;">&#123;</span>        
                <span style="color: #FF0000;">string</span> newUrl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span>;        
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>            
                    newUrl <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">ApplicationPath</span> <span style="color: #008000;">+</span> originalUrl.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;//&quot;</span>, <span style="color: #666666;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span>;        
                <span style="color: #0600FF;">else</span>  <span style="color: #008080; font-style: italic;">// *** Not context: assume current directory is the base directory              </span>
                    <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Invalid URL: Relative URL not allowed.&quot;</span><span style="color: #000000;">&#41;</span>;                                                
                <span style="color: #008080; font-style: italic;">// *** Just to be sure fix up any double slashes        </span>
                <span style="color: #0600FF;">return</span> newUrl;    
            <span style="color: #000000;">&#125;</span>     
            <span style="color: #0600FF;">return</span> originalUrl;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// Works like Control.ResolveUrl including support for ~ syntax</span>
        <span style="color: #008080; font-style: italic;">/// but returns an absolute URL.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ServerUrl&quot;&gt;Any Url, either App relative or fully qualified&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;forceHttps&quot;&gt;if true forces the url to use https&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> ResolveServerUrl<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> serverUrl, <span style="color: #FF0000;">bool</span> forceHttps<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>    <span style="color: #008080; font-style: italic;">// *** Is it already an absolute Url?    </span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>serverUrl.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;://&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&gt;</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>        
                <span style="color: #0600FF;">return</span> serverUrl;     
            <span style="color: #008080; font-style: italic;">// *** Start by fixing up the Url an Application relative Url   </span>
            <span style="color: #FF0000;">string</span> newUrl <span style="color: #008000;">=</span> ResolveUrl<span style="color: #000000;">&#40;</span>serverUrl<span style="color: #000000;">&#41;</span>;     
            Uri originalUri <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">Url</span>;    
            newUrl <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>forceHttps <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;https&quot;</span> <span style="color: #008000;">:</span> originalUri.<span style="color: #0000FF;">Scheme</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;://&quot;</span> <span style="color: #008000;">+</span> originalUri.<span style="color: #0000FF;">Authority</span> <span style="color: #008000;">+</span> newUrl;     
            <span style="color: #0600FF;">return</span> newUrl;
        <span style="color: #000000;">&#125;</span>  
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// This method returns a fully qualified absolute server Url which includes</span>
        <span style="color: #008080; font-style: italic;">/// the protocol, server, port in addition to the server relative Url.</span>
        <span style="color: #008080; font-style: italic;">/// </span>
        <span style="color: #008080; font-style: italic;">/// It work like Page.ResolveUrl, but adds these to the beginning.</span>
        <span style="color: #008080; font-style: italic;">/// This method is useful for generating Urls for AJAX methods</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ServerUrl&quot;&gt;Any Url, either App relative or fully qualified&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> ResolveServerUrl<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> serverUrl<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>    
            <span style="color: #0600FF;">return</span> ResolveServerUrl<span style="color: #000000;">&#40;</span>serverUrl, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span></pre></div></div>

<p>
<strong>Usage:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"> sb.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;p class='{0}'&gt;&quot;</span>, row.<span style="color: #0000FF;">WidgetItemClass</span><span style="color: #000000;">&#41;</span>;
                        sb.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;a href='{0}'&gt;&lt;img border='0' src='{1}' alt='More Info'/&gt;&lt;/a&gt;&quot;</span>,
                                            ProductsService.<span style="color: #0000FF;">GetProductUrl</span><span style="color: #000000;">&#40;</span>row.<span style="color: #0000FF;">ProductID</span><span style="color: #000000;">&#41;</span>,
                                            ResolveServerUrl<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/GetWhiteLabelFile.aspx?whiteLabelFileID=&quot;</span> <span style="color: #008000;">+</span> row.<span style="color: #0000FF;">WidgetItemLinkImageID</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
                        sb.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;/p&gt;&quot;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/resolve-url-functions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Send message in Twitter using .NET</title>
		<link>http://fransiscuss.com/send-message-in-twitter-using-net/</link>
		<comments>http://fransiscuss.com/send-message-in-twitter-using-net/#comments</comments>
		<pubDate>Sun, 24 May 2009 12:57:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Send message using Twitter in .NET]]></category>

		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=229</guid>
		<description><![CDATA[This code snippet is used to wrap Twitter API to send message. It&#8217;s not a complete Twitter wrapper but It can send message using Twitter easily. There are two overloaded constructors where both of them requires user name and password to be passed and proxy server is used as the overloaded parameter.

using System;
using System.IO;
using System.Net;
using [...]]]></description>
			<content:encoded><![CDATA[<p>This code snippet is used to wrap Twitter API to send message. It&#8217;s not a complete Twitter wrapper but It can send message using Twitter easily. There are two overloaded constructors where both of them requires user name and password to be passed and proxy server is used as the overloaded parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Xml</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span>;
&nbsp;
<span style="color: #0600FF;">namespace</span> Helpers
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TwitterHelpers
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _twitterJsonUrl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://twitter.com/statuses/update.json&quot;</span>;
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> TwitterJsonUrl
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _twitterJsonUrl; <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> _twitterJsonUrl <span style="color: #008000;">=</span> value; <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _twitterUser <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> TwitterUser
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _twitterUser; <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> _twitterUser <span style="color: #008000;">=</span> value; <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _twitterPass <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> TwitterPass
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _twitterPass; <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> _twitterPass <span style="color: #008000;">=</span> value; <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _proxyServer <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> ProxyServer
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _proxyServer; <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> _proxyServer <span style="color: #008000;">=</span> value; <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> SendTwitterMessage<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> message<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>HttpWebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>TwitterJsonUrl<span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">System.<span style="color: #0000FF;">Net</span></span>.<span style="color: #0000FF;">ServicePointManager</span>.<span style="color: #0000FF;">Expect100Continue</span> <span style="color: #008000;">=</span> false;
&nbsp;
                <span style="color: #FF0000;">string</span> post <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
                <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>TextWriter writer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringWriter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    writer.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;status={0}&quot;</span>, HttpUtility.<span style="color: #0000FF;">UrlEncode</span><span style="color: #000000;">&#40;</span>message.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>0,<span style="color: #FF0000;">140</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
                    post <span style="color: #008000;">=</span> writer.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
&nbsp;
                SetRequestParams<span style="color: #000000;">&#40;</span>request<span style="color: #000000;">&#41;</span>;
&nbsp;
                request.<span style="color: #0000FF;">Credentials</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<span style="color: #000000;">&#40;</span>TwitterUser, TwitterPass<span style="color: #000000;">&#41;</span>;
&nbsp;
                <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>Stream requestStream <span style="color: #008000;">=</span> request.<span style="color: #0000FF;">GetRequestStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>StreamWriter writer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamWriter<span style="color: #000000;">&#40;</span>requestStream<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        writer.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>post<span style="color: #000000;">&#41;</span>;
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                WebResponse response <span style="color: #008000;">=</span> request.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #FF0000;">string</span> content;
&nbsp;
                <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>Stream responseStream <span style="color: #008000;">=</span> response.<span style="color: #0000FF;">GetResponseStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>StreamReader reader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>responseStream<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        content <span style="color: #008000;">=</span> reader.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF;">return</span> content;
&nbsp;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">throw</span> ex;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> SetRequestParams<span style="color: #000000;">&#40;</span>HttpWebRequest request<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            request.<span style="color: #0000FF;">Timeout</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">500000</span>;
            request.<span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;POST&quot;</span>;
            request.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/x-www-form-urlencoded&quot;</span>;
            request.<span style="color: #0000FF;">UserAgent</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;My Twitter Application&quot;</span>;
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>_proxyServer<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                request.<span style="color: #0000FF;">Proxy</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebProxy<span style="color: #000000;">&#40;</span>_proxyServer, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> TwitterHelpers<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> userName, <span style="color: #FF0000;">string</span> userPassword, <span style="color: #FF0000;">string</span> proxyServer<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _twitterUser <span style="color: #008000;">=</span> userName;
            _twitterPass <span style="color: #008000;">=</span> userPassword;
            _proxyServer <span style="color: #008000;">=</span> proxyServer;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> TwitterHelpers<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> userName, <span style="color: #FF0000;">string</span> userPassword<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _twitterUser <span style="color: #008000;">=</span> userName;
            _twitterPass <span style="color: #008000;">=</span> userPassword;
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Usage:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span>;
&nbsp;
<span style="color: #0600FF;">namespace</span> ConsoleAppTest
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">class</span> Program
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> TwitterHelpers _twitterHelpers <span style="color: #008000;">=</span> null;
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> TwitterHelpers TwitterHelpers
        <span style="color: #000000;">&#123;</span>
            get 
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_twitterHelpers <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    _twitterHelpers <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TwitterHelpers<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;twitteruser&quot;</span>, <span style="color: #666666;">&quot;twitterpassword&quot;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF;">return</span> _twitterHelpers;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            TwitterHelpers.<span style="color: #0000FF;">SendTwitterMessage</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Hello World!!&quot;</span><span style="color: #000000;">&#41;</span>;
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Press any key to continue...&quot;</span><span style="color: #000000;">&#41;</span>;
            Console.<span style="color: #0000FF;">ReadKey</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/send-message-in-twitter-using-net/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Upload Multiple Files to FTP in .NET</title>
		<link>http://fransiscuss.com/upload-multiple-files-to-ftp-in-net/</link>
		<comments>http://fransiscuss.com/upload-multiple-files-to-ftp-in-net/#comments</comments>
		<pubDate>Sun, 24 May 2009 12:47:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[VB.NET]]></category>

		<category><![CDATA[Asynchronous Upload]]></category>

		<category><![CDATA[The remote server returned an error: (503) Bad sequence of commands]]></category>

		<category><![CDATA[Upload Files to FTP]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=227</guid>
		<description><![CDATA[I&#8217;ve played around with FTP couple of weeks back. My application is required to feed FTP folder with 1000+ files daily. I&#8217;ve been googling around and I found one of the methods is to use WebApplication so I decided to put this WebApplication method to upload while iterating every single files in the directory. After 5 files, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve played around with FTP couple of weeks back. My application is required to feed FTP folder with 1000+ files daily. I&#8217;ve been googling around and I found one of the methods is to use <strong>WebApplication</strong> so I decided to put this WebApplication method to upload while iterating every single files in the directory. After 5 files, it keeps giving me error <strong>The remote server returned an error: (503) Bad sequence of commands. </strong>Strangely the only the first 4 files always uploaded successfully, my thought was the FTP server forcely terminated the connection which is right. After googling I found that we should set <strong>KeepAlive</strong> property to false which means new connection is always created instead of maintained but unfortunately <strong>WebClient</strong> class doesn&#8217;t have KeepAlive property. Finally, I&#8217;ve come up with this code which solves my issue in uploading 1000+ files one after the other by doing Asynchronous Upload</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span>;
&nbsp;
<span style="color: #0600FF;">namespace</span> Helpers
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> FtpHelpers
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> FtpState
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">private</span> ManualResetEvent wait;
            <span style="color: #0600FF;">private</span> FtpWebRequest request;
            <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> fileName;
            <span style="color: #0600FF;">private</span> Exception operationException <span style="color: #008000;">=</span> null;
            <span style="color: #FF0000;">string</span> status;
&nbsp;
            <span style="color: #0600FF;">public</span> FtpState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                wait <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ManualResetEvent<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">public</span> ManualResetEvent OperationComplete
            <span style="color: #000000;">&#123;</span>
                get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> wait; <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">public</span> FtpWebRequest Request
            <span style="color: #000000;">&#123;</span>
                get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> request; <span style="color: #000000;">&#125;</span>
                set <span style="color: #000000;">&#123;</span> request <span style="color: #008000;">=</span> value; <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FileName
            <span style="color: #000000;">&#123;</span>
                get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> fileName; <span style="color: #000000;">&#125;</span>
                set <span style="color: #000000;">&#123;</span> fileName <span style="color: #008000;">=</span> value; <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">public</span> Exception OperationException
            <span style="color: #000000;">&#123;</span>
                get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> operationException; <span style="color: #000000;">&#125;</span>
                set <span style="color: #000000;">&#123;</span> operationException <span style="color: #008000;">=</span> value; <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> StatusDescription
            <span style="color: #000000;">&#123;</span>
                get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> status; <span style="color: #000000;">&#125;</span>
                set <span style="color: #000000;">&#123;</span> status <span style="color: #008000;">=</span> value; <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AsynchronousUpload<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> destinationPath, <span style="color: #FF0000;">string</span> sourcePath, <span style="color: #FF0000;">string</span> userName, <span style="color: #FF0000;">string</span> password<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Create a Uri instance with the specified URI string.</span>
            <span style="color: #008080; font-style: italic;">// If the URI is not correctly formed, the Uri constructor</span>
            <span style="color: #008080; font-style: italic;">// will throw an exception.</span>
            ManualResetEvent waitObject;
&nbsp;
            Uri target <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Uri<span style="color: #000000;">&#40;</span>destinationPath<span style="color: #000000;">&#41;</span>;
            <span style="color: #FF0000;">string</span> fileName <span style="color: #008000;">=</span> sourcePath;
            FtpState state <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FtpState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            FtpWebRequest request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>FtpWebRequest<span style="color: #000000;">&#41;</span>WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>target<span style="color: #000000;">&#41;</span>;
            request.<span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> WebRequestMethods.<span style="color: #0000FF;">Ftp</span>.<span style="color: #0000FF;">UploadFile</span>;
            request.<span style="color: #0000FF;">KeepAlive</span> <span style="color: #008000;">=</span> false;
            request.<span style="color: #0000FF;">Proxy</span> <span style="color: #008000;">=</span> null;
&nbsp;
            <span style="color: #008080; font-style: italic;">// This example uses anonymous logon.</span>
            <span style="color: #008080; font-style: italic;">// The request is anonymous by default; the credential does not have to be specified. </span>
            <span style="color: #008080; font-style: italic;">// The example specifies the credential only to</span>
            <span style="color: #008080; font-style: italic;">// control how actions are logged on the server.</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>userName<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>password<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                request.<span style="color: #0000FF;">Credentials</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<span style="color: #000000;">&#40;</span>userName, password<span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Store the request in the object that we pass into the</span>
            <span style="color: #008080; font-style: italic;">// asynchronous operations.</span>
            state.<span style="color: #0000FF;">Request</span> <span style="color: #008000;">=</span> request;
            state.<span style="color: #0000FF;">FileName</span> <span style="color: #008000;">=</span> fileName;
&nbsp;
            <span style="color: #008080; font-style: italic;">// Get the event to wait on.</span>
            waitObject <span style="color: #008000;">=</span> state.<span style="color: #0000FF;">OperationComplete</span>;
&nbsp;
            <span style="color: #008080; font-style: italic;">// Asynchronously get the stream for the file contents.</span>
            request.<span style="color: #0000FF;">BeginGetRequestStream</span><span style="color: #000000;">&#40;</span>
                <span style="color: #008000;">new</span> AsyncCallback<span style="color: #000000;">&#40;</span>EndGetStreamCallback<span style="color: #000000;">&#41;</span>,
                state
            <span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #008080; font-style: italic;">// Block the current thread until all operations are complete.</span>
            waitObject.<span style="color: #0000FF;">WaitOne</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #008080; font-style: italic;">// The operations either completed or threw an exception.</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>state.<span style="color: #0000FF;">OperationException</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">throw</span> state.<span style="color: #0000FF;">OperationException</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> EndGetStreamCallback<span style="color: #000000;">&#40;</span>IAsyncResult ar<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            FtpState state <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>FtpState<span style="color: #000000;">&#41;</span>ar.<span style="color: #0000FF;">AsyncState</span>;
&nbsp;
            Stream requestStream <span style="color: #008000;">=</span> null;
            <span style="color: #008080; font-style: italic;">// End the asynchronous call to get the request stream.</span>
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                requestStream <span style="color: #008000;">=</span> state.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">EndGetRequestStream</span><span style="color: #000000;">&#40;</span>ar<span style="color: #000000;">&#41;</span>;
                <span style="color: #008080; font-style: italic;">// Copy the file contents to the request stream.</span>
                <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> bufferLength <span style="color: #008000;">=</span> <span style="color: #FF0000;">2048</span>;
                <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span>bufferLength<span style="color: #000000;">&#93;</span>;
                <span style="color: #FF0000;">int</span> count <span style="color: #008000;">=</span> 0;
                <span style="color: #FF0000;">int</span> readBytes <span style="color: #008000;">=</span> 0;
                FileStream stream <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">OpenRead</span><span style="color: #000000;">&#40;</span>state.<span style="color: #0000FF;">FileName</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #0600FF;">do</span>
                <span style="color: #000000;">&#123;</span>
                    readBytes <span style="color: #008000;">=</span> stream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>buffer, 0, bufferLength<span style="color: #000000;">&#41;</span>;
                    requestStream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>buffer, 0, readBytes<span style="color: #000000;">&#41;</span>;
                    count <span style="color: #008000;">+=</span> readBytes;
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>readBytes <span style="color: #008000;">!=</span> 0<span style="color: #000000;">&#41;</span>;
                <span style="color: #008080; font-style: italic;">// IMPORTANT: Close the request stream before sending the request.</span>
                requestStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #008080; font-style: italic;">// Asynchronously get the response to the upload request.</span>
                state.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">BeginGetResponse</span><span style="color: #000000;">&#40;</span>
                    <span style="color: #008000;">new</span> AsyncCallback<span style="color: #000000;">&#40;</span>EndGetResponseCallback<span style="color: #000000;">&#41;</span>,
                    state
                <span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #008080; font-style: italic;">// Return exceptions to the main application thread.</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                state.<span style="color: #0000FF;">OperationException</span> <span style="color: #008000;">=</span> e;
                state.<span style="color: #0000FF;">OperationComplete</span>.<span style="color: #0000FF;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                return;
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// The EndGetResponseCallback method  </span>
        <span style="color: #008080; font-style: italic;">// completes a call to BeginGetResponse.</span>
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> EndGetResponseCallback<span style="color: #000000;">&#40;</span>IAsyncResult ar<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            FtpState state <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>FtpState<span style="color: #000000;">&#41;</span>ar.<span style="color: #0000FF;">AsyncState</span>;
            FtpWebResponse response <span style="color: #008000;">=</span> null;
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                response <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>FtpWebResponse<span style="color: #000000;">&#41;</span>state.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">EndGetResponse</span><span style="color: #000000;">&#40;</span>ar<span style="color: #000000;">&#41;</span>;
                response.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                state.<span style="color: #0000FF;">StatusDescription</span> <span style="color: #008000;">=</span> response.<span style="color: #0000FF;">StatusDescription</span>;
                <span style="color: #008080; font-style: italic;">// Signal the main application thread that </span>
                <span style="color: #008080; font-style: italic;">// the operation is complete.</span>
                state.<span style="color: #0000FF;">OperationComplete</span>.<span style="color: #0000FF;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #008080; font-style: italic;">// Return exceptions to the main application thread.</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                state.<span style="color: #0000FF;">OperationException</span> <span style="color: #008000;">=</span> e;
                state.<span style="color: #0000FF;">OperationComplete</span>.<span style="color: #0000FF;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Usage:</p>

<div class="wp_syntax"><div class="code"><pre class="vb vb" style="font-family:monospace;">#Region <span style="color: #ff0000;">&quot;Properties&quot;</span>
&nbsp;
    <span style="color: #b1b100;">Private</span> _ftpUploader <span style="color: #b1b100;">As</span> FtpHelpers = <span style="color: #b1b100;">Nothing</span>
&nbsp;
    <span style="color: #b1b100;">Private</span> ReadOnly <span style="color: #b1b100;">Property</span> FtpUploader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">As</span> FtpHelpers
        <span style="color: #b1b100;">Get</span>
            <span style="color: #b1b100;">If</span> <span style="color: #66cc66;">&#40;</span>_ftpUploader Is <span style="color: #b1b100;">Nothing</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">Then</span>
                _ftpUploader = <span style="color: #b1b100;">New</span> FtpHelpers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
&nbsp;
            <span style="color: #b1b100;">Return</span> _ftpUploader
        <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Get</span>
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Property</span>
&nbsp;
#End Region
&nbsp;
<span style="color: #b1b100;">Private</span> <span style="color: #b1b100;">Sub</span> ProcessFilesToFTP<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #808080;">' make a reference to a directory</span>
        <span style="color: #b1b100;">Dim</span> di <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">New</span> IO.<span style="color: #66cc66;">DirectoryInfo</span><span style="color: #66cc66;">&#40;</span>ConfigurationManager.<span style="color: #66cc66;">AppSettings</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;OutputZIPDirectory&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">Dim</span> jobFiles <span style="color: #b1b100;">As</span> IO.<span style="color: #66cc66;">FileInfo</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> = di.<span style="color: #66cc66;">GetFiles</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        Console.<span style="color: #66cc66;">WriteLine</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">String</span>.<span style="color: #b1b100;">Format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;FTP Location: {0}&quot;</span>, ConfigurationManager.<span style="color: #66cc66;">AppSettings</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ftplocation&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #808080;">'list the names of all files in the specified directory</span>
        <span style="color: #b1b100;">For</span> Each jobFile <span style="color: #b1b100;">As</span> IO.<span style="color: #66cc66;">FileInfo</span> In jobFiles
&nbsp;
            <span style="color: #808080;">'process only zip file</span>
            <span style="color: #b1b100;">If</span> <span style="color: #66cc66;">&#40;</span>jobFile.<span style="color: #66cc66;">FullName</span>.<span style="color: #66cc66;">Contains</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;zip&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">Then</span>
                Console.<span style="color: #66cc66;">WriteLine</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">String</span>.<span style="color: #b1b100;">Format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Upload file {0}&quot;</span>, jobFile.<span style="color: #b1b100;">Name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
                FtpUploader.<span style="color: #66cc66;">AsynchronousUpload</span><span style="color: #66cc66;">&#40;</span>ConfigurationManager.<span style="color: #66cc66;">AppSettings</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ftplocation&quot;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;/&quot;</span> + jobFile.<span style="color: #b1b100;">Name</span>, jobFile.<span style="color: #66cc66;">FullName</span>, _
                                   ConfigurationManager.<span style="color: #66cc66;">AppSettings</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ftpuser&quot;</span><span style="color: #66cc66;">&#41;</span>, _
                                   ConfigurationManager.<span style="color: #66cc66;">AppSettings</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ftppassword&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
            <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
&nbsp;
        <span style="color: #b1b100;">Next</span>
&nbsp;
        Console.<span style="color: #66cc66;">WriteLine</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">String</span>.<span style="color: #b1b100;">Format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;XML files has been uploaded to {0}&quot;</span>, ConfigurationManager.<span style="color: #66cc66;">AppSettings</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ftplocation&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Sub</span></pre></div></div>

<p>The drawback with this code is in the FTP connection where the connection always reinitialized for every single file.</p>
]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/upload-multiple-files-to-ftp-in-net/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SQL Server Function using CLR</title>
		<link>http://fransiscuss.com/sql-server-function-using-clr/</link>
		<comments>http://fransiscuss.com/sql-server-function-using-clr/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 23:47:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=196</guid>
		<description><![CDATA[UPDATED: I&#8217;ve added one function to write from BLOB in SQL Server table to the disk straight away
I thought this article might be useful for anyone that wants to implement .NET code to SQL server level. In this case I really need CLR because I want to do compression of images and I believe it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATED: I&#8217;ve added one function to write from BLOB in SQL Server table to the disk straight away</strong></p>
<p>I thought this article might be useful for anyone that wants to implement .NET code to SQL server level. In this case I really need CLR because I want to do compression of images and I believe it&#8217;s not possible to do that using pure SQL server stored procedure and I&#8217;m trying to avoid creating a .NET application just for compression of images through row by row processing. </p>
<p>Here we start:</p>
<p>This is your C#/.NET code, you need to declare it as SQL function or SQL stored procedure</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data.SqlTypes</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Server</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Zip </span><span style="color: #008000;">=</span> ICSharpCode.<span style="color: #0000FF;">SharpZipLib</span>.<span style="color: #0000FF;">Zip</span>.<span style="color: #0000FF;">Compression</span>;
&nbsp;
<span style="color: #0600FF;">namespace</span> CLRCompressionFunctions
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> partial <span style="color: #FF0000;">class</span> CompressionCore
    <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#91;</span>SqlFunction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> SqlBytes fn_Compress<span style="color: #000000;">&#40;</span>SqlBytes uncompressedBytes<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>uncompressedBytes.<span style="color: #0000FF;">IsNull</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> uncompressedBytes;
&nbsp;
            MemoryStream memory <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            ICSharpCode.<span style="color: #0000FF;">SharpZipLib</span>.<span style="color: #0000FF;">Zip</span>.<span style="color: #0000FF;">Compression</span>.<span style="color: #0000FF;">Streams</span>.<span style="color: #0000FF;">DeflaterOutputStream</span> stream <span style="color: #008000;">=</span>
                <span style="color: #008000;">new</span> ICSharpCode.<span style="color: #0000FF;">SharpZipLib</span>.<span style="color: #0000FF;">Zip</span>.<span style="color: #0000FF;">Compression</span>.<span style="color: #0000FF;">Streams</span>.<span style="color: #0000FF;">DeflaterOutputStream</span><span style="color: #000000;">&#40;</span>memory, <span style="color: #008000;">new</span> Zip.<span style="color: #0000FF;">Deflater</span><span style="color: #000000;">&#40;</span>Zip.<span style="color: #0000FF;">Deflater</span>.<span style="color: #0000FF;">BEST_COMPRESSION</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">131072</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            stream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>uncompressedBytes.<span style="color: #0000FF;">Buffer</span>, 0, Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>uncompressedBytes.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
            stream.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            stream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            stream <span style="color: #008000;">=</span> null;
&nbsp;
            <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> SqlBytes<span style="color: #000000;">&#40;</span>memory.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>SqlFunction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> SqlBytes fn_Decompress<span style="color: #000000;">&#40;</span>SqlBytes compressedBytes<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>compressedBytes.<span style="color: #0000FF;">IsNull</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> compressedBytes;
&nbsp;
            ICSharpCode.<span style="color: #0000FF;">SharpZipLib</span>.<span style="color: #0000FF;">Zip</span>.<span style="color: #0000FF;">Compression</span>.<span style="color: #0000FF;">Streams</span>.<span style="color: #0000FF;">InflaterInputStream</span> stream <span style="color: #008000;">=</span>
                <span style="color: #008000;">new</span> ICSharpCode.<span style="color: #0000FF;">SharpZipLib</span>.<span style="color: #0000FF;">Zip</span>.<span style="color: #0000FF;">Compression</span>.<span style="color: #0000FF;">Streams</span>.<span style="color: #0000FF;">InflaterInputStream</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span>compressedBytes.<span style="color: #0000FF;">Buffer</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
            MemoryStream memory <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> writeData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">4096</span><span style="color: #000000;">&#93;</span>;
            <span style="color: #FF0000;">int</span> size;
&nbsp;
            <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                size <span style="color: #008000;">=</span> stream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>writeData, 0, writeData.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>size <span style="color: #008000;">&amp;</span>gt; 0<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    memory.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>writeData, 0, size<span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">else</span> break;
            <span style="color: #000000;">&#125;</span>
            stream.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            stream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            stream <span style="color: #008000;">=</span> null;
&nbsp;
            <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> SqlBytes<span style="color: #000000;">&#40;</span>memory.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>SqlFunction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> SqlString fn_WriteFile<span style="color: #000000;">&#40;</span>SqlString path, SqlBytes bytesFile, SqlBoolean isCompressed<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> returnString <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
&nbsp;
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//check if the file exists or not</span>
                FileStream myFStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileStream<span style="color: #000000;">&#40;</span>path.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, FileMode.<span style="color: #0000FF;">OpenOrCreate</span>, FileAccess.<span style="color: #0000FF;">ReadWrite</span><span style="color: #000000;">&#41;</span>;
&nbsp;
                SqlBytes fileBytes <span style="color: #008000;">=</span> bytesFile;
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isCompressed<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    fileBytes <span style="color: #008000;">=</span> fn_Decompress<span style="color: #000000;">&#40;</span>bytesFile<span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #FF0000;">int</span> Length <span style="color: #008000;">=</span> <span style="color: #FF0000;">256</span>;
                <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#91;</span>Length<span style="color: #000000;">&#93;</span>;
&nbsp;
                Stream readStream <span style="color: #008000;">=</span> fileBytes.<span style="color: #0000FF;">Stream</span>;
&nbsp;
                <span style="color: #FF0000;">int</span> bytesRead <span style="color: #008000;">=</span> readStream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>buffer, 0, Length<span style="color: #000000;">&#41;</span>;
&nbsp;
                <span style="color: #008080; font-style: italic;">// write the required bytes</span>
                <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>bytesRead <span style="color: #008000;">&gt;</span> 0<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    myFStream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>buffer, 0, bytesRead<span style="color: #000000;">&#41;</span>;
                    bytesRead <span style="color: #008000;">=</span> readStream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>buffer, 0, Length<span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
&nbsp;
                readStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                myFStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                returnString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;File is written successfully&quot;</span>;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                returnString <span style="color: #008000;">=</span> ex.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> SqlString<span style="color: #000000;">&#40;</span>returnString<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Installation time to your SQL Server, You need to register your assembly(.dll) as well as referenced Assembly to SQL Server</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> TestAssembly <span style="color: #993333; font-weight: bold;">SET</span> TRUSTWORTHY <span style="color: #993333; font-weight: bold;">ON</span>
GO
EXEC sp_configure <span style="color: #ff0000;">'clr enabled'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span>;
RECONFIGURE <span style="color: #993333; font-weight: bold;">WITH</span> OVERRIDE;
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> ASSEMBLY <span style="color: #66cc66;">&#91;</span>ICSharpCode<span style="color: #66cc66;">.</span>SharpZipLib<span style="color: #66cc66;">.</span>dll<span style="color: #66cc66;">&#93;</span>
                  <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'D:<span style="color: #000099; font-weight: bold;">\A</span>pplications<span style="color: #000099; font-weight: bold;">\F</span>ileCompressorApp<span style="color: #000099; font-weight: bold;">\C</span>LRCompressionFunctions<span style="color: #000099; font-weight: bold;">\D</span>eployment<span style="color: #000099; font-weight: bold;">\I</span>CSharpCode.SharpZipLib.dll'</span>
                  <span style="color: #993333; font-weight: bold;">WITH</span> PERMISSION_SET <span style="color: #66cc66;">=</span> UNSAFE
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> ASSEMBLY <span style="color: #66cc66;">&#91;</span>CLRCompressionFunctions<span style="color: #66cc66;">&#93;</span>
                  <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'D:<span style="color: #000099; font-weight: bold;">\A</span>pplications<span style="color: #000099; font-weight: bold;">\F</span>ileCompressorApp<span style="color: #000099; font-weight: bold;">\C</span>LRCompressionFunctions<span style="color: #000099; font-weight: bold;">\D</span>eployment<span style="color: #000099; font-weight: bold;">\C</span>LRCompressionFunctions.dll'</span>
                  <span style="color: #993333; font-weight: bold;">WITH</span> PERMISSION_SET <span style="color: #66cc66;">=</span> EXTERNAL_ACCESS
GO</pre></div></div>

<p>PERMISSION_SET = SAFE only if you don&#8217;t want the assembly accessing external resources such as writing to disk, but in this case the function is used to write into the disk<br />
e.g How about if you want to use/register System.Drawing to your assembly? Yes you can do it by using</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> ASSEMBLY <span style="color: #66cc66;">&#91;</span>System<span style="color: #66cc66;">.</span>Drawing<span style="color: #66cc66;">.</span>dll<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'C:<span style="color: #000099; font-weight: bold;">\W</span>INDOWS<span style="color: #000099; font-weight: bold;">\M</span>icrosoft.NET<span style="color: #000099; font-weight: bold;">\F</span>ramework<span style="color: #000099; font-weight: bold;">\v</span>2.0.50727<span style="color: #000099; font-weight: bold;">\S</span>ystem.Drawing.dll'</span>
<span style="color: #993333; font-weight: bold;">WITH</span> PERMISSION_SET <span style="color: #66cc66;">=</span> UNSAFE</pre></div></div>

<p>You need to enable the CLR on SQL server in order to use your function</p>
<p>Now you need to create your function based on your assembly</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>fn_Compress<span style="color: #66cc66;">&#93;</span>           <span style="color: #66cc66;">&#40;</span>
                 @uncompressedBytes varbinary<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            RETURNS varbinary<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span>
            <span style="color: #993333; font-weight: bold;">AS</span>    EXTERNAL NAME CLRCompressionFunctions<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>CLRCompressionFunctions<span style="color: #66cc66;">.</span>CompressionCore<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span>fn_Compress
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>fn_Decompress<span style="color: #66cc66;">&#93;</span>           <span style="color: #66cc66;">&#40;</span>
                 @uncompressedBytes varbinary<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            RETURNS varbinary<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span>
            <span style="color: #993333; font-weight: bold;">AS</span>    EXTERNAL NAME CLRCompressionFunctions<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>CLRCompressionFunctions<span style="color: #66cc66;">.</span>CompressionCore<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span>fn_Decompress
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>fn_WriteFile<span style="color: #66cc66;">&#93;</span>           <span style="color: #66cc66;">&#40;</span>
                 @path nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
				 @bytesFile varbinary<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
				 @bitCompressed bit<span style="color: #66cc66;">&#41;</span>
			RETURNS nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #993333; font-weight: bold;">AS</span>    EXTERNAL NAME CLRCompressionFunctions<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>CLRCompressionFunctions<span style="color: #66cc66;">.</span>CompressionCore<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span>fn_WriteFile
GO</pre></div></div>

<p>Usage, It&#8217;s the same as you call a function in SQL Server</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>fn_Compress<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>testimage<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> tblImages
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>fn_Decompress<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>imgFileContent<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> TABLE_NAME
GO	
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>fn_WriteFile<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'c:<span style="color: #000099; font-weight: bold;">\t</span>est.pdf'</span><span style="color: #66cc66;">,</span>imgFileContent<span style="color: #66cc66;">,</span> 0<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> TABLE_NAME
GO</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/sql-server-function-using-clr/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Linq.Binary(SQL Image Data Type) Type to File Stream</title>
		<link>http://fransiscuss.com/linqbinarysql-image-data-type-type-to-file-stream/</link>
		<comments>http://fransiscuss.com/linqbinarysql-image-data-type-type-to-file-stream/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 23:46:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=192</guid>
		<description><![CDATA[I got a column with Data Type of Image in SQL Server. What I would like to do is to omit this data to a FileStream or to a file. I tried to read the data using LINQ and I found the data type detected for that particular column is System.Data.Linq.Binary. I was expecting it [...]]]></description>
			<content:encoded><![CDATA[<p>I got a column with Data Type of Image in SQL Server. What I would like to do is to omit this data to a FileStream or to a file. I tried to read the data using LINQ and I found the data type detected for that particular column is <strong>System.Data.Linq.Binary</strong>. I was expecting it to be Byte Data type. So I need to convert the Binary to byte then to File Stream. But I found a simpler way by using &#8220;ToArray&#8221; properties and cast it back to byte solves my problem.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;">    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>tblExpReceiptFile file <span style="color: #0600FF;">in</span> ExpReceiptFactory.<span style="color: #0000FF;">tblExpReceiptFileSelect</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span> fileName <span style="color: #008000;">=</span> file.<span style="color: #0000FF;">vcFileName</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>,<span style="color: #666666;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span>;
                FileStream fileStream <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>DirectoryPath <span style="color: #008000;">+</span> <span style="">@&quot;\&quot;</span> <span style="color: #008000;">+</span> fileName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;.pdf&quot;</span><span style="color: #000000;">&#41;</span>;
                fileStream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>file.<span style="color: #0000FF;">imgFileContent</span>.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, 0, <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>file.<span style="color: #0000FF;">imgFileContent</span>.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span>;
                fileStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/linqbinarysql-image-data-type-type-to-file-stream/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SQL Methods in LINQ</title>
		<link>http://fransiscuss.com/sql-methods-in-linq/</link>
		<comments>http://fransiscuss.com/sql-methods-in-linq/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 06:59:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=190</guid>
		<description><![CDATA[I&#8217;m trying to implement DateDiff in LINQ. I would like to get all records with the same date and ignoring the timestamp.
Here is my code snippet which doesn&#8217;t work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 public int tblExpReceiptFileUniqueID&#40;string chCreateStaffCode, string vcFileName, 
                    [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m trying to implement DateDiff in LINQ. I would like to get all records with the same date and ignoring the timestamp.</p>
<p>Here is my code snippet which doesn&#8217;t work</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;"> <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> tblExpReceiptFileUniqueID<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> chCreateStaffCode, <span style="color: #FF0000;">string</span> vcFileName, 
                                                <span style="color: #FF0000;">long</span> intFileSize, DateTime<span style="color: #008000;">?</span> sdCreateDate<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span> 
            var expReceiptFile <span style="color: #008000;">=</span> from ef <span style="color: #0600FF;">in</span> DataContext.<span style="color: #0000FF;">tblExpReceiptFiles</span> orderby ef.<span style="color: #0000FF;">intFileID</span> 
                                 where ef.<span style="color: #0000FF;">chCreateStaffCode</span> <span style="color: #008000;">==</span> chCreateStaffCode
                                 <span style="color: #008000;">&amp;&amp;</span> ef.<span style="color: #0000FF;">vcFileName</span> <span style="color: #008000;">==</span> vcFileName 
                                 <span style="color: #008000;">&amp;&amp;</span> ef.<span style="color: #0000FF;">bintFileSize</span> <span style="color: #008000;">==</span> intFileSize
                                 <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>DateTime<span style="color: #000000;">&#41;</span>ef.<span style="color: #0000FF;">sdCreateDate</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Date</span> <span style="color: #008000;">==</span> sdCreateDate.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">Date</span>
                                 select ef;
&nbsp;
            tblExpReceiptFile expReceiptFileRec <span style="color: #008000;">=</span> expReceiptFile.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">return</span> expReceiptFileRec.<span style="color: #0000FF;">intFileID</span>;
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;m trying to cast the column to &#8220;DateTime&#8221; and use the &#8220;Date&#8221; property but I got this error<br />
<strong>&#8220;The Member  &#8216;System.DateTime.Date&#8217; has no supported translation to SQL&#8221;</strong></p>
<p>I thought of &#8220;DATEDIFF&#8221; function in SQL server and I&#8217;m trying to get my head around in implementing this using LINQ and I found that we can use a library called &#8220;SQLClient&#8221;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data.Linq.SqlClient</span>;</pre></td></tr></table></div>

<p>Code Snippet which is working</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> tblExpReceiptFileUniqueID<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> chCreateStaffCode, <span style="color: #FF0000;">string</span> vcFileName, 
                                                <span style="color: #FF0000;">long</span> intFileSize, DateTime<span style="color: #008000;">?</span> sdCreateDate<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span> 
            var expReceiptFile <span style="color: #008000;">=</span> from ef <span style="color: #0600FF;">in</span> DataContext.<span style="color: #0000FF;">tblExpReceiptFiles</span> orderby ef.<span style="color: #0000FF;">intFileID</span> 
                                 where ef.<span style="color: #0000FF;">chCreateStaffCode</span> <span style="color: #008000;">==</span> chCreateStaffCode
                                 <span style="color: #008000;">&amp;&amp;</span> ef.<span style="color: #0000FF;">vcFileName</span> <span style="color: #008000;">==</span> vcFileName 
                                 <span style="color: #008000;">&amp;&amp;</span> ef.<span style="color: #0000FF;">bintFileSize</span> <span style="color: #008000;">==</span> intFileSize
                                 <span style="color: #008000;">&amp;&amp;</span> SqlMethods.<span style="color: #0000FF;">DateDiffDay</span><span style="color: #000000;">&#40;</span>ef.<span style="color: #0000FF;">sdCreateDate</span>, sdCreateDate.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">Date</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> 0
                                 select ef;
&nbsp;
            tblExpReceiptFile expReceiptFileRec <span style="color: #008000;">=</span> expReceiptFile.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">return</span> expReceiptFileRec.<span style="color: #0000FF;">intFileID</span>;
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/sql-methods-in-linq/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Convert DataTable to CSV Function</title>
		<link>http://fransiscuss.com/convert-datatable-to-csv-function/</link>
		<comments>http://fransiscuss.com/convert-datatable-to-csv-function/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 22:39:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://fransiscus.com.au/?p=174</guid>
		<description><![CDATA[This is the class that you can use to create a CSV file from DataTable. Basically what it does is to iterate each column and row in datatable and separate them with comma.
Class/Function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
&#160;
namespace BusinessLayer
&#123;
    public class CSVBuilder
    &#123;
       [...]]]></description>
			<content:encoded><![CDATA[<p>This is the class that you can use to create a CSV file from DataTable. Basically what it does is to iterate each column and row in datatable and separate them with comma.</p>
<p><strong>Class/Function:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span>;
&nbsp;
<span style="color: #0600FF;">namespace</span> BusinessLayer
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CSVBuilder
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> BuildCSVDocument<span style="color: #000000;">&#40;</span>DataTable table<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            StringBuilder   builder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            DataColumn      col;
&nbsp;
            <span style="color: #008080; font-style: italic;">// append header</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> index <span style="color: #008000;">=</span> 0; index <span style="color: #008000;">&lt;</span> table.<span style="color: #0000FF;">Columns</span>.<span style="color: #0000FF;">Count</span>; index<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                col <span style="color: #008000;">=</span> table.<span style="color: #0000FF;">Columns</span><span style="color: #000000;">&#91;</span>index<span style="color: #000000;">&#93;</span>;
                builder.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0},&quot;</span>, col.<span style="color: #0000FF;">ColumnName</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
            builder.<span style="color: #0000FF;">AppendLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #008080; font-style: italic;">// append rows</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>DataRow row <span style="color: #0600FF;">in</span> table.<span style="color: #0000FF;">Rows</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> values <span style="color: #008000;">=</span> row.<span style="color: #0000FF;">ItemArray</span>;
&nbsp;
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> index <span style="color: #008000;">=</span> 0; index <span style="color: #008000;">&lt;</span> values.<span style="color: #0000FF;">Length</span>; index<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #FF0000;">object</span> value <span style="color: #008000;">=</span> row<span style="color: #000000;">&#91;</span>index<span style="color: #000000;">&#93;</span>;
                    <span style="color: #FF0000;">string</span> valueString;
&nbsp;
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">is</span> DateTime<span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        valueString <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToDateTime</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;dd/MM/yyyy hh:mm&quot;</span><span style="color: #000000;">&#41;</span>;
                    <span style="color: #000000;">&#125;</span>
                    <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">is</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        valueString <span style="color: #008000;">=</span> value.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;'&quot;</span>, <span style="color: #666666;">&quot;`&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;,&quot;</span>, <span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>;
                    <span style="color: #000000;">&#125;</span>
                    <span style="color: #0600FF;">else</span>
                    <span style="color: #000000;">&#123;</span>
                        valueString <span style="color: #008000;">=</span> value.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                    <span style="color: #000000;">&#125;</span>
&nbsp;
                    builder.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0},&quot;</span>, valueString<span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
&nbsp;
                builder.<span style="color: #0000FF;">AppendLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> builder.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> StreamCSV<span style="color: #000000;">&#40;</span>DataTable table, HttpResponse response, <span style="color: #FF0000;">string</span> fileName<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// convert the extract to CSV</span>
            <span style="color: #FF0000;">string</span> csv <span style="color: #008000;">=</span> BuildCSVDocument<span style="color: #000000;">&#40;</span>table<span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #008080; font-style: italic;">// Send it to browser</span>
            response.<span style="color: #0000FF;">ClearContent</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Disposition&quot;</span>, <span style="color: #666666;">&quot;attachment; filename=&quot;</span> <span style="color: #008000;">+</span> fileName<span style="color: #000000;">&#41;</span>;
            response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;text/csv&quot;</span>;
&nbsp;
            <span style="color: #008080; font-style: italic;">// Write to the stream</span>
            StreamWriter sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamWriter<span style="color: #000000;">&#40;</span>response.<span style="color: #0000FF;">OutputStream</span><span style="color: #000000;">&#41;</span>;
            sw.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>csv<span style="color: #000000;">&#41;</span>;
            sw.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            response.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            response.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Usage</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;"> <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> GenerateMyCsv<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        lblErrorOrderID.<span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> false;
&nbsp;
        <span style="color: #0600FF;">try</span>
        <span style="color: #000000;">&#123;</span>
            tdsReports.<span style="color: #0000FF;">MissingBatchNumberSelectDataTable</span> dtBatch <span style="color: #008000;">=</span>
                ReportFactory.<span style="color: #0000FF;">GetMissingBatch</span><span style="color: #000000;">&#40;</span>txtOrderID.<span style="color: #0000FF;">Text</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #008080; font-style: italic;">//display no results returned message</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>dtBatch.<span style="color: #0000FF;">Rows</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">!=</span> 0<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
               CSVBuilder.<span style="color: #0000FF;">StreamCSV</span><span style="color: #000000;">&#40;</span>dtBatch , Response, <span style="color: #666666;">&quot;MyCSV.csv&quot;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ApplicationException ex<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            lblErrorOrderID.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> ex.<span style="color: #0000FF;">Message</span>;
            lblErrorOrderID.<span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> true;
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            lblErrorOrderID.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> ex.<span style="color: #0000FF;">Message</span>;
            lblErrorOrderID.<span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> true;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/convert-datatable-to-csv-function/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
