<?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</title>
	<atom:link href="http://fransiscuss.com/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>Shrink all databases and Set it to Simple in SQL Server</title>
		<link>http://fransiscuss.com/shrink-all-databases-and-set-it-to-simple-in-sql-server/</link>
		<comments>http://fransiscuss.com/shrink-all-databases-and-set-it-to-simple-in-sql-server/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 05:02:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=249</guid>
		<description><![CDATA[Normally, I need to run this script against dev sql server in order for me to free up some space

CREATE TABLE #temp_dbs_table
&#40;
&#91;db_name&#93; sysname NOT NULL PRIMARY KEY,
&#91;mod&#93; tinyint NOT NULL DEFAULT 1
&#41;
&#160;
INSERT INTO #temp_dbs_table ([db_name])
SELECT
name
FROM
master..sysdatabases
WHERE
dbid &#38;gt; 4 --- skip master, tempdb, model and msdb databases
&#160;
declare @db_name sysname
&#160;
SET @db_name = ''
&#160;
while @db_name IS NOT NULL
begin
SET @db_name [...]]]></description>
			<content:encoded><![CDATA[<p>Normally, I need to run this script against dev sql server in order for me to free up some space</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;">TABLE</span> <span style="color: #808080; font-style: italic;">#temp_dbs_table</span>
<span style="color: #66cc66;">&#40;</span>
<span style="color: #66cc66;">&#91;</span>db_name<span style="color: #66cc66;">&#93;</span> sysname <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#91;</span>mod<span style="color: #66cc66;">&#93;</span> tinyint <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #cc66cc;">1</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #808080; font-style: italic;">#temp_dbs_table ([db_name])</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>
name
<span style="color: #993333; font-weight: bold;">FROM</span>
master<span style="color: #66cc66;">..</span>sysdatabases
<span style="color: #993333; font-weight: bold;">WHERE</span>
dbid &amp;gt; <span style="color: #cc66cc;">4</span> <span style="color: #808080; font-style: italic;">--- skip master, tempdb, model and msdb databases</span>
&nbsp;
declare @db_name sysname
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @db_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">''</span>
&nbsp;
while @db_name <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
begin
<span style="color: #993333; font-weight: bold;">SET</span> @db_name <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> top <span style="color: #cc66cc;">1</span> @db_name <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>db_name<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #808080; font-style: italic;">#temp_dbs_table where [mod] = 1</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">IF</span> @db_name <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span>
break
&nbsp;
print <span style="color: #ff0000;">'--------------------------------------------------'</span>
&nbsp;
print <span style="color: #ff0000;">'&amp;gt; Database: '</span> <span style="color: #66cc66;">+</span> @db_name
&nbsp;
print <span style="color: #ff0000;">'&amp;gt; Changing recovery mode to simple'</span>
&nbsp;
declare @n_cmd nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @n_cmd <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'alter database ['</span> <span style="color: #66cc66;">+</span> @db_name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'] set recovery simple'</span>
&nbsp;
exec sp_executesql @n_cmd
&nbsp;
print <span style="color: #ff0000;">'&amp;gt; Shrinking database'</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @n_cmd <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'dbcc shrinkdatabase(['</span> <span style="color: #66cc66;">+</span> @db_name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'])'</span>
&nbsp;
exec sp_executesql @n_cmd
&nbsp;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #808080; font-style: italic;">#temp_dbs_table set [mod] = 0 where [db_name] = @db_name</span>
end
&nbsp;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#temp_dbs_table</span></pre></div></div>

<p>I got this script from <a href="http://www.sqlservercentral.com/scripts/Backup+%2F+Restore/61952/" target="_blank">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/shrink-all-databases-and-set-it-to-simple-in-sql-server/feed/</wfw:commentRss>
		</item>
		<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>TRY&#8230;.CATCH Rollback Transaction In SQL Server 2005</title>
		<link>http://fransiscuss.com/trycatch-rollback-transaction-in-sql-server-2005/</link>
		<comments>http://fransiscuss.com/trycatch-rollback-transaction-in-sql-server-2005/#comments</comments>
		<pubDate>Sat, 23 May 2009 01:16:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

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

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

		<category><![CDATA[TRY CATCH]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=215</guid>
		<description><![CDATA[This feature has been sometime in SQL Server 2005 in SQL Server 2000 you normally use @@TRANCOUNT to detect any exception but now in SQL Server 2005 you can use try catch block.
In this snippet, there are 2 INSERT statement and 1 UPDATE statement. I&#8217;m expecting when there is any failure (e.g the UPDATE statement [...]]]></description>
			<content:encoded><![CDATA[<p>This feature has been sometime in SQL Server 2005 in SQL Server 2000 you normally use @@TRANCOUNT to detect any exception but now in SQL Server 2005 you can use try catch block.</p>
<p>In this snippet, there are 2 INSERT statement and 1 UPDATE statement. I&#8217;m expecting when there is any failure (e.g the UPDATE statement fails) then all the preceding INSERT/UPDATE/DELETE within the TRANSACTION block will be canceled</p>
<p>e.g</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;">BEGIN TRY
BEGIN TRANSACTION transABC
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> TABLEA <span style="color: #66cc66;">&#40;</span>col1<span style="color: #66cc66;">,</span>col2<span style="color: #66cc66;">,</span>col3<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'b'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'c'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> TABLEB <span style="color: #66cc66;">&#40;</span>col1<span style="color: #66cc66;">,</span>col2<span style="color: #66cc66;">,</span>col3<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'b'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'c'</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">UPDATE</span> TABLEA <span style="color: #993333; font-weight: bold;">SET</span> col2<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'abcde'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> col1 <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'a'</span>
&nbsp;
COMMIT TRANSACTION transABC
END TRY
&nbsp;
BEGIN CATCH
<span style="color: #993333; font-weight: bold;">IF</span> @@TRANCOUNT <span style="color: #66cc66;">&gt;</span> 0
ROLLBACK TRANSACTION transABC <span style="color: #808080; font-style: italic;">--RollBack in case of Error</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Raise an error with the details of the exception</span>
DECLARE @ErrMsg nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @ErrSeverity int
<span style="color: #993333; font-weight: bold;">SELECT</span> @ErrMsg <span style="color: #66cc66;">=</span> ERROR_MESSAGE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
@ErrSeverity <span style="color: #66cc66;">=</span> ERROR_SEVERITY<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
RAISERROR<span style="color: #66cc66;">&#40;</span>@ErrMsg<span style="color: #66cc66;">,</span> @ErrSeverity<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
END CATCH</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/trycatch-rollback-transaction-in-sql-server-2005/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Index in Table Variable</title>
		<link>http://fransiscuss.com/index-in-table-variable/</link>
		<comments>http://fransiscuss.com/index-in-table-variable/#comments</comments>
		<pubDate>Thu, 14 May 2009 07:32:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=212</guid>
		<description><![CDATA[One biggest advantage of table variable for me is you can&#8217;t put index on the columns. I was having a problem where doing self join in table variable (it has 1000+ records) is taking really really long time. One way I found to optimize it is to add primary key assuming every single record is [...]]]></description>
			<content:encoded><![CDATA[<p>One biggest advantage of table variable for me is you can&#8217;t put index on the columns. I was having a problem where doing self join in table variable (it has 1000+ records) is taking really really long time. One way I found to optimize it is to add <strong>primary key</strong> assuming every single record is unique on the table variable. The performance improvement is really significant by adding the primary key on table variable. And <strong>by changing table variable to temp table</strong>, it improves the performance more than by twice</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;">TABLE</span> <span style="color: #808080; font-style: italic;">#dtblJobsBillDates</span>
<span style="color: #66cc66;">&#40;</span>
		chProcessID			CHAR<span style="color: #66cc66;">&#40;</span>03<span style="color: #66cc66;">&#41;</span>		COLLATE database_default <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		intAssgnmtID		INTEGER									 <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		chBillOfficeCode	CHAR<span style="color: #66cc66;">&#40;</span>03<span style="color: #66cc66;">&#41;</span>		COLLATE database_default <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		chBillEntityCode	CHAR<span style="color: #66cc66;">&#40;</span>03<span style="color: #66cc66;">&#41;</span>		COLLATE database_default <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		chBillNo			CHAR<span style="color: #66cc66;">&#40;</span>08<span style="color: #66cc66;">&#41;</span>		COLLATE database_default <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>intAssgnmtID<span style="color: #66cc66;">,</span> chBillNo<span style="color: #66cc66;">,</span> chProcessID<span style="color: #66cc66;">,</span> chBillOfficeCode<span style="color: #66cc66;">,</span> chBillEntityCode<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/index-in-table-variable/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Disabling Time Synchronization of Virtual PC image</title>
		<link>http://fransiscuss.com/disabling-time-synchronization-of-virtual-pc-image/</link>
		<comments>http://fransiscuss.com/disabling-time-synchronization-of-virtual-pc-image/#comments</comments>
		<pubDate>Thu, 14 May 2009 07:21:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Others]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=208</guid>
		<description><![CDATA[I was having the problem where I tried to change the date and time on my VPC environment, it keeps resetting it back. To resolve the issue, you need to edit the *.VMC file and add the highlighted section below, resave the file and launch your VPC.
&#60;integration&#62;
    &#60;microsoft&#62;
        &#60;mouse&#62;
            &#60;allow type=&#8221;boolean&#8221;&#62;true&#60;/allow&#62;
        &#60;/mouse&#62;
        &#60;components&#62;
            &#60;host_time_sync&#62;
                &#60;enabled [...]]]></description>
			<content:encoded><![CDATA[<p>I was having the problem where I tried to change the date and time on my VPC environment, it keeps resetting it back. To resolve the issue, you need to edit the *.VMC file and add the highlighted section below, resave the file and launch your VPC.</p>
<p><span style="color: #000080; font-family: Courier New;">&lt;integration&gt;<br />
    &lt;microsoft&gt;<br />
        &lt;mouse&gt;<br />
            &lt;allow type=&#8221;boolean&#8221;&gt;true&lt;/allow&gt;<br />
        &lt;/mouse&gt;<br />
     <strong>   &lt;components&gt;<br />
            &lt;host_time_sync&gt;<br />
                &lt;enabled type=&#8221;boolean&#8221;&gt;false&lt;/enabled&gt;<br />
            &lt;/host_time_sync&gt;<br />
        &lt;/components&gt;</strong> </span></p>
]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/disabling-time-synchronization-of-virtual-pc-image/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
