<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;Ak4FSXw8fip7ImA9WhRUFUg.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375</id><updated>2012-01-26T11:21:58.276+05:00</updated><category term="Lambda Statements" /><category term="Post Links" /><category term="Crystal Reports" /><category term="Threading" /><category term="DataGridView" /><category term="User Defined Table Functions" /><category term="How To" /><category term="ADO.net" /><category term="Advanced Topics" /><category term="Post Updated" /><category term="Increase Performance" /><category term="Beginner Topics" /><category term="Javascript" /><category term="VS 2005" /><category term="Win Forms" /><category term="AJAX" /><category term="Data Paging" /><category term="SQL Server 2005" /><category term="Stored Procedures" /><category term="Intermediate Topics" /><category term="BackgroundWorker" /><category term="Transactions" /><category term="C#" /><category term="Typed DataSet" /><category term="Asynchronous Working" /><category term="Custom Controls" /><category term="Web Development" /><category term="SQL Server Functions" /><category term="VB.net" /><category term="HTML Tips/Tricks" /><category term="Urdu" /><category term="Useful Links" /><category term="Bitwise Operator" /><category term="FTP" /><category term="Control FAQs" /><category term="Useful Functions" /><category term="Data Binding" /><category term="VB 2005" /><category term=".net 2.0" /><category term="T-SQL" /><category term="Regular Expression" /><category term="About Blog" /><category term="Anonymous Methods" /><category term="ASP.net" /><title>Arsalan Tamiz</title><subtitle type="html">Always Learning</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://arsalantamiz.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>87</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/VisualBasicDevelopmentIssues" /><feedburner:info uri="visualbasicdevelopmentissues" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>VisualBasicDevelopmentIssues</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><entry gd:etag="W/&quot;DEcBSXs9eSp7ImA9WhRXEU8.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-3595771116188857532</id><published>2011-12-17T17:39:00.000+05:00</published><updated>2011-12-17T17:47:38.561+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-17T17:47:38.561+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Threading" /><category scheme="http://www.blogger.com/atom/ns#" term="Lambda Statements" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Advanced Topics" /><category scheme="http://www.blogger.com/atom/ns#" term="Anonymous Methods" /><title>Understanding Variable Scope in Anonymous Methods / Lambda Statements</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;p&gt;There are 2 goals of this article. 1) to understand the variable scope in anonymous methods and 2) to look at a real world scenario where we have used the anonymous methods effectively.&lt;/p&gt;
&lt;a name='more'&gt;&lt;/a&gt;
&lt;p&gt;Probably there will be several articles about this topic out there. Like:
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.codeproject.com/KB/cs/InsideAnonymousMethods.aspx"&gt;Inside C# 2.0 Anonymous Methods&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.codeproject.com/KB/cs/DelegatesOMy.aspx?msg=3590758"&gt;C# Delegates, Anonymous Methods, and Lambda Expressions – O My!&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/p&gt;
&lt;p&gt;To understand this article you should have:
&lt;ul&gt;
&lt;li&gt;Strong Knowledge of: Object Oriented Programming&lt;/li&gt;
&lt;li&gt;Little bit know how of: Anonymous Methods / Lambda Statements&lt;/li&gt;
&lt;/ul&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Description:&lt;/b&gt;&lt;/p&gt;
&lt;p class="notes"&gt;
Note that in this article I've used term &lt;b&gt;anonymous method&lt;/b&gt; which means both normal &lt;b&gt;anonymous methods&lt;/b&gt; or &lt;b&gt;lambda statements&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;While working with JavaScript we use anonymous methods as delegates to function most of the time, for example:&lt;/p&gt;
&lt;pre class="code"&gt;document.getElementById(&lt;span style="color: red;"&gt;'button1'&lt;/span&gt;).onclick = &lt;span style="color: blue;"&gt;function&lt;/span&gt;() {
    alert(&lt;span style="color: red;"&gt;'button 1 clicked'&lt;/span&gt;);
}&lt;/pre&gt;
&lt;p&gt;The thing which confused me a lot is the variable scoping. So let's try to learn this thing intuitively.&lt;/p&gt;
&lt;p&gt;Let's start with a code for example:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public partial class&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;Form1&lt;/span&gt; : &lt;span style="color: #5591bd;"&gt;Form&lt;/span&gt;
    {
        &lt;span style="color: blue;"&gt;public&lt;/span&gt; Form1()
        {
            InitializeComponent();
        }

        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; InitializeButton(&lt;span style="color: blue;"&gt;string&lt;/span&gt; sValue)
        {
            &lt;span style="color: blue;"&gt;string&lt;/span&gt; sToAdd = &lt;span style="color: red;"&gt;"****"&lt;/span&gt;;
            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.button1.Click += (sender, e) =&amp;gt;
            {
                &lt;span style="color: #5591bd;"&gt;MessageBox&lt;/span&gt;.Show(sToAdd + sValue + sToAdd);
            };
        }

        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; Form1_Load(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #5591bd;"&gt;EventArgs&lt;/span&gt; e)
        {
            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.InitializeButton(&lt;span style="color: red;"&gt;"Testing method"&lt;/span&gt;);
        }
    }&lt;/pre&gt;
&lt;p&gt;I used to think that this anonymous method gets created somewhere in memory without being a part of any class. But then how this anonymous method is accessing the variables that are out of its scope like &lt;span class="code"&gt;sValue&lt;/span&gt; and &lt;span class="code"&gt;sToAdd&lt;/span&gt; in above example? This was the question which bothered me.&lt;/p&gt;
&lt;p&gt;Later I tried to figure out if I don't have the anonymous method then how can I achieve this functionality i.e. how can we access the variables that are out of scope. Following solution came into my mind.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public partial class&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;Form1&lt;/span&gt; : &lt;span style="color: #5591bd;"&gt;Form&lt;/span&gt;
    {
        &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;tempclass&lt;/span&gt;
        {
            &lt;span style="color: blue;"&gt;public string&lt;/span&gt; sValue;
            &lt;span style="color: blue;"&gt;public string&lt;/span&gt; sToAdd;

            &lt;span style="color: blue;"&gt;public void&lt;/span&gt; tempmethod1(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #5591bd;"&gt;EventArgs&lt;/span&gt; e)
            {
                &lt;span style="color: #5591bd;"&gt;MessageBox&lt;/span&gt;.Show(sToAdd + sValue + sToAdd);
            }
        }

        &lt;span style="color: blue;"&gt;public&lt;/span&gt; Form1()
        {
            InitializeComponent();
        }

        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; InitializeButton(&lt;span style="color: blue;"&gt;string&lt;/span&gt; sValue)
        {
            &lt;span style="color: blue;"&gt;string&lt;/span&gt; sToAdd = &lt;span style="color: red;"&gt;"****"&lt;/span&gt;;
            &lt;span style="color: #5591bd;"&gt;tempclass&lt;/span&gt; t = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;tempclass&lt;/span&gt;();
            t.sValue = sValue;
            t.sToAdd = sToAdd;
            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.button1.Click += t.tempmethod1;
        }

        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; Form1_Load(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #5591bd;"&gt;EventArgs&lt;/span&gt; e)
        {
            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.InitializeButton(&lt;span style="color: red;"&gt;"Testing method"&lt;/span&gt;);
        }
    }&lt;/pre&gt;
&lt;p&gt;What I have done is just encapsulated the anonymous method and its variables in a temporary class. And guess what, compiler actually does the same thing when we use anonymous method and local variables.&lt;/p&gt;
&lt;p&gt;Another workaround could be to move the method's local variables to class level for example:&lt;/p&gt;
&lt;pre class="code"&gt;    &lt;span style="color: blue;"&gt;public partial class&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;Form1&lt;/span&gt; : &lt;span style="color: #5591bd;"&gt;Form&lt;/span&gt;
    {        
        &lt;span style="color: blue;"&gt;public&lt;/span&gt; Form1()
        {
            InitializeComponent();
        }

        &lt;span style="color: blue;"&gt;private string&lt;/span&gt; sToAdd;
        &lt;span style="color: blue;"&gt;private string&lt;/span&gt; sValue;

        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; InitializeButton(&lt;span style="color: blue;"&gt;string&lt;/span&gt; sValue)
        {
            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.sToAdd = &lt;span style="color: red;"&gt;"****"&lt;/span&gt;;
            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.sValue = sValue;
            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.button1.Click += this.button1click;
        }

        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; button1click(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #5591bd;"&gt;EventArgs&lt;/span&gt; e)
        {
            &lt;span style="color: #5591bd;"&gt;MessageBox&lt;/span&gt;.Show(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.sToAdd + &lt;span style="color: blue;"&gt;this&lt;/span&gt;.sValue + &lt;span style="color: blue;"&gt;this&lt;/span&gt;.sToAdd);
        }

        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; Form1_Load(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #5591bd;"&gt;EventArgs&lt;/span&gt; e)
        {
            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.InitializeButton(&lt;span style="color: red;"&gt;"Testing method"&lt;/span&gt;);
        }
    }&lt;/pre&gt;
&lt;p&gt;Above example does the same thing, but we are actually extracting the inner variables of method to outer class which ultimately will make our class a mess and managing such type of classes is horribly hard.&lt;/p&gt;
&lt;p&gt;As you can see the anonymous method makes our code simplified and easy to manage. We can easily encapsulate our method's local variables and we don't need to define them on class level.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;A Real World Scenario:&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The given example is NOT a real world scenario and does NOT actually show us the benefit of using anonymous method. So now I'll share a real world scenario which I face.&lt;/p&gt;
&lt;p&gt;I was actually creating a TCP Communication Layer, which later can be used in network projects like if you are creating a chat server, multiplayer game, etc.&lt;/p&gt;
&lt;p&gt;In short if you are creating a network application, you typically have a server which listens for incoming connections and also you have one or more clients which connect to server. When a connection is established between server and client, client then sends requests to server and server responds back for that particular request. Normally this communication between clients and server is carried out via events, which means whenever client/server receives data, an event is triggered that Data has been received.&lt;/p&gt;
&lt;pre class="code"&gt;    &lt;span style="color: blue;"&gt;public class&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;Server&lt;/span&gt;
    {
        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; PacketReceived(&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt; pkt)
        {
            &lt;span style="color: blue;"&gt;switch&lt;/span&gt; (pkt.Type)
            {
                &lt;span style="color: blue;"&gt;case&lt;/span&gt; &lt;span style="color: red;"&gt;"Login"&lt;/span&gt;:
                    &lt;span style="color: blue;"&gt;if&lt;/span&gt; (isValidLogin(pkt))
                        SendPacket(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;LoginSuccessPacket&lt;/span&gt;(pkt.PacketID));
                    &lt;span style="color: blue;"&gt;else&lt;/span&gt;
                        SendPacket(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;LoginFailedPacket&lt;/span&gt;(pkt.PacketID));
                    &lt;span style="color: blue;"&gt;break&lt;/span&gt;;
            }
        }
        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; SendPacket(&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt; pkt)
        {
            &lt;span style="color: green;"&gt;// code to send packet&lt;/span&gt;
        }
    }

    &lt;span style="color: blue;"&gt;public class&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;Client&lt;/span&gt;
    {
        &lt;span style="color: green;"&gt;// this login method just sends login packet to server and does NOT
        // return any login success or failure message because we can only
        // determine the success or failure once we received the response
        // packet back from server&lt;/span&gt;
        &lt;span style="color: blue;"&gt;public void&lt;/span&gt; Login(&lt;span style="color: blue;"&gt;string&lt;/span&gt; ID, &lt;span style="color: blue;"&gt;string&lt;/span&gt; pwd)
        {
            SendPacket(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;LoginPacket&lt;/span&gt;(ID, pwd));
        }

        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; PacketReceived(&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt; pkt)
        {
            &lt;span style="color: blue;"&gt;switch&lt;/span&gt; (pkt.Type)
            {
                &lt;span style="color: blue;"&gt;case&lt;/span&gt; &lt;span style="color: red;"&gt;"LoginSuccessPaket"&lt;/span&gt;:
                    &lt;span style="color: green;"&gt;// successfully logged in&lt;/span&gt;
                    &lt;span style="color: blue;"&gt;break&lt;/span&gt;;
                &lt;span style="color: blue;"&gt;case&lt;/span&gt; &lt;span style="color: red;"&gt;"LoginFailedPacket"&lt;/span&gt;:
                    &lt;span style="color: green;"&gt;// failed&lt;/span&gt;
                    &lt;span style="color: blue;"&gt;break&lt;/span&gt;;
            }
        }

        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; SendPacket(&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt; pkt)
        {
            pkt.PacketID = GetNewPacketID();
            &lt;span style="color: green;"&gt;// code to send packet&lt;/span&gt;
        }
    }&lt;/pre&gt;
&lt;p&gt;Suppose we have above 2 classes. In both classes &lt;span class="code"&gt;PacketReceived()&lt;/span&gt; is executed whenever some data is received and similarly &lt;span class="code"&gt;SendPacket()&lt;/span&gt; method is used to send packet to other end.&lt;/p&gt;
&lt;p&gt;Now we have used &lt;span class="code"&gt;Client&lt;/span&gt; Class in our Client application (a Windows Forms Application) and have created a Login Form which uses that class to send Login Request to server.&lt;/p&gt;
&lt;p&gt;Whenever we hit "Login" button &lt;span class="code"&gt;Login()&lt;/span&gt; method gets called and login packet is sent to server. At this point the main UI thread sends bytes and immediately returns back. Now UI thread is free and you can again hit the Login button or can even close this window.  We have to write some logic here to disable the Login button and to prevent the Form to be closed.&lt;/p&gt;
&lt;p&gt;What I want is that my UI thread should freeze on the point where I have called the &lt;span class="code"&gt;Login()&lt;/span&gt; method of &lt;span class="code"&gt;Client&lt;/span&gt; class. It should either through the exception or should send me the success code. In simple words I want the thread to be blocked until
&lt;ul&gt;
&lt;li&gt;I don't receive a response from server either success or failure&lt;/li&gt;
&lt;li&gt;Or Timeout occurs in case server didn't respond in time&lt;/li&gt;
&lt;/ul&gt;&lt;/p&gt;
&lt;p&gt;This is because I don't want my login process to be spread across events. I just want my &lt;span class="code"&gt;Login()&lt;/span&gt; method to be simple and concrete and it should return either the error or success.&lt;/p&gt;
&lt;p&gt;Modified version of Client Class&lt;/p&gt;
&lt;p class="notes"&gt;
Note that in order to understand this solution you should have a strong grip on &lt;b&gt;Threading&lt;/b&gt; and &lt;b&gt;Locking&lt;/b&gt;&lt;/p&gt;
&lt;pre class="code"&gt;    &lt;span style="color: blue;"&gt;public class&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;Client&lt;/span&gt;
    {
        &lt;span style="color: blue;"&gt;private int&lt;/span&gt; GetNewPacketID()
        {
            &lt;span style="color: green;"&gt;// code to generate new unique Packet ID&lt;/span&gt;
        }

        &lt;span style="color: green;"&gt;// now login method is concrete and it will return either error or success&lt;/span&gt;
        &lt;span style="color: blue;"&gt;public void&lt;/span&gt; Login(&lt;span style="color: blue;"&gt;string&lt;/span&gt; ID, &lt;span style="color: blue;"&gt;string&lt;/span&gt; pwd)
        {
            &lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt; pktReturned;
            &lt;span style="color: green;"&gt;// here thread will be blocked until the response from server is NOT received or timeout expired&lt;/span&gt;
            pktReturned = SendPacketGetResponse(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;LoginPacket&lt;/span&gt;(ID, pwd), 10000); &lt;span style="color: green;"&gt;// 10000 milliseconds = 10 seconds
            // code to perform operation on pktReturned&lt;/span&gt;
        }

        &lt;span style="color: green;"&gt;// this is actually an event handler which is executed
        // if a packet is received from server&lt;/span&gt;
        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; PacketReceived(&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt; pkt)
        {
            &lt;span style="color: #5591bd;"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt;&amp;gt; callback = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;

            &lt;span style="color: blue;"&gt;lock&lt;/span&gt; (&lt;span style="color: blue;"&gt;this&lt;/span&gt;.CallBackMethods)
            {
                &lt;span style="color: green;"&gt;// trying to extract out the callback if is associated with this packet id&lt;/span&gt;
                &lt;span style="color: blue;"&gt;if&lt;/span&gt; (&lt;span style="color: blue;"&gt;this&lt;/span&gt;.CallBackMethods.TryGetValue(pkt.PacketID, &lt;span style="color: blue;"&gt;out&lt;/span&gt; callback))
                    &lt;span style="color: green;"&gt;// if found then we will remove it from dictionary&lt;/span&gt;
                    &lt;span style="color: blue;"&gt;this&lt;/span&gt;.CallBackMethods.Remove(pkt.PacketID);
            }

            &lt;span style="color: green;"&gt;// if this is the response of some packet&lt;/span&gt;
            &lt;span style="color: blue;"&gt;if&lt;/span&gt; (callback != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            {
                &lt;span style="color: green;"&gt;// call the callback&lt;/span&gt;
                callback(pkt);
            }
            &lt;span style="color: blue;"&gt;else&lt;/span&gt;
            {
                &lt;span style="color: green;"&gt;// handle packet normally&lt;/span&gt;
            }
        }

        &lt;span style="color: green;"&gt;// this dictionary will hold the callback methods associated
        // with packet id. this dictionary will only be used
        // if we want to call our custom delegate on arrival of certain
        // packet id&lt;/span&gt;
        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;, &lt;span style="color: #5591bd;"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt;&amp;gt;&amp;gt; CallBackMethods = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;, &lt;span style="color: #5591bd;"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt;&amp;gt;&amp;gt;();

        &lt;span style="color: green;"&gt;// sends packet to server and does NOT wait for server response
        // immediately returns to the caller&lt;/span&gt;
        &lt;span style="color: blue;"&gt;private void&lt;/span&gt; SendPacket(&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt; pkt, &lt;span style="color: #5591bd;"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt;&amp;gt; callback)
        {
            &lt;span style="color: green;"&gt;// attach a new unique packet id with the given packet&lt;/span&gt;
            pkt.PacketID = GetNewPacketID();

            &lt;span style="color: green;"&gt;// if callback method is provided then it means we want
            // to execute our callback method whenever response of
            // the given packet is received&lt;/span&gt;
            &lt;span style="color: blue;"&gt;if&lt;/span&gt; (callback != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            {
                &lt;span style="color: green;"&gt;// add in the dictionary&lt;/span&gt;
                &lt;span style="color: blue;"&gt;lock&lt;/span&gt; (&lt;span style="color: blue;"&gt;this&lt;/span&gt;.CallBackMethods)
                {
                    &lt;span style="color: blue;"&gt;this&lt;/span&gt;.CallBackMethods.Add(pkt.PacketID, callback);
                }
            }
            
            &lt;span style="color: green;"&gt;// code to send packet&lt;/span&gt;
        }

        &lt;span style="color: green;"&gt;// this method will block the calling thread until
        // 1) response is received from server
        // 2) or timeout expired&lt;/span&gt;
        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt; SendPacketGetResponse(&lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt; pktToSend, &lt;span style="color: blue;"&gt;int&lt;/span&gt; iTimeOut)
        {
            &lt;span style="color: #5591bd;"&gt;PacketClass&lt;/span&gt; pktReturn = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;

            &lt;span style="color: blue;"&gt;using&lt;/span&gt; (System.Threading.&lt;span style="color: #5591bd;"&gt;AutoResetEvent&lt;/span&gt; are = &lt;span style="color: blue;"&gt;new&lt;/span&gt; System.Threading.&lt;span style="color: #5591bd;"&gt;AutoResetEvent&lt;/span&gt;(&lt;span style="color: blue;"&gt;false&lt;/span&gt;))
            {
                &lt;span style="color: green;"&gt;// send packet to server&lt;/span&gt;
                &lt;span style="color: blue;"&gt;this&lt;/span&gt;.SendPacket(pktToSend, (pktReceived) =&amp;gt;
                    &lt;span style="color: green;"&gt;// anonymous method:&lt;/span&gt;
                {
                    &lt;span style="color: green;"&gt;// pktReturn variable has been declared in the outer method i.e.
                    // SendPacketGetResponse&lt;/span&gt;
                    pktReturn = pktReceived;
                    are.Set();  &lt;span style="color: green;"&gt;// Release the sendpacketgetresponse() method thread&lt;/span&gt;
                });

                &lt;span style="color: green;"&gt;// here the calling thread will be blocked
                // and once the response packet is received in our PacketReceived() method
                // it will call our anonymous method (written above) and our anonymous
                // method will call are.Set() which will resume this thread&lt;/span&gt;
                &lt;span style="color: blue;"&gt;if&lt;/span&gt; (!are.WaitOne(iTimeOut))
                {
                    &lt;span style="color: green;"&gt;// since timeout has been expired therefore
                    // we are now removing the callback method from our dictionary
                    // even if server sends a response we are NOT interested &lt;/span&gt;
                    &lt;span style="color: blue;"&gt;lock&lt;/span&gt; (&lt;span style="color: blue;"&gt;this&lt;/span&gt;.CallBackMethods)
                    {
                        &lt;span style="color: blue;"&gt;this&lt;/span&gt;.CallBackMethods.Remove(pkt.PacketID);
                    }
                    &lt;span style="color: blue;"&gt;throw new&lt;/span&gt; &lt;span style="color: #5591bd;"&gt;Exception&lt;/span&gt;(&lt;span style="color: red;"&gt;"Timeout Expired"&lt;/span&gt;);
                }
            }
            &lt;span style="color: blue;"&gt;return&lt;/span&gt; pktReturn;
        }
    }&lt;/pre&gt;
&lt;p&gt;The core of this topic is the &lt;span class="code"&gt;SendPacketGetResponse()&lt;/span&gt; method in above example.&lt;/p&gt;
&lt;p&gt;The main goal which I've achieved here is that I have performed asynchronous operation synchronously. Plus the the returning variable is declared within the &lt;span class="code"&gt;SendPacketGetResponse()&lt;/span&gt; method while the actual packet is received in &lt;span class="code"&gt;PacketReceived()&lt;/span&gt; method but using Anonymous Method we encapsulated our whole process in a single method.&lt;/p&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-3595771116188857532?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/-wVa3I90PXhOvMiiue0Cjyi8frI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-wVa3I90PXhOvMiiue0Cjyi8frI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/-wVa3I90PXhOvMiiue0Cjyi8frI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-wVa3I90PXhOvMiiue0Cjyi8frI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/5Cr-TVNRI9E" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/3595771116188857532/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=3595771116188857532" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/3595771116188857532?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/3595771116188857532?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/5Cr-TVNRI9E/understanding-variable-scope-in.html" title="Understanding Variable Scope in Anonymous Methods / Lambda Statements" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2011/12/understanding-variable-scope-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQGRXcyfip7ImA9WxBWEEU.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-8135496361729450567</id><published>2010-01-28T10:35:00.004+05:00</published><updated>2010-02-02T09:52:04.996+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-02T09:52:04.996+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Win Forms" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGridView" /><title>DataGridView control change Selection on Mouse Up</title><content type="html">&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;DataGridView Control&lt;/li&gt;&lt;li&gt;Windows Forms&lt;/li&gt;&lt;/ul&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;Recently, one of our friend "Luc" asked me,&lt;br /&gt;&lt;br /&gt;&lt;div style="padding:8px;border:1px solid #AAAAAA;background-color:#EEEEEE;"&gt;Is there an "easy" way to change the selection behavior in a datagridview so that a click on a selected row does not toggle the selection as long as the mouse button is not released, without resorting to a user-defined control with inheritence and method overrides ?&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;So I was about to paste the whole code in comments but that was looking a mess. So I decided to publish a new post, after a very long time :).&lt;br /&gt;&lt;br /&gt;The solution "is" simple. We just need to look what is the event sequence. So whenever we press mouse button on a cell or row of DataGridView control, events trigger in this way,&lt;ol&gt;&lt;li&gt;MouseDown&lt;/li&gt;&lt;li&gt;SelectionChanged&lt;/li&gt;&lt;li&gt;MouseUp&lt;/li&gt;&lt;/ol&gt;When MouseDown event occurs Selection does NOT change. So in this event handler we can save the current row in some variable and then in SelectionChanged event handler we can select previously saved row, in this way user will experience that selection never changed.&lt;br /&gt;&lt;br /&gt;And here is the code,&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;font color=blue&gt;Public&lt;/font&gt; &lt;font color=blue&gt;Class&lt;/font&gt; Form1&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; Form1_Load(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; System.&lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.EventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; &lt;font color=blue&gt;MyBase&lt;/font&gt;.Load&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.Columns.Add(&lt;font color=darkred&gt;&amp;quot;Column1&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;Column1&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.Columns.Add(&lt;font color=darkred&gt;&amp;quot;Column2&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;Column2&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.Columns.Add(&lt;font color=darkred&gt;&amp;quot;Column3&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;Column3&amp;quot;&lt;/font&gt;)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.Rows.Add(&lt;font color=darkred&gt;&amp;quot;laksjdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;alksjfsdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;aksldjf&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.Rows.Add(&lt;font color=darkred&gt;&amp;quot;laksjdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;alksjfsdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;aksldjf&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.Rows.Add(&lt;font color=darkred&gt;&amp;quot;laksjdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;alksjfsdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;aksldjf&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.Rows.Add(&lt;font color=darkred&gt;&amp;quot;laksjdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;alksjfsdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;aksldjf&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.Rows.Add(&lt;font color=darkred&gt;&amp;quot;laksjdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;alksjfsdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;aksldjf&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.Rows.Add(&lt;font color=darkred&gt;&amp;quot;laksjdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;alksjfsdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;aksldjf&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.Rows.Add(&lt;font color=darkred&gt;&amp;quot;laksjdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;alksjfsdf&amp;quot;&lt;/font&gt;, &lt;font color=darkred&gt;&amp;quot;aksldjf&amp;quot;&lt;/font&gt;)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' ***************************************&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' IMPORTANT: MultiSelect property of DataGridView control should be False&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;The result maybe same but if we set it to true then you &lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;may experience the flickering&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' ***************************************&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.MultiSelect = &lt;font color=blue&gt;False&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; _MouseDown &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Boolean&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; _PreviousSelectedCell &lt;font color=blue&gt;As&lt;/font&gt; DataGridViewCell&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; _NewSelectedCell &lt;font color=blue&gt;As&lt;/font&gt; DataGridViewCell&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; DataGridView1_MouseDown(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.Windows.Forms.MouseEventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; DataGridView1.MouseDown&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' if left button is clicked&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; e.Button = Windows.Forms.MouseButtons.Left &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;._MouseDown = &lt;font color=blue&gt;True&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' we will mark a flag that mouse is down&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' and will note the row before the selection changed&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;._PreviousSelectedCell = &lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.CurrentCell&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; DataGridView1_SelectionChanged(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.EventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; DataGridView1.SelectionChanged&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; &lt;font color=blue&gt;Me&lt;/font&gt;._MouseDown &lt;font color=blue&gt;Then&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=green&gt;' if this event is triggerd after the mouse down&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' we will first note the new row on which mouse was clicked&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;._NewSelectedCell = &lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.CurrentCell&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' then change the selection back to the one before mouse was down&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.CurrentCell = &lt;font color=blue&gt;Me&lt;/font&gt;._PreviousSelectedCell&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; DataGridView1_MouseUp(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.Windows.Forms.MouseEventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; DataGridView1.MouseUp&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; &lt;font color=blue&gt;Me&lt;/font&gt;._MouseDown &lt;font color=blue&gt;Then&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=green&gt;' if this event is triggered after the mouse down&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' we will set the selection to the new row which was clicked&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.DataGridView1.CurrentCell = &lt;font color=blue&gt;Me&lt;/font&gt;._NewSelectedCell&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;._MouseDown = &lt;font color=blue&gt;False&lt;/font&gt;&amp;nbsp;&amp;nbsp; &lt;font color=green&gt;' reset the flag&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Class&lt;/font&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-8135496361729450567?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/My7CqpsF5-PRydwFc7ZY6PgqX9w/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/My7CqpsF5-PRydwFc7ZY6PgqX9w/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/My7CqpsF5-PRydwFc7ZY6PgqX9w/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/My7CqpsF5-PRydwFc7ZY6PgqX9w/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/BA_YOXBl5ts" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/8135496361729450567/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=8135496361729450567" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/8135496361729450567?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/8135496361729450567?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/BA_YOXBl5ts/datagridview-control-change-selection.html" title="DataGridView control change Selection on Mouse Up" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>1</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2010/01/datagridview-control-change-selection.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkEEQ34-fip7ImA9WxNQF0o.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-8523234755262582924</id><published>2009-09-24T12:24:00.003+06:00</published><updated>2009-09-24T12:43:22.056+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-24T12:43:22.056+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Useful Links" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005" /><title>Useful Links for SQL Server</title><content type="html">Stuck while manually inserting values in an Identity column? Here is what you need to do &lt;a href="http://www.sqlteam.com/article/how-to-insert-values-into-an-identity-column-in-sql-server"&gt;How to Insert Values into an Identity Column in SQL Server&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Here is a great topic for beginners to learn about the Execution Plans in SQL Server &lt;a href="http://www.sqlservercentral.com/articles/Administration/executionplans/1345/"&gt;Beginners topic for understanding Execution Plans in SQL Server&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Is your SQL Server eating your CPU too much? Don't know which Query or Stored Procedure is behind this? A must have tool for SQL Server &lt;a href="http://www.sql-server-performance.com/articles/per/bm_performance_dashboard_2005_p1.aspx"&gt;Making the Most Out of the SQL Server 2005 Performance Dashboard&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Are you an advanced SQL Server 2005 Programmer? Do you know about CROSS APPLY? &lt;a href="http://www.sqlteam.com/article/using-cross-apply-in-sql-server-2005"&gt;Using CROSS APPLY in SQL Server 2005&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-8523234755262582924?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/m_LAdFziasvOM7UXGj0wO-olc6g/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/m_LAdFziasvOM7UXGj0wO-olc6g/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/m_LAdFziasvOM7UXGj0wO-olc6g/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/m_LAdFziasvOM7UXGj0wO-olc6g/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/0qzJ1uUppX0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/8523234755262582924/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=8523234755262582924" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/8523234755262582924?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/8523234755262582924?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/0qzJ1uUppX0/useful-links-for-sql-server.html" title="Useful Links for SQL Server" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2009/09/useful-links-for-sql-server.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUcFRHc7cSp7ImA9WxJSFkQ.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-6697671469924420785</id><published>2009-05-07T16:15:00.003+06:00</published><updated>2009-05-07T16:43:35.909+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-05-07T16:43:35.909+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Web Development" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005" /><title>Importing IIS Web Log into SQL Server 2005</title><content type="html">Today I was trying to import the IIS log of my Web Server into SQL Server 2005 Database (for further analyses). So first I started googling about it and found this link,&lt;br /&gt;&lt;br /&gt;&lt;a href="http://support.microsoft.com/kb/296085"&gt;How To Use SQL Server to Analyze Web Logs&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Very useful article BUT this one is old maybe because the table have different schema. By the way I have IIS 6.0 on my web server. So I did some workarounds and successfully imported the Log. Now I am posting here in case someone might be trying to do the same thing.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Step #1: Create a table in Database&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:blue"&gt;CREATE TABLE&lt;/span&gt; [dbo].[IISLog](&lt;br /&gt; [date] [datetime] &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [time] [datetime] &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [site-name] [varchar](255) &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [s-computername] [varchar](255) &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [s-ip] [varchar](50) &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [cs-uri-stem] [varchar](255) &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [cs-uri-query] [varchar](2048) &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [c-ip] [varchar](50) &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [cs(User-Agent)] [varchar](2048) &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [cs(Cookie)] [varchar](2048) &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [cs(Referer)] [varchar](2048) &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [sc-status] [int] &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [sc-bytes] [int] &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [cs-bytes] [int] &lt;span style="color:gray"&gt;NULL&lt;/span&gt;,&lt;br /&gt; [time-taken] [int] &lt;span style="color:gray"&gt;NULL&lt;/span&gt;&lt;br /&gt;)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Step #2: Prepare the Log file (since it contains some description lines on top)&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;So this is a tricky step. As the Log file contains some description lines on top starting with "#" sign. Therefore SQL Server will NOT be able to import it. One more thing is that these log files can be large (or very large). The Log file I had was  of size aprox. 216 MB. So obviously we cannot open it in NotePad etc.&lt;br /&gt;&lt;br /&gt;The same article provides a small utility which removes the line, but I think there is bug in this utility, cause it is limiting file upto 43 MB. So I decided to write my own version.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://cid-c23746227866488d.skydrive.live.com/self.aspx/Public/PrepIISLogFileForImport.zip"&gt;PrepIISLogFileForImport.zip&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;As I started to create this application for IIS Log import but then it ends up with a generic utility. Which actually displays the text file content and have an option to skip number of lines from start. Therefore we can use it as,&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:10px;font-size:80%;background-color:black;color:white;"&gt;C:\&gt;PrepIISLogFileForImport C:\LogFile.Log skip=4 &gt;newlogfile.txt&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Step #3: Bulk import the Log file into SQL Server Table&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Hence the final step is to import the log file in the same table. Which can be done by,&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:blue"&gt;BULK INSERT&lt;/span&gt; [dbo].[IISLog] &lt;span style="color:blue"&gt;FROM&lt;/span&gt; &lt;span style="color:red"&gt;'C:\newlogfile.txt'&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue"&gt;WITH&lt;/span&gt; (&lt;br /&gt;    &lt;span style="color:blue"&gt;FIELDTERMINATOR&lt;/span&gt; = &lt;span style="color:red"&gt;' '&lt;/span&gt;,&lt;br /&gt;    &lt;span style="color:blue"&gt;ROWTERMINATOR&lt;/span&gt; = &lt;span style="color:red"&gt;'\n'&lt;/span&gt;&lt;br /&gt;)&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-6697671469924420785?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/9QHWw7eOGv8_UKqqRF_u1EqxiFo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9QHWw7eOGv8_UKqqRF_u1EqxiFo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/9QHWw7eOGv8_UKqqRF_u1EqxiFo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9QHWw7eOGv8_UKqqRF_u1EqxiFo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/fME-7cYYGQo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/6697671469924420785/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=6697671469924420785" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/6697671469924420785?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/6697671469924420785?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/fME-7cYYGQo/importing-iis-web-log-into-sql-server.html" title="Importing IIS Web Log into SQL Server 2005" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2009/05/importing-iis-web-log-into-sql-server.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEANRnc4eSp7ImA9WxVVFk8.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-3948590518106362624</id><published>2009-03-09T16:24:00.009+05:00</published><updated>2009-03-09T23:19:57.931+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-03-09T23:19:57.931+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Useful Functions" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server Functions" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005" /><title>SQL Server Date Time Useful Functions</title><content type="html">&lt;a id="sqldatetimetop"&gt;&lt;/a&gt;Here are some SQL Server Date Time Useful functions.&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;&lt;a href="#datecreate"&gt;DateCreate&lt;/a&gt;&lt;/strong&gt; - Creates Date through Year, Month and Day&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;a href="#datetostrshort"&gt;DateToStrShort&lt;/a&gt;&lt;/strong&gt; - Accepts Date and returns as '1-Jan-2009'&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;a href="#dategetdaysinmonth"&gt;DateGetDaysInMonth&lt;/a&gt;&lt;/strong&gt; - Accepts Month and Year and returns number of days in month&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;a href="#dategettimeonly"&gt;DateGetTimeOnly&lt;/a&gt;&lt;/strong&gt; - Accepts date and returns time as '10:33 PM'&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;a href="#dategetdateonly"&gt;DateGetDateOnly&lt;/a&gt;&lt;/strong&gt; - Accepts Date and convert it into Date only i.e. removes the Time Part&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;a href="#dategetmonthfirstdate"&gt;DateGetMonthFirstDate&lt;/a&gt;&lt;/strong&gt; - Gets the first date of Month&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;a href="#dategetmonthlastdate"&gt;DateGetMonthLastDate&lt;/a&gt;&lt;/strong&gt; - Gets the last date of Month&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;a href="#dategetmonthname"&gt;DateGetMonthName&lt;/a&gt;&lt;/strong&gt; - Gets the Month Name&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Review the functions below or &lt;a href="http://cid-c23746227866488d.skydrive.live.com/self.aspx/Public/UsefulDateTimeFunctions.sql"&gt;download&lt;/a&gt; the SQL file.&lt;br /&gt;&lt;br /&gt;&lt;a id="datecreate"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;font color="green"&gt;-- =============================================&lt;br /&gt;-- Author:        Arsalan Tamiz&lt;br /&gt;-- Description:   This function accepts Year, Month and Day&lt;br /&gt;--                and returns Date&lt;br /&gt;-- =============================================&lt;/font&gt;&lt;br /&gt;&lt;font color="blue"&gt;CREATE FUNCTION&lt;/font&gt; [DateCreate]&lt;br /&gt;(&lt;br /&gt;    @Year &lt;font color="blue"&gt;int&lt;/font&gt;,&lt;br /&gt;    @Month &lt;font color="blue"&gt;int&lt;/font&gt;,&lt;br /&gt;    @Day &lt;font color="blue"&gt;int&lt;/font&gt;&lt;br /&gt;)&lt;br /&gt;&lt;font color="blue"&gt;RETURNS datetime&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;/font&gt;&lt;br /&gt;    &lt;font color="green"&gt;-- Declare the return variable here&lt;/font&gt;&lt;br /&gt;    &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @Result &lt;font color="blue"&gt;datetime&lt;/font&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;font color="green"&gt;-- Add the T-SQL statements to compute the return value here&lt;/font&gt;&lt;br /&gt;    &lt;font color="blue"&gt;SET&lt;/font&gt; @Result = &lt;font color="magenta"&gt;CAST&lt;/font&gt;    (&lt;br /&gt;                            &lt;font color="magenta"&gt;CAST&lt;/font&gt;(@Year &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(10)) + &lt;font color="red"&gt;'-'&lt;/font&gt; +&lt;br /&gt;                            &lt;font color="magenta"&gt;CAST&lt;/font&gt;(@Month &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(2)) + &lt;font color="red"&gt;'-'&lt;/font&gt; + &lt;br /&gt;                            &lt;font color="magenta"&gt;CAST&lt;/font&gt;(@Day &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(2))&lt;br /&gt;                            &lt;font color="blue"&gt;AS datetime&lt;/font&gt;&lt;br /&gt;                          );&lt;br /&gt;&lt;br /&gt;    &lt;font color="green"&gt;-- Return the result of the function&lt;/font&gt;&lt;br /&gt;    &lt;font color="blue"&gt;RETURN&lt;/font&gt; @Result;&lt;br /&gt;&lt;font color="blue"&gt;END&lt;/font&gt;&lt;/pre&gt;&lt;div style="text-align:right;"&gt;&lt;a href="#sqldatetimetop"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;a id="datetostrshort"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;font color="green"&gt;-- =============================================&lt;br /&gt;-- Author:        Arsalan Tamiz&lt;br /&gt;-- Description:   This function converts the given&lt;br /&gt;--                date in to string. Useful function&lt;br /&gt;--                for displaying date as '10-Jan-2009' for example&lt;br /&gt;-- =============================================&lt;/font&gt;&lt;br /&gt;&lt;font color="blue"&gt;CREATE FUNCTION&lt;/font&gt; [DateToStrShort] &lt;br /&gt;(&lt;br /&gt;    &lt;font color="green"&gt;-- Add the parameters for the function here&lt;/font&gt;&lt;br /&gt;    @DateToConvert &lt;font color="blue"&gt;datetime&lt;/font&gt;&lt;br /&gt;)&lt;br /&gt;&lt;font color="blue"&gt;RETURNS varchar&lt;/font&gt;(50)&lt;br /&gt;&lt;font color="blue"&gt;AS&lt;br /&gt;BEGIN&lt;/font&gt;&lt;br /&gt;    &lt;font color="green"&gt;-- Declare the return variable here&lt;/font&gt;&lt;br /&gt;    &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @Result &lt;font color="blue"&gt;varchar&lt;/font&gt;(50)&lt;br /&gt;&lt;br /&gt;    &lt;font color="blue"&gt;SET&lt;/font&gt; @Result = &lt;font color="magenta"&gt;CAST&lt;/font&gt;(&lt;font color="magenta"&gt;Day&lt;/font&gt;(@DateToConvert) &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(50)) + &lt;font color="red"&gt;'-'&lt;/font&gt; + &lt;br /&gt;                    &lt;font color="gray"&gt;Left&lt;/font&gt;(&lt;font color="magenta"&gt;DateName&lt;/font&gt;(m, @DateToConvert), 3) + &lt;font color="red"&gt;'-'&lt;/font&gt; + &lt;br /&gt;                    &lt;font color="magenta"&gt;CAST&lt;/font&gt;(&lt;font color="magenta"&gt;Year&lt;/font&gt;(@DateToConvert) &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(50))&lt;br /&gt;&lt;br /&gt;    &lt;font color="green"&gt;-- Return the result of the function&lt;/font&gt;&lt;br /&gt;    &lt;font color="blue"&gt;RETURN&lt;/font&gt; @Result;&lt;br /&gt;&lt;font color="blue"&gt;END&lt;/font&gt;&lt;/pre&gt;&lt;div style="text-align:right;"&gt;&lt;a href="#sqldatetimetop"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;a id="dategetdaysinmonth"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;font color="green"&gt;-- =============================================&lt;br /&gt;-- Author:        unknown (I copied it from internet)&lt;br /&gt;-- Description:   Returns the Days in a Month&lt;br /&gt;--                parameters = Year and Month&lt;br /&gt;-- =============================================&lt;/font&gt;&lt;br /&gt;&lt;font color="blue"&gt;CREATE FUNCTION&lt;/font&gt; [DateGetDaysInMonth] (@Year &lt;font color="blue"&gt;int&lt;/font&gt;, @Month &lt;font color="blue"&gt;int&lt;/font&gt;)&lt;br /&gt;&lt;font color="blue"&gt;RETURNS INT&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;    &lt;font color="blue"&gt;RETURN CASE WHEN&lt;/font&gt; @Month &lt;font color="gray"&gt;IN&lt;/font&gt; (1, 3, 5, 7, 8, 10, 12) &lt;font color="blue"&gt;THEN&lt;/font&gt; 31&lt;br /&gt;                &lt;font color="blue"&gt;WHEN&lt;/font&gt; @Month &lt;font color="gray"&gt;IN&lt;/font&gt; (4, 6, 9, 11) &lt;font color="blue"&gt;THEN&lt;/font&gt; 30&lt;br /&gt;                &lt;font color="blue"&gt;ELSE CASE WHEN&lt;/font&gt; (@Year % 4    = 0 &lt;font color="gray"&gt;AND&lt;/font&gt;&lt;br /&gt;                                @Year % 100 != 0) &lt;font color="blue"&gt;OR&lt;/font&gt;&lt;br /&gt;                               (@Year % 400  = 0)&lt;br /&gt;                          &lt;font color="blue"&gt;THEN&lt;/font&gt; 29&lt;br /&gt;                          &lt;font color="blue"&gt;ELSE&lt;/font&gt; 28&lt;br /&gt;                     &lt;font color="blue"&gt;END&lt;br /&gt;           END&lt;br /&gt;&lt;br /&gt;END&lt;/font&gt;&lt;/pre&gt;&lt;div style="text-align:right;"&gt;&lt;a href="#sqldatetimetop"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;a id="dategettimeonly"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;font color="green"&gt;-- =============================================&lt;br /&gt;-- Author:  Arsalan Tamiz&lt;br /&gt;-- Description: Gets the Time from Date&lt;br /&gt;-- =============================================&lt;/font&gt;&lt;br /&gt;&lt;font color="blue"&gt;CREATE FUNCTION&lt;/font&gt; [DateGetTimeOnly] &lt;br /&gt;(&lt;br /&gt; &lt;font color="green"&gt;-- Add the parameters for the function here&lt;/font&gt;&lt;br /&gt; @DateToFormat &lt;font color="blue"&gt;datetime&lt;/font&gt;&lt;br /&gt;)&lt;br /&gt;&lt;font color="blue"&gt;RETURNS varchar&lt;/font&gt;(100)&lt;br /&gt;&lt;font color="blue"&gt;AS&lt;br /&gt;BEGIN&lt;/font&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Declare the return variable here&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @Result &lt;font color="blue"&gt;varchar&lt;/font&gt;(100);&lt;br /&gt; &lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @H &lt;font color="blue"&gt;varchar&lt;/font&gt;(3);&lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @Hour &lt;font color="blue"&gt;int&lt;/font&gt;;&lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @M &lt;font color="blue"&gt;varchar&lt;/font&gt;(3); &lt;br /&gt;&lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @Hour = &lt;font color="magenta"&gt;DatePart&lt;/font&gt;(hh, @DateToFormat);&lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @M = &lt;font color="magenta"&gt;DateName&lt;/font&gt;(mi, @DateToFormat);&lt;br /&gt; &lt;font color="blue"&gt;If&lt;/font&gt; @M = &lt;font color="red"&gt;'0'&lt;/font&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @M = &lt;font color="red"&gt;'00'&lt;/font&gt;;&lt;br /&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Add the T-SQL statements to compute the return value here&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @H = &lt;font color="magenta"&gt;CAST&lt;/font&gt;(&lt;font color="magenta"&gt;CASE&lt;/font&gt; &lt;font color="blue"&gt;WHEN&lt;/font&gt; @Hour &gt; 12 &lt;font color="blue"&gt;THEN&lt;/font&gt; @Hour - 12 &lt;font color="blue"&gt;ELSE CASE WHEN&lt;/font&gt; @Hour = 0 &lt;font color="blue"&gt;THEN&lt;/font&gt; 12 &lt;font color="blue"&gt;ELSE&lt;/font&gt; @Hour &lt;font color="blue"&gt;END END AS varchar&lt;/font&gt;);&lt;br /&gt; &lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @Result = @H + &lt;font color="red"&gt;':'&lt;/font&gt; + @M + &lt;font color="red"&gt;' '&lt;/font&gt; + &lt;font color="blue"&gt;CASE WHEN&lt;/font&gt; @Hour &gt;= 12 &lt;font color="blue"&gt;THEN&lt;/font&gt; &lt;font color="red"&gt;'PM'&lt;/font&gt; &lt;font color="blue"&gt;ELSE&lt;/font&gt; &lt;font color="red"&gt;'AM'&lt;/font&gt; &lt;font color="blue"&gt;END&lt;/font&gt;;&lt;br /&gt; &lt;br /&gt; &lt;font color="green"&gt;-- Return the result of the function&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;RETURN&lt;/font&gt; @Result;&lt;br /&gt;&lt;font color="blue"&gt;END&lt;/font&gt;&lt;/pre&gt;&lt;div style="text-align:right;"&gt;&lt;a href="#sqldatetimetop"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;a id="dategetdateonly"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;font color="green"&gt;-- =============================================&lt;br /&gt;-- Author:  Arsalan Tamiz&lt;br /&gt;-- Description: Returns Date only from given Date&lt;br /&gt;--    That is removes the Time Part&lt;br /&gt;--    so that we can compare two dates&lt;br /&gt;-- =============================================&lt;/font&gt;&lt;br /&gt;&lt;font color="blue"&gt;CREATE FUNCTION&lt;/font&gt; [DateGetDateOnly] &lt;br /&gt;(&lt;br /&gt; &lt;font color="green"&gt;-- Add the parameters for the function here&lt;/font&gt;&lt;br /&gt; @DateToConvert &lt;font color="blue"&gt;datetime&lt;/font&gt;&lt;br /&gt;)&lt;br /&gt;&lt;font color="blue"&gt;RETURNS datetime&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;/font&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Declare the return variable here&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @Result &lt;font color="blue"&gt;datetime&lt;/font&gt;;&lt;br /&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Add the T-SQL statements to compute the return value here&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @Result = &lt;font color="magenta"&gt;CAST&lt;/font&gt;(&lt;font color="magenta"&gt;CONVERT&lt;/font&gt;(&lt;font color="blue"&gt;varchar&lt;/font&gt;(100), @DateToConvert, 112) &lt;font color="blue"&gt;AS datetime&lt;/font&gt;);&lt;br /&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Return the result of the function&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;RETURN&lt;/font&gt; @Result;&lt;br /&gt;&lt;font color="blue"&gt;END&lt;/font&gt;&lt;/pre&gt;&lt;div style="text-align:right;"&gt;&lt;a href="#sqldatetimetop"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;a id="dategetmonthfirstdate"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;font color="green"&gt;-- =============================================&lt;br /&gt;-- Author:  Arsalan Tamiz&lt;br /&gt;-- Description: Gets the first date of month&lt;br /&gt;-- =============================================&lt;/font&gt;&lt;br /&gt;&lt;font color="blue"&gt;CREATE FUNCTION&lt;/font&gt; [DateGetMonthFirstDate] &lt;br /&gt;(&lt;br /&gt; &lt;font color="green"&gt;-- Add the parameters for the function here&lt;/font&gt;&lt;br /&gt; @Month &lt;font color="blue"&gt;int&lt;/font&gt;&lt;br /&gt;)&lt;br /&gt;&lt;font color="blue"&gt;RETURNS datetime&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;/font&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Declare the return variable here&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @Result &lt;font color="blue"&gt;datetime&lt;/font&gt;;&lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @Year &lt;font color="blue"&gt;int&lt;/font&gt;;&lt;br /&gt;&lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @Year = &lt;font color="magenta"&gt;Year&lt;/font&gt;(&lt;font color="magenta"&gt;GetDate&lt;/font&gt;());&lt;br /&gt;&lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @Result = &lt;font color="magenta"&gt;CAST&lt;/font&gt; (&lt;br /&gt;    &lt;font color="magenta"&gt;CAST&lt;/font&gt;(@Year &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(10)) + &lt;font color="red"&gt;'-'&lt;/font&gt; + &lt;br /&gt;    &lt;font color="magenta"&gt;CAST&lt;/font&gt;(@Month &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(2)) + &lt;font color="red"&gt;'-1'&lt;/font&gt; &lt;font color="blue"&gt;AS datetime&lt;/font&gt;&lt;br /&gt;    );&lt;br /&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Return the result of the function&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;RETURN&lt;/font&gt; @Result;&lt;br /&gt;&lt;font color="blue"&gt;END&lt;/font&gt;&lt;/pre&gt;&lt;div style="text-align:right;"&gt;&lt;a href="#sqldatetimetop"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;a id="dategetmonthlastdate"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;font color="green"&gt;-- =============================================&lt;br /&gt;-- Author:  Arsalan Tamiz&lt;br /&gt;-- Description: Returns last date of month&lt;br /&gt;-- ***********************************************&lt;br /&gt;-- IMPORTANT: This function depends on [dbo].[DateGetDaysInMonth]() function&lt;br /&gt;--    which can be found above&lt;br /&gt;-- ***********************************************&lt;br /&gt;-- =============================================&lt;/font&gt;&lt;br /&gt;&lt;font color="blue"&gt;CREATE FUNCTION&lt;/font&gt; [DateGetMonthLastDate] &lt;br /&gt;(&lt;br /&gt; &lt;font color="green"&gt;-- Add the parameters for the function here&lt;/font&gt;&lt;br /&gt; @Month &lt;font color="blue"&gt;int&lt;/font&gt;&lt;br /&gt;)&lt;br /&gt;&lt;font color="blue"&gt;RETURNS datetime&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;/font&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Declare the return variable here&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @Result &lt;font color="blue"&gt;datetime&lt;/font&gt;;&lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @Year &lt;font color="blue"&gt;int&lt;/font&gt;;&lt;br /&gt; &lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @Year = &lt;font color="magenta"&gt;Year&lt;/font&gt;(&lt;font color="magenta"&gt;GetDate&lt;/font&gt;());&lt;br /&gt; &lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @Result = &lt;font color="magenta"&gt;CAST&lt;/font&gt; (&lt;br /&gt;    &lt;font color="magenta"&gt;CAST&lt;/font&gt;(@Year &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(10)) + &lt;font color="red"&gt;'-'&lt;/font&gt; + &lt;br /&gt;    &lt;font color="magenta"&gt;CAST&lt;/font&gt;(@Month &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(2)) + &lt;font color="red"&gt;'-'&lt;/font&gt; +&lt;br /&gt;    &lt;font color="magenta"&gt;CAST&lt;/font&gt;([dbo].[DateGetDaysInMonth](@Year, @Month) &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(2)) +&lt;br /&gt;    &lt;font color="blue"&gt;AS datetime&lt;/font&gt;&lt;br /&gt;    );&lt;br /&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Return the result of the function&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;RETURN&lt;/font&gt; @Result;&lt;br /&gt;&lt;font color="blue"&gt;END&lt;/font&gt;&lt;/pre&gt;&lt;div style="text-align:right;"&gt;&lt;a href="#sqldatetimetop"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;a id="dategetmonthname"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;font color="green"&gt;-- =============================================&lt;br /&gt;-- Author:  Arsalan Tamiz&lt;br /&gt;-- Description: This function Retuns the name of month&lt;br /&gt;-- =============================================&lt;/font&gt;&lt;br /&gt;&lt;font color="blue"&gt;CREATE FUNCTION&lt;/font&gt; [DateGetMonthName] &lt;br /&gt;(&lt;br /&gt; &lt;font color="green"&gt;-- Add the parameters for the function here&lt;/font&gt;&lt;br /&gt; @Month &lt;font color="blue"&gt;int&lt;/font&gt;&lt;br /&gt;)&lt;br /&gt;&lt;font color="blue"&gt;RETURNS varchar&lt;/font&gt;(100)&lt;br /&gt;&lt;font color="blue"&gt;AS&lt;br /&gt;BEGIN&lt;/font&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Declare the return variable here&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @Result &lt;font color="blue"&gt;varchar&lt;/font&gt;(100)&lt;br /&gt; &lt;font color="blue"&gt;DECLARE&lt;/font&gt; @d &lt;font color="blue"&gt;datetime&lt;/font&gt;;&lt;br /&gt;&lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @d = &lt;font color="magenta"&gt;CAST&lt;/font&gt;(&lt;font color="red"&gt;'2009-'&lt;/font&gt; + &lt;font color="magenta"&gt;CAST&lt;/font&gt;(@Month &lt;font color="blue"&gt;AS varchar&lt;/font&gt;(2)) + &lt;font color="red"&gt;'-1'&lt;/font&gt; &lt;font color="blue"&gt;AS datetime&lt;/font&gt;);&lt;br /&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Add the T-SQL statements to compute the return value here&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;SET&lt;/font&gt; @Result = &lt;font color="magenta"&gt;DateName&lt;/font&gt;(mm, @d);&lt;br /&gt;&lt;br /&gt; &lt;font color="green"&gt;-- Return the result of the function&lt;/font&gt;&lt;br /&gt; &lt;font color="blue"&gt;RETURN&lt;/font&gt; @Result;&lt;br /&gt;&lt;font color="blue"&gt;END&lt;/font&gt;&lt;/pre&gt;&lt;div style="text-align:right;"&gt;&lt;a href="#sqldatetimetop"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-3948590518106362624?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/E_ZrBYc5SGHaGo8L7DVy3ZNVxHo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/E_ZrBYc5SGHaGo8L7DVy3ZNVxHo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/E_ZrBYc5SGHaGo8L7DVy3ZNVxHo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/E_ZrBYc5SGHaGo8L7DVy3ZNVxHo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/CeWBo9HBYTI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/3948590518106362624/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=3948590518106362624" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/3948590518106362624?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/3948590518106362624?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/CeWBo9HBYTI/sql-server-date-time-useful-functions.html" title="SQL Server Date Time Useful Functions" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2009/03/sql-server-date-time-useful-functions.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEcDR3g5fCp7ImA9WxVXFUQ.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-6303981005839181608</id><published>2009-02-14T11:01:00.004+05:00</published><updated>2009-02-14T11:14:36.624+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-02-14T11:14:36.624+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="VB.net" /><category scheme="http://www.blogger.com/atom/ns#" term="Custom Controls" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><title>Notification Bar Control For Windows Forms (Win Forms)</title><content type="html">&lt;a href="http://1.bp.blogspot.com/_HfnUI4smnYA/SZZeXp9Wa8I/AAAAAAAAAH8/Hj9uj_vU2mk/s1600-h/NotificationBar.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 129px;" src="http://1.bp.blogspot.com/_HfnUI4smnYA/SZZeXp9Wa8I/AAAAAAAAAH8/Hj9uj_vU2mk/s320/NotificationBar.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5302529371768515522" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Here is simple Notification Bar control (like we see in Internet Explorer). Gives application a good look. Note that I haven't done much thing, you can customize is further if you like. It supports blinking too.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;ShowNotification()&lt;/strong&gt; method to display the Notification.&lt;br /&gt;&lt;strong&gt;BlinkTimes&lt;/strong&gt; property to set the Number of Times to blink.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Download Source Code:&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://cid-c23746227866488d.skydrive.live.com/self.aspx/Public/NotificationBar.zip"&gt;Notificationbar.zip&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-6303981005839181608?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XcOJh_kA_-6i7bB9bhFgb87KQ-A/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XcOJh_kA_-6i7bB9bhFgb87KQ-A/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XcOJh_kA_-6i7bB9bhFgb87KQ-A/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XcOJh_kA_-6i7bB9bhFgb87KQ-A/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/2bRwa9Wyxdg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/6303981005839181608/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=6303981005839181608" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/6303981005839181608?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/6303981005839181608?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/2bRwa9Wyxdg/notification-bar-control-for-windows.html" title="Notification Bar Control For Windows Forms (Win Forms)" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/_HfnUI4smnYA/SZZeXp9Wa8I/AAAAAAAAAH8/Hj9uj_vU2mk/s72-c/NotificationBar.jpg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2009/02/notification-bar-control-for-windows.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0IESX0-cCp7ImA9WxVRGUg.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-7763839873142305988</id><published>2009-01-26T12:44:00.003+05:00</published><updated>2009-01-26T13:45:08.358+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-01-26T13:45:08.358+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="VB.net" /><category scheme="http://www.blogger.com/atom/ns#" term="Win Forms" /><category scheme="http://www.blogger.com/atom/ns#" term="Custom Controls" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGridView" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><title>DataGridView Custom Cell ToolTip</title><content type="html">&lt;a href="http://4.bp.blogspot.com/_HfnUI4smnYA/SX1qdXLPhLI/AAAAAAAAAH0/joQsoUD2JmA/s1600-h/DGVCustomCellToolTip.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 217px;" src="http://4.bp.blogspot.com/_HfnUI4smnYA/SX1qdXLPhLI/AAAAAAAAAH0/joQsoUD2JmA/s320/DGVCustomCellToolTip.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5295505789527491762" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;ul&gt;&lt;li&gt;DataGridView Control&lt;/li&gt;&lt;li&gt;Windows Forms&lt;/li&gt;&lt;/ul&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;We have a built-in ToolTip for each Cell in DataGridView control. Which is displayed when the text is long and cannot be displayed completely in Cell. But my requirement was to create a custom ToolTip (as displayed in above image).&lt;br /&gt;&lt;br /&gt;There were 2 main requirements,&lt;br /&gt;&lt;br /&gt;1) ToolTip should be displayed permanently that is, it should NOT be disappeared automatically after sometime.&lt;br /&gt;2) I want to have a fancy look of my ToolTip (again see the image).&lt;br /&gt;&lt;br /&gt;For this purpose we can also use the .net's built-in ToolTip control. Which can have a little fancy look by setting some of its properties like &lt;strong&gt;IsBubble&lt;/strong&gt;, &lt;strong&gt;ToolTipIcon&lt;/strong&gt; and &lt;strong&gt;ToolTipTitle&lt;/strong&gt;. Also the ToolTip control can be more customized by using &lt;strong&gt;OwnerDraw&lt;/strong&gt; mode. But still I would like to use my own ToolTip window. Because to me, this is more easier to do.&lt;br /&gt;&lt;br /&gt;Simply I implemented it by creating a new window for ToolTip and I show this window when mouse enters in a cell. But to make it more user friendly, I have used a Timer control. So that whenever mouse enters in a Cell I start timer and in Timer's Tick Event Handler, I display the ToolTip window.&lt;br /&gt;&lt;br /&gt;Few things to be considered here,&lt;br /&gt;&lt;br /&gt;1) DataGridView's &lt;strong&gt;ShowCellToolTips&lt;/strong&gt; property should be set to &lt;strong&gt;False&lt;/strong&gt;&lt;br /&gt;2) Decide where to display the ToolTip window&lt;br /&gt;&lt;br /&gt;So in my case I decided to display the ToolTip window just on the Cell. OK this is NOT just straight. We first need to get the Cell's actual Coordinate in DataGridView by executing DataGridView's &lt;strong&gt;GetCellDisplayRectangle()&lt;/strong&gt; function. Then we need to convert these coordinates into Screen's Coordinates by calling the DataGridView's &lt;strong&gt;PointToScreen()&lt;/strong&gt; function.&lt;br /&gt;&lt;br /&gt;Please note that, to display our own custom ToolTip window on our given position we need to set its property &lt;strong&gt;StartPosition = Manual&lt;/strong&gt;, which I have discussed in my earlier post &lt;a href="http://arsalantamiz.blogspot.com/2008/11/setting-window-form-position.html"&gt;Setting Window / Form Position Programmatically&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Another thing I have added in the ToolTip window is the &lt;strong&gt;Close Button&lt;/strong&gt;. Which is actually a &lt;strong&gt;NON Focusable&lt;/strong&gt; button as I have discussed my previous post &lt;a href="http://arsalantamiz.blogspot.com/2009/01/not-focusable-not-selectable-button.html"&gt;NOT Focusable / NOT Selectable Button&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Download Source:&lt;br /&gt;&lt;a href="http://cid-c23746227866488d.skydrive.live.com/self.aspx/arsalantamizblogspotfiles/DataGridViewCustomCellToolTip.zip"&gt;DataGridViewCustomCellToolTip.zip&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-7763839873142305988?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/98BbAhU3hAbbjtmCUL5PosW9aYA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/98BbAhU3hAbbjtmCUL5PosW9aYA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/98BbAhU3hAbbjtmCUL5PosW9aYA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/98BbAhU3hAbbjtmCUL5PosW9aYA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/jZDJCXKtsR0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/7763839873142305988/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=7763839873142305988" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/7763839873142305988?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/7763839873142305988?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/jZDJCXKtsR0/datagridview-custom-cell-tooltip.html" title="DataGridView Custom Cell ToolTip" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_HfnUI4smnYA/SX1qdXLPhLI/AAAAAAAAAH0/joQsoUD2JmA/s72-c/DGVCustomCellToolTip.jpg" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2009/01/datagridview-custom-cell-tooltip.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQGSXc-cSp7ImA9WxVRF00.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-673291084176235702</id><published>2009-01-23T15:10:00.006+05:00</published><updated>2009-01-23T15:25:28.959+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-01-23T15:25:28.959+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="VB.net" /><category scheme="http://www.blogger.com/atom/ns#" term="Custom Controls" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><title>NOT Focusable / NOT Selectable Button</title><content type="html">&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;br /&gt;User Controls&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;Few days back I was creating a user control like ComboBox. In ComboBox we have a Drop Down button at the right side, which cannot have focus. This button can only be clicked by mouse to open the Drop Down portion (Alt+Down is the shortcut).&lt;br /&gt;&lt;br /&gt;Similarly my control also have a button on right. I decided to make that button just like the Drop Down button of ComboBox. For this purpose I added a plain Class in my project and then put the following Code,&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;font color="blue"&gt;Public Class&lt;/font&gt; NotSelectableButton&lt;br /&gt;    &lt;font color="blue"&gt;Inherits&lt;/font&gt; Button&lt;br /&gt;&lt;br /&gt;    &lt;font color="blue"&gt;Public Sub New&lt;/font&gt;()&lt;br /&gt;        &lt;font color="blue"&gt;MyBase&lt;/font&gt;.&lt;font color="blue"&gt;New&lt;/font&gt;()&lt;br /&gt;        &lt;font color="green"&gt;' following line will make this button Not Focusable&lt;/font&gt;&lt;br /&gt;        SetStyle(ControlStyles.Selectable, &lt;font color="blue"&gt;False&lt;/font&gt;)&lt;br /&gt;    &lt;font color="blue"&gt;End Sub&lt;/font&gt;&lt;br /&gt;&lt;font color="blue"&gt;End Class&lt;/font&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-673291084176235702?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/2T_OJWVdzGLadlECwyNfDzEtNsA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2T_OJWVdzGLadlECwyNfDzEtNsA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/2T_OJWVdzGLadlECwyNfDzEtNsA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2T_OJWVdzGLadlECwyNfDzEtNsA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/1zvNO4NJxV0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/673291084176235702/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=673291084176235702" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/673291084176235702?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/673291084176235702?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/1zvNO4NJxV0/not-focusable-not-selectable-button.html" title="NOT Focusable / NOT Selectable Button" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>1</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2009/01/not-focusable-not-selectable-button.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkMEQ3k5eyp7ImA9WxVREUo.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-2938315967681550261</id><published>2009-01-17T12:28:00.005+05:00</published><updated>2009-01-17T12:46:42.723+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-01-17T12:46:42.723+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Useful Functions" /><category scheme="http://www.blogger.com/atom/ns#" term="VB.net" /><category scheme="http://www.blogger.com/atom/ns#" term="FTP" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><title>FTP File Upload Error</title><content type="html">It seems that our Network Administrator has changed some settings because my previous file uploading code is NOT working. And giving the following exception:&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;I was using the VB.net &lt;strong&gt;My&lt;/strong&gt; namespace,&lt;pre class="code"&gt;&lt;font color="blue"&gt;My&lt;/font&gt;.Computer.Network.UploadFile(&lt;font color="red"&gt;"C:\SomeFile.txt"&lt;/font&gt;, &lt;font color="red"&gt;"ftp://ftpsite/somefile.txt"&lt;/font&gt;, &lt;font color="red"&gt;"userid"&lt;/font&gt;, &lt;font color="red"&gt;"pwd"&lt;/font&gt;)&lt;/pre&gt;So I Googled about that error and found some dicussion on &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/661a537b-813f-465c-bdd3-e1df975b7e3c"&gt;MSDN forums&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Then I modified and created my own upload function as,&lt;pre class="code"&gt;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; UploadFile(&lt;font color=blue&gt;ByVal&lt;/font&gt; sDestination &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;String&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; sSource &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;String&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; sUserID &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;String&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; sPassword &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;String&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; i &lt;font color=blue&gt;As&lt;/font&gt; System.Net.FtpWebRequest&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; us &lt;font color=blue&gt;As&lt;/font&gt; System.IO.Stream&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; filebytes &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Byte&lt;/font&gt;()&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' create the FTP Web Request&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i = System.Net.WebRequest.Create(sDestination)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' set credentials&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i.Credentials = &lt;font color=blue&gt;New&lt;/font&gt; System.Net.NetworkCredential(sUserID, sPassword)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' method = Upload File&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i.Method = System.Net.WebRequestMethods.Ftp.UploadFile&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i.UsePassive = &lt;font color=blue&gt;False&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i.Proxy = &lt;font color=blue&gt;Nothing&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' get the Request Stream&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;us = i.GetRequestStream()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' get the file bytes from source&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;filebytes = My.Computer.FileSystem.ReadAllBytes(sSource)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' write the bytes in stream&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;us.Write(filebytes, 0, filebytes.Length)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' close the stream&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;us.Close()&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;/pre&gt;&lt;div style="background-color:yellow;border-style:solid;border-width:2px;border-color:red;padding:10px;"&gt;Warning! Above function will read &lt;strong&gt;all the bytes (in one go)&lt;/strong&gt; from file. Which is NOT a good practice for &lt;strong&gt;larger files&lt;/strong&gt;.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-2938315967681550261?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/MtSAW9xa1QpsTMvOP5AoM4NJhjY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MtSAW9xa1QpsTMvOP5AoM4NJhjY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/MtSAW9xa1QpsTMvOP5AoM4NJhjY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MtSAW9xa1QpsTMvOP5AoM4NJhjY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/FKVU0aVSsio" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/2938315967681550261/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=2938315967681550261" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/2938315967681550261?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/2938315967681550261?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/FKVU0aVSsio/ftp-file-upload-error.html" title="FTP File Upload Error" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2009/01/ftp-file-upload-error.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0MCRXozeyp7ImA9WxVTGEo.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-943483184740389024</id><published>2009-01-02T11:14:00.006+05:00</published><updated>2009-01-02T11:57:44.483+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-01-02T11:57:44.483+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="HTML Tips/Tricks" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.net" /><category scheme="http://www.blogger.com/atom/ns#" term="AJAX" /><title>Another way of accessing a file on Web Server (Javascript/ASP.net)</title><content type="html">Few days back while working on back-end of a Web Portal. I was trying to create an &lt;strong&gt;Anchor Tag (&amp;lt;a&amp;gt;)&lt;/strong&gt; in such a way that a log should also be created whenever that anchor tag is clicked by user.&lt;br /&gt;&lt;br /&gt;So there are couple of techniques.&lt;br /&gt;&lt;br /&gt;Approach #1:&lt;br /&gt;On Click: Post back to server create the log and redirect to the URL.&lt;br /&gt;&lt;br /&gt;Approach #2:&lt;br /&gt;Use AJAX&lt;br /&gt;&lt;br /&gt;So I used the 2nd approach. For this purpose I first created a Javascript function as,&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;First Try:&lt;/strong&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;font color="blue"&gt;function&lt;/font&gt; createlog(param1, param2) {&lt;br /&gt; &lt;font color="blue"&gt;var&lt;/font&gt; xmlHttp;&lt;br /&gt; &lt;font color="blue"&gt;try&lt;/font&gt;&lt;br /&gt;  {  &lt;font color="green"&gt;// Firefox, Opera 8.0+, Safari&lt;/font&gt;&lt;br /&gt;  xmlHttp=&lt;font color="blue"&gt;new&lt;/font&gt; XMLHttpRequest();  &lt;br /&gt; }&lt;br /&gt; &lt;font color="blue"&gt;catch&lt;/font&gt; (e)&lt;br /&gt; {  &lt;font color="green"&gt;// Internet Explorer&lt;/font&gt;&lt;br /&gt;  &lt;font color="blue"&gt;try&lt;/font&gt;&lt;br /&gt;  {&lt;br /&gt;   xmlHttp=&lt;font color="blue"&gt;new&lt;/font&gt; ActiveXObject(&lt;font color="red"&gt;"Msxml2.XMLHTTP"&lt;/font&gt;);    &lt;br /&gt;  }&lt;br /&gt;  &lt;font color="blue"&gt;catch&lt;/font&gt; (e)&lt;br /&gt;      {&lt;br /&gt;   &lt;font color="blue"&gt;try&lt;/font&gt;&lt;br /&gt;   {&lt;br /&gt;    xmlHttp=&lt;font color="blue"&gt;new&lt;/font&gt; ActiveXObject(&lt;font color="red"&gt;"Microsoft.XMLHTTP"&lt;/font&gt;);&lt;br /&gt;   }&lt;br /&gt;       &lt;font color="blue"&gt;catch&lt;/font&gt; (e)&lt;br /&gt;   {&lt;br /&gt;    alert(&lt;font color="red"&gt;"Your browser does not support AJAX!"&lt;/font&gt;);&lt;br /&gt;    &lt;font color="blue"&gt;return false&lt;/font&gt;;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; &lt;font color="blue"&gt;try&lt;/font&gt;&lt;br /&gt; {&lt;br /&gt;  &lt;font color="blue"&gt;var&lt;/font&gt; u;&lt;br /&gt;  u = &lt;font color="red"&gt;"createlog.aspx?param1="&lt;/font&gt; + param1 + &lt;font color="red"&gt;"&amp;amp;param2="&lt;/font&gt; + param2;&lt;br /&gt;  xmlHttp.open(&lt;font color="red"&gt;"POST"&lt;/font&gt;, u, &lt;font color="blue"&gt;false&lt;/font&gt;);&lt;br /&gt;  xmlHttp.send(&lt;font color="blue"&gt;null&lt;/font&gt;);&lt;br /&gt;  &lt;font color="blue"&gt;return true&lt;/font&gt;;&lt;br /&gt; }&lt;br /&gt; &lt;font color="blue"&gt;catch&lt;/font&gt; (e)&lt;br /&gt; {&lt;br /&gt;  &lt;font color="blue"&gt;return false&lt;/font&gt;;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So this is an example function. Which will create a log by executing a "createlog.aspx" file on server which accepts 2 parameters in QueryString. The above function is simply using the &lt;strong&gt;XMLHttpRequest()&lt;/strong&gt; object for AJAX call.&lt;br /&gt;&lt;br /&gt;Then we can use this function in an anchor tag as,&lt;br /&gt;&lt;pre class="code"&gt;&amp;lt;a href="http://www.domain.com" onclick="return createlog(1, 2);"&amp;gt;www.domain.com&amp;lt;/a&amp;gt;&lt;/pre&gt;&lt;br /&gt;When user will click on the link the &lt;strong&gt;createlog()&lt;/strong&gt; function will be executed first. Which then will create the log on server and then redirect to the link.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Second Try:&lt;/strong&gt;&lt;br /&gt;Secondly I looked in the Google's Result Page. Because they are doing this also. Whenever we click on a result, Google redirects to that URL directly (without posting back) but also keeps the track of clicking. I looked into their Javascript and found a very interesting thing. And hence modified my Javascript function to this,&lt;br /&gt;&lt;pre class="code"&gt;&lt;font color="blue"&gt;function&lt;/font&gt; createlog(param1, param2) {&lt;br /&gt; &lt;font color="blue"&gt;var&lt;/font&gt; u = &lt;font color="red"&gt;"createlog.aspx?param1="&lt;/font&gt; + param1 + &lt;font color="red"&gt;"&amp;amp;param2="&lt;/font&gt; + param2;&lt;br /&gt; (&lt;font color="blue"&gt;new&lt;/font&gt; Image).src = u;&lt;br /&gt; &lt;font color="blue"&gt;return true&lt;/font&gt;;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Because my requirement is to just execute the "createlog.aspx" file on the server. I do NOT need the returned value. That is why XMLHttpRequest() is NOT critically required in this scenario. On the other hand &lt;strong&gt;(new Image).src&lt;/strong&gt; approach is lot simpler.&lt;br /&gt;&lt;br /&gt;&lt;div style="padding:10px 10px 10px 10px; background-color:lightyellow; border-style:solid; border-width:1px; border-color:orange"&gt;What happens is that the browser creates an Image object and go to the "source" (in this case my createlog.aspx) which actually do NOT return any data (i.e. image bytes). But by going to that URL it actually is executed on server. And the Image object is then discarded at the end of function (I guess).&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;One more thing I found was Google is using "onmousedown" event instead of "onclick". I don't know exactly the benefits but I have experience that "onclick" event is NOT fired (on some browsers) when we right click and open the link in new window or tab. "onmousedown" event is triggered each time we click on link even if we right click on it. So it seems that this is NOT a good approach to use "onmousedown". But I have checked that whenever we right/left click on the link for the first time, the file "createlog.aspx" is executed on server. But it does NOT execute, if we click again. I think this is due to cache maybe.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-943483184740389024?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/aXCIhTXjiptkiEyrmEbU790g9u8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/aXCIhTXjiptkiEyrmEbU790g9u8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/aXCIhTXjiptkiEyrmEbU790g9u8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/aXCIhTXjiptkiEyrmEbU790g9u8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/13wSPg60NXA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/943483184740389024/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=943483184740389024" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/943483184740389024?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/943483184740389024?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/13wSPg60NXA/another-way-of-accessing-file-on-web.html" title="Another way of accessing a file on Web Server (Javascript/ASP.net)" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>2</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2009/01/another-way-of-accessing-file-on-web.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUUNRnY_eCp7ImA9WxVTEEw.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-5939653265951594517</id><published>2008-12-23T12:14:00.004+05:00</published><updated>2008-12-23T12:28:17.840+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-12-23T12:28:17.840+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Useful Functions" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><title>Image to Byte Array and Byte Array to Image</title><content type="html">&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;br /&gt;Image Class&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;Here are simple functions that convert an Image into Byte Array and Byte Array into Image. An image is required to be converted into bytes array when we save it in Database, hence on reading the bytes from database, it needs to be converted back into image.&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Function&lt;/font&gt; BytesToImage(&lt;font color=blue&gt;ByVal&lt;/font&gt; ImageBytes() &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Byte&lt;/font&gt;) &lt;font color=blue&gt;As&lt;/font&gt; Image&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; imgNew &lt;font color=blue&gt;As&lt;/font&gt; Image&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; memImage &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;New&lt;/font&gt; System.IO.MemoryStream(ImageBytes)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;imgNew = Image.FromStream(memImage)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Return imgNew&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Function&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Function&lt;/font&gt; ImageToBytes(&lt;font color=blue&gt;ByVal&lt;/font&gt; Image &lt;font color=blue&gt;As&lt;/font&gt; Image) &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Byte&lt;/font&gt;()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; memImage &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;New&lt;/font&gt; System.IO.MemoryStream&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; bytImage() &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Byte&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Image.Save(memImage, Image.RawFormat)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bytImage = memImage.GetBuffer()&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Return bytImage&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Function&lt;/font&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-5939653265951594517?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/eBZ3-3t-0LXHPEpkb0tpJnMZhxk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eBZ3-3t-0LXHPEpkb0tpJnMZhxk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/eBZ3-3t-0LXHPEpkb0tpJnMZhxk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eBZ3-3t-0LXHPEpkb0tpJnMZhxk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/sj_GLWq4K-Q" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/5939653265951594517/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=5939653265951594517" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/5939653265951594517?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/5939653265951594517?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/sj_GLWq4K-Q/image-to-byte-array-and-byte-array-to.html" title="Image to Byte Array and Byte Array to Image" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>2</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/12/image-to-byte-array-and-byte-array-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0UGQ3s9fSp7ImA9WxVTGEo.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-5950252544519798374</id><published>2008-12-18T22:44:00.003+05:00</published><updated>2009-01-02T10:47:02.565+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-01-02T10:47:02.565+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="HTML Tips/Tricks" /><title>Java Script Function to Show Flash Banner</title><content type="html">Usually I develop Desktop Application. But sometimes I also get involve in Web Development. So we were developing a Website in which we were trying to create a Flash Banner rotator. To accomplish this there are several techniques, but I created the following Java Script function for our Web Developer.&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;font color="blue"&gt;function&lt;/font&gt; showflashbanner(htmlobjectid, flashfilename, width, height) {&lt;br /&gt;    &lt;font color="blue"&gt;var&lt;/font&gt; script;&lt;br /&gt;    script = &lt;font color="red"&gt;"&amp;lt;object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' "&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0' "&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"width='"&lt;/font&gt; + width + &lt;font color="red"&gt;"' height='"&lt;/font&gt; + height + &lt;font color="red"&gt;"' \&amp;gt;"&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"&amp;lt;param name='movie' "&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"value='"&lt;/font&gt; + flashfilename + &lt;font color="red"&gt;"' /\&amp;gt;"&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"&amp;lt;param name='quality' value='high' /\&amp;gt;"&lt;/font&gt; + &lt;br /&gt;             &lt;font color="red"&gt;"&amp;lt;embed src='"&lt;/font&gt; + flashfilename + &lt;font color="red"&gt;"' "&lt;/font&gt; + &lt;br /&gt;             &lt;font color="red"&gt;"quality='high' "&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' "&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"type='application/x-shockwave-flash'"&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"width='"&lt;/font&gt; + width + &lt;font color="red"&gt;"'"&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"height='"&lt;/font&gt; + height + &lt;font color="red"&gt;"'\&amp;gt'"&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"&amp;lt;/embed\&amp;gt;"&lt;/font&gt; +&lt;br /&gt;             &lt;font color="red"&gt;"&amp;lt;/object\&amp;gt;"&lt;/font&gt;;&lt;br /&gt;    &lt;font color="blue"&gt;document&lt;/font&gt;.getElementById(htmlobjectid).innerHTML = script;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br /&gt;&lt;pre class="code"&gt;&amp;lt;body onload="showflashbanner('bannercontainer', 'http://www.mywebsite.com/banners/banner1.swf', 720, 100)"&amp;gt;&lt;br /&gt;&amp;lt;div id="bannercontainer"&amp;gt;Loading...&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;/pre&gt;&lt;br /&gt;As you can see the above example we have used "div" as a Banner container. Then we are passing this div's id in our function so the Flash File will be loaded in it.&lt;br /&gt;&lt;br /&gt;The Web developer then used this function by creating an Array of Flash Files. He also created another function in which he used &lt;strong&gt;setTimeout&lt;/strong&gt; function. So the function calls itself after some time with new Flash File.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-5950252544519798374?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/sK3ko0jWBi6bZRRMdEe-h6giq7k/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/sK3ko0jWBi6bZRRMdEe-h6giq7k/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/sK3ko0jWBi6bZRRMdEe-h6giq7k/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/sK3ko0jWBi6bZRRMdEe-h6giq7k/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/AkSfbMtFRI8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/5950252544519798374/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=5950252544519798374" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/5950252544519798374?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/5950252544519798374?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/AkSfbMtFRI8/java-script-function-to-show-flash.html" title="Java Script Function to Show Flash Banner" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/12/java-script-function-to-show-flash.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C04NQn8_cSp7ImA9WxRUEE0.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-4559020116112540515</id><published>2008-11-18T14:21:00.003+05:00</published><updated>2008-11-18T14:53:13.149+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-11-18T14:53:13.149+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Win Forms" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><title>Setting Window / Form Position Programmatically</title><content type="html">&lt;strong&gt;Level:&lt;/strong&gt; Beginner&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;br /&gt;Windows Forms&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;While working with Windows Forms we sometimes require to set the Window/Form Position manually before we show it. Consider the following code:&lt;br /&gt;&lt;pre class="code"&gt;&lt;font color=blue&gt;Dim&lt;/font&gt; frmNew &lt;font color=blue&gt;as&lt;/font&gt; Form2&lt;br /&gt;frmNew = &lt;font color=blue&gt;New&lt;/font&gt; Form2()&lt;br /&gt;frmNew.Location = &lt;font color=blue&gt;New&lt;/font&gt; Point(10, 10)&lt;br /&gt;frmNew.Show()&lt;/pre&gt;&lt;br /&gt;If we execute the above code, it is NOT sure that window will be displayed at 10, 10. Because we haven't set the &lt;strong&gt;StartPosition&lt;/strong&gt; property. By default this property is equal to &lt;strong&gt;WindowsDefaultLocation&lt;/strong&gt; that is why whenever we display the Form, Windows Operating system choose where to put it. And if we change &lt;strong&gt;Location&lt;/strong&gt; after the &lt;strong&gt;frmNew.Show()&lt;/strong&gt;, then our location will set.&lt;br /&gt;&lt;br /&gt;To set the &lt;strong&gt;Location&lt;/strong&gt; programmatically we should first set the &lt;strong&gt;StartPosition&lt;/strong&gt; property as,&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;font color=blue&gt;Dim&lt;/font&gt; frmNew &lt;font color=blue&gt;as&lt;/font&gt; Form2&lt;br /&gt;frmNew = &lt;font color=blue&gt;New&lt;/font&gt; Form2()&lt;br /&gt;frmNew.StartPosition = FormStartPosition.Manual&lt;br /&gt;frmNew.Location = &lt;font color=blue&gt;New&lt;/font&gt; Point(10, 10)&lt;br /&gt;frmNew.Show()&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-4559020116112540515?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/wSjvBqKsSFMexRQYiOmDP_vzsdw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/wSjvBqKsSFMexRQYiOmDP_vzsdw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/wSjvBqKsSFMexRQYiOmDP_vzsdw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/wSjvBqKsSFMexRQYiOmDP_vzsdw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/meVIUdW4i0Q" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/4559020116112540515/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=4559020116112540515" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/4559020116112540515?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/4559020116112540515?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/meVIUdW4i0Q/setting-window-form-position.html" title="Setting Window / Form Position Programmatically" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/11/setting-window-form-position.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0QBSHg9eip7ImA9WxRXEU4.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-5354704768478083606</id><published>2008-10-16T11:52:00.002+06:00</published><updated>2008-10-16T12:42:39.662+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-16T12:42:39.662+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="HTML Tips/Tricks" /><category scheme="http://www.blogger.com/atom/ns#" term="Web Development" /><title>HTML Anchor Tag (&lt;a&gt; Link) with NO Redirection</title><content type="html">Although this Blog is purely for Visual Basic Windows Forms Application Development. But I think we can discuss other stuff too.&lt;br /&gt;&lt;br /&gt;So meanwhile working with HTML, I was trying to create an Anchor with a href defined but the link should NOT work. Click the following link,&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.google.com" onclick="return false;"&gt;Google&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;As you can see the above link should redirect this page to Google, but it is NOT redirecting it. OK now try to open the link in a new Tab (if you are using a borwser which has tabbed browsing support, i.e. Ctrl+Click). This time it should work. Here is the HTML,&lt;br /&gt;&lt;pre class="code"&gt;&amp;lt;a href="http://www.google.com" onclick="return false;"&amp;gt;Google&amp;lt;/a&amp;gt;&lt;/pre&gt;&lt;br /&gt;As you can see in above "onclick" event we have executed a little javascript which returns false. This is the statement which is suppressing the default behaviour of anchor. Some sites handle it like this,&lt;br /&gt;&lt;br /&gt;&lt;a href="javascript:void(0);" onclick="alert('You Clicked the Link');"&gt;Google&lt;/a&gt;&lt;br /&gt;&lt;pre class="code"&gt;&amp;lt;a href="javascript:void(0);" onclick="alert('You Clicked the Link');"&amp;gt;Google&amp;lt;/a&amp;gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-5354704768478083606?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ls6LxTv-k-ktySMiGhKrdXku5lA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ls6LxTv-k-ktySMiGhKrdXku5lA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ls6LxTv-k-ktySMiGhKrdXku5lA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ls6LxTv-k-ktySMiGhKrdXku5lA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/_USHyAB3M9I" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/5354704768478083606/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=5354704768478083606" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/5354704768478083606?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/5354704768478083606?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/_USHyAB3M9I/html-anchor-tag-link-with-no.html" title="HTML Anchor Tag (&amp;lt;a&amp;gt; Link) with NO Redirection" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/10/html-anchor-tag-link-with-no.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ENQnYzfip7ImA9WxRQE0g.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-8784433998371889185</id><published>2008-09-14T13:27:00.006+06:00</published><updated>2008-10-07T12:08:13.886+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-07T12:08:13.886+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Beginner Topics" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGridView" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Data Binding" /><title>Binding DataGridView ComboBox Column</title><content type="html">&lt;a href="http://4.bp.blogspot.com/_HfnUI4smnYA/SMy9zcVAwyI/AAAAAAAAAF0/l4XCC6_EyXQ/s1600-h/OrderForm.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_HfnUI4smnYA/SMy9zcVAwyI/AAAAAAAAAF0/l4XCC6_EyXQ/s320/OrderForm.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5245776357455086370" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Level:&lt;/strong&gt; Beginner&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;ul&gt;&lt;li&gt;Data Binding&lt;/li&gt;&lt;li&gt;DataGridView&lt;/li&gt;&lt;/ul&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;We use DataGridView control lots of times while developing a Data Driven Application. We usually bind the DataGridView control with some BindingSource. Also we can bind the DataGridView's ComboBox Column to some other BindingSource. That is DataGridView has different DataSource and it's ComboBox column has a different. Consider the following scenario,&lt;br /&gt;&lt;br /&gt;We have following Typed DataSet,&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_HfnUI4smnYA/SMy-0Uq2GcI/AAAAAAAAAF8/hyn6sTgyHJo/s1600-h/OrderDataSet.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_HfnUI4smnYA/SMy-0Uq2GcI/AAAAAAAAAF8/hyn6sTgyHJo/s320/OrderDataSet.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5245777472090675650" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;div align="center"&gt;OrderDataSet&lt;/div&gt;&lt;br /&gt;First we will create a simple User Interface,&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_HfnUI4smnYA/SM0fW_OawDI/AAAAAAAAAGU/Wf2mTOjuubM/s1600-h/OrderForm2.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_HfnUI4smnYA/SM0fW_OawDI/AAAAAAAAAGU/Wf2mTOjuubM/s320/OrderForm2.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5245883620745986098" /&gt;&lt;/a&gt;&lt;br /&gt;As you can see in the above figure, the DataGridView control is bind with &lt;strong&gt;OrderDetail&lt;/strong&gt; Table. Note that the Product_ID column is displaying ID which is NOT a friendly approach. Instead Product Name should be displayed here. We can use the DataGridView's ComboBox Column here, in which we will populate all the products.&lt;br /&gt;&lt;br /&gt;The main point to be focused here is that, we will be binding the DataGridView control with the same OrderDetail DataTable, in which there is no Product_Name column. But on front-end the Product_ID column will be set to ComboBox Column in which we will populate the Products' Name.&lt;br /&gt;&lt;br /&gt;Since we need to populate the ComboBox with Products' Name. Therefore we will add another Typed DataSet and BindingSource to our Form,&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_HfnUI4smnYA/SOrslBq8clI/AAAAAAAAAGc/ZrtWH3SzPJM/s1600-h/DGVCBProductDataSet.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_HfnUI4smnYA/SOrslBq8clI/AAAAAAAAAGc/ZrtWH3SzPJM/s320/DGVCBProductDataSet.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5254272036128977490" /&gt;&lt;/a&gt;&lt;br /&gt;We have bind our &lt;strong&gt;ProductBindingSource&lt;/strong&gt; with &lt;strong&gt;ProductDataSet&lt;/strong&gt; which contains &lt;strong&gt;Product&lt;/strong&gt; DataTable. We will fill this table in &lt;strong&gt;Form_Load&lt;/strong&gt; event Handler.&lt;br /&gt;&lt;br /&gt;&lt;div style="padding:10px 10px 10px 10px; background-color:lightyellow; border-style:solid; border-width:1px; border-color:orange"&gt;IMPORTANT: This table needs to be filled before we fill the OrderDataSet OR at-least before the OrderDetail DataGridView is displayed&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Now we need to setup the Product_ID column. First set the ColumnType of Product_ID column to DataGridViewComboBoxColumn and then set the properties as,&lt;ul&gt;&lt;li&gt;DataPropertyName = Product_ID&lt;/li&gt;&lt;li&gt;DataSource = ProductBindingSource&lt;/li&gt;&lt;li&gt;DisplayMember = Product_Name&lt;/li&gt;&lt;li&gt;ValueMember = Product_ID&lt;/li&gt;&lt;/ul&gt;&lt;a href="http://2.bp.blogspot.com/_HfnUI4smnYA/SOrv1LYltjI/AAAAAAAAAGk/iCy93lAN6Kg/s1600-h/DGVCBProperties.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_HfnUI4smnYA/SOrv1LYltjI/AAAAAAAAAGk/iCy93lAN6Kg/s320/DGVCBProperties.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5254275612149134898" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;DataPropertyName&lt;/strong&gt;: tells that Product_ID column of OrderDetail DataTable should be updated&lt;br /&gt;&lt;strong&gt;DataSource&lt;/strong&gt;: is the source from where the list of Products will be taken, in this case the ProductBindingSource which is actually bind with Product DataTable&lt;br /&gt;&lt;strong&gt;DisplayMember&lt;/strong&gt;: Column of Product DataTable which should be used to display in DataGridView control&lt;br /&gt;&lt;strong&gt;ValueMember&lt;/strong&gt;: Column of Product DataTable which should be used to set the Value in DataGridView which ultemately will send the value in OrderDetail DataTable&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_HfnUI4smnYA/SOr40qkxclI/AAAAAAAAAGs/GyvYtUWH7jo/s1600-h/DGVCBBindingExplain.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_HfnUI4smnYA/SOr40qkxclI/AAAAAAAAAGs/GyvYtUWH7jo/s320/DGVCBBindingExplain.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5254285498946515538" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Download:&lt;br /&gt;&lt;a href="http://cid-c23746227866488d.skydrive.live.com/self.aspx/Public/BindingDataGridViewComboBox.zip"&gt;BindingDataGridViewComboBox.zip&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-8784433998371889185?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/d-QHE8HZDE3UjqA3hFG6sCDfuX4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/d-QHE8HZDE3UjqA3hFG6sCDfuX4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/d-QHE8HZDE3UjqA3hFG6sCDfuX4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/d-QHE8HZDE3UjqA3hFG6sCDfuX4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/tMvbha2RnPE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/8784433998371889185/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=8784433998371889185" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/8784433998371889185?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/8784433998371889185?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/tMvbha2RnPE/binding-datagridview-combobox-column.html" title="Binding DataGridView ComboBox Column" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_HfnUI4smnYA/SMy9zcVAwyI/AAAAAAAAAF0/l4XCC6_EyXQ/s72-c/OrderForm.jpg" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/09/binding-datagridview-combobox-column.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUIGQn04fip7ImA9WxRXGE8.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-5296639869596996619</id><published>2008-09-11T10:36:00.003+06:00</published><updated>2008-10-24T11:52:03.336+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-24T11:52:03.336+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="How To" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Intermediate Topics" /><category scheme="http://www.blogger.com/atom/ns#" term="Crystal Reports" /><title>How to handle Overlapping of Large Text Fields in Crystal Reports</title><content type="html">&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;br /&gt;Crystal Reports&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;While working in Crystal Reports, we sometimes face a scenario when we need to put a Large Text Field (which can grow upto multiple lines). The issue comes if we try to put another Field after this Text Field. Example:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_HfnUI4smnYA/SMijXfcOZeI/AAAAAAAAAFM/buhQ2-X7xbE/s1600-h/CrystalReportsFieldsTogethe.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_HfnUI4smnYA/SMijXfcOZeI/AAAAAAAAAFM/buhQ2-X7xbE/s320/CrystalReportsFieldsTogethe.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5244621390045144546" /&gt;&lt;/a&gt;&lt;br /&gt;As you can see in above I have put to Formula Fields, both will contain Large Text which may expand. So here is the preview,&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_HfnUI4smnYA/SMilEf0fjtI/AAAAAAAAAFU/6LBHcS8hDIg/s1600-h/CrystalReportsFieldsTogetherPreview.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_HfnUI4smnYA/SMilEf0fjtI/AAAAAAAAAFU/6LBHcS8hDIg/s320/CrystalReportsFieldsTogetherPreview.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5244623262752673490" /&gt;&lt;/a&gt;&lt;br /&gt;Therefore to solve this issue we can create multiple &lt;strong&gt;sub-sections in the same section&lt;/strong&gt;. This can be achieved by Section Expert.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_HfnUI4smnYA/SMil3E56fBI/AAAAAAAAAFc/8socG21xJos/s1600-h/CrystalReportsSectionExpert.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_HfnUI4smnYA/SMil3E56fBI/AAAAAAAAAFc/8socG21xJos/s320/CrystalReportsSectionExpert.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5244624131700980754" /&gt;&lt;/a&gt;&lt;br /&gt;Now we will put the Fields in different sub-sections as,&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_HfnUI4smnYA/SMimjVnPVaI/AAAAAAAAAFk/QFm6XOQW1L8/s1600-h/CrystalReportsFieldsSection.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_HfnUI4smnYA/SMimjVnPVaI/AAAAAAAAAFk/QFm6XOQW1L8/s320/CrystalReportsFieldsSection.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5244624892100302242" /&gt;&lt;/a&gt;&lt;br /&gt;And here is the preview again,&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_HfnUI4smnYA/SMioC8Hw6OI/AAAAAAAAAFs/iKfB9d3mu9I/s1600-h/CrsytalReportsFieldsSectionPreview.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_HfnUI4smnYA/SMioC8Hw6OI/AAAAAAAAAFs/iKfB9d3mu9I/s320/CrsytalReportsFieldsSectionPreview.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5244626534524840162" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Note that in Crystal Reports Sections automatically expand, that is why when the first Text Field expands, the section also expands itself, therefore ultimately the next field renders properly.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-5296639869596996619?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/LzJhyI2FtO7ZjE0r2Qx4AXFHqDg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/LzJhyI2FtO7ZjE0r2Qx4AXFHqDg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/LzJhyI2FtO7ZjE0r2Qx4AXFHqDg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/LzJhyI2FtO7ZjE0r2Qx4AXFHqDg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/sU8c2tC3YRU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/5296639869596996619/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=5296639869596996619" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/5296639869596996619?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/5296639869596996619?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/sU8c2tC3YRU/how-to-handle-overlapping-of-large-text.html" title="How to handle Overlapping of Large Text Fields in Crystal Reports" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/_HfnUI4smnYA/SMijXfcOZeI/AAAAAAAAAFM/buhQ2-X7xbE/s72-c/CrystalReportsFieldsTogethe.jpg" height="72" width="72" /><thr:total>3</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/09/how-to-handle-overlapping-of-large-text.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ENQnc5eCp7ImA9WxRQE0g.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-6302020754815978227</id><published>2008-09-04T13:18:00.004+06:00</published><updated>2008-10-07T12:08:13.920+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-07T12:08:13.920+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGridView" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Intermediate Topics" /><title>Using DataGridView CheckBox Column as RadioButton (OptionButton)</title><content type="html">&lt;a href="http://3.bp.blogspot.com/_HfnUI4smnYA/SL-Mj1VS48I/AAAAAAAAAE8/4nvEl5LJR9E/s1600-h/DGVCheckBoxAsRadioButtonForm.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_HfnUI4smnYA/SL-Mj1VS48I/AAAAAAAAAE8/4nvEl5LJR9E/s320/DGVCheckBoxAsRadioButtonForm.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5242063038522254274" /&gt;&lt;/a&gt;&lt;strong&gt;Level:&lt;/strong&gt; Beginner&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;ul&gt;&lt;li&gt;DataGridView Control&lt;/li&gt;&lt;li&gt;Data Binding&lt;/li&gt;&lt;/ul&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;In this post we shall see that how we can transform DataGridView control’s CheckBox column into Radio Button (option button).&lt;br /&gt;&lt;br /&gt;I have done 2 main things,&lt;ol&gt;&lt;li&gt;Created a back-end logic when user clicks on CheckBox Column so only one CheckBox should be checked at a time&lt;/li&gt;&lt;li&gt;Change the look of CheckBox column so it looks a Radio Button Column&lt;/li&gt;&lt;/ol&gt;So to understand the first one, consider a DataGridView control as shown in the above figure. This DataGridView control is actually bind with a DataSet for example,&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_HfnUI4smnYA/SL-M6rJVIyI/AAAAAAAAAFE/vRAYjFkyIyw/s1600-h/DGVCheckBoxDataSet.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_HfnUI4smnYA/SL-M6rJVIyI/AAAAAAAAAFE/vRAYjFkyIyw/s320/DGVCheckBoxDataSet.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5242063430924706594" /&gt;&lt;/a&gt;&lt;br /&gt;As you can see there are 2 columns. The &lt;strong&gt;IsSelected&lt;/strong&gt; column is actually a Boolean column which is normally rendered as CheckBox in DataGridView control. What we will be doing is that we first set our DataGridView to &lt;strong&gt;Read Only&lt;/strong&gt; i.e.,&lt;br /&gt;&lt;br /&gt;AllowUserToAddRows = False&lt;br /&gt;AllowUserToDeleteRows = False&lt;br /&gt;ReadOnly = True&lt;br /&gt;&lt;br /&gt;We are making our DataGridView control Read Only because it is easier to set the CheckBox checked or unchecked programmatically otherwise DataGridView control itself will be interfering and will create problems and complexities for us.&lt;br /&gt;&lt;br /&gt;OK now whenever user clicks on CheckBox we will be performing our custom operation. To do this we will use the DataGridView’s &lt;strong&gt;CellContentClick&lt;/strong&gt; event. Here is the code,&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; ShutDownOptionsDataGridView_CellContentClick(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; System.&lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.Windows.Forms.DataGridViewCellEventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; ShutDownOptionsDataGridView.CellContentClick&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; e.ColumnIndex = &lt;font color=blue&gt;Me&lt;/font&gt;.columnIsSelected.Index &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; drv &lt;font color=blue&gt;As&lt;/font&gt; DataRowView&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; rowShutDownOption &lt;font color=blue&gt;As&lt;/font&gt; ShutdownOptionDataSet.ShutDownOptionsRow&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' in this event handler we know that which DataGridView's row is clicked&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' so we are going to extract out the actual DataTable's row which is&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' bind with this DataGridView's Row&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;drv = &lt;font color=blue&gt;CType&lt;/font&gt;(&lt;font color=blue&gt;Me&lt;/font&gt;.ShutDownOptionsDataGridView.Rows(e.RowIndex).DataBoundItem, DataRowView)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' get the DataTable's row&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rowShutDownOption = &lt;font color=blue&gt;CType&lt;/font&gt;(drv.Row, ShutdownOptionDataSet.ShutDownOptionsRow)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' get the row which is currently selected&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; rowCurrentlySelected() &lt;font color=blue&gt;As&lt;/font&gt; ShutdownOptionDataSet.ShutDownOptionsRow&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rowCurrentlySelected = &lt;font color=blue&gt;Me&lt;/font&gt;.ShutdownOptionDataSet.ShutDownOptions.&lt;font color=blue&gt;Select&lt;/font&gt;(&lt;font color=darkred&gt;&amp;quot;IsSelected=True&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' if some row found then make it de-selected&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; rowCurrentlySelected.Length &amp;gt; 0 &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rowCurrentlySelected(0).IsSelected = &lt;font color=blue&gt;False&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' ok now select the row which is clicked&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rowShutDownOption.IsSelected = &lt;font color=blue&gt;True&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;What we do is first get the row in which &lt;strong&gt;IsSelected=True&lt;/strong&gt; and we make that row &lt;strong&gt;IsSelected=False&lt;/strong&gt;. Then we set the row which is clicked as &lt;strong&gt;IsSelected=True&lt;/strong&gt;.&lt;br /&gt;&lt;br /&gt;Next thing is to change the look of CheckBox to OptionButton / RadioButton. For this purpose we will be using DataGridView’s &lt;strong&gt;CellPainting&lt;/strong&gt; event. Here is the code,&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; ShutDownOptionsDataGridView_CellPainting(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.Windows.Forms.DataGridViewCellPaintingEventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; ShutDownOptionsDataGridView.CellPainting&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; e.ColumnIndex = &lt;font color=blue&gt;Me&lt;/font&gt;.columnIsSelected.Index &lt;font color=blue&gt;AndAlso&lt;/font&gt; _&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.RowIndex &amp;gt;= 0 &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.PaintBackground(e.ClipBounds, &lt;font color=blue&gt;True&lt;/font&gt;)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; rectRadioButton &lt;font color=blue&gt;As&lt;/font&gt; Rectangle&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rectRadioButton.Width = 14&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rectRadioButton.Height = 14&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rectRadioButton.X = e.CellBounds.X + (e.CellBounds.Width - rectRadioButton.Width) / 2&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rectRadioButton.Y = e.CellBounds.Y + (e.CellBounds.Height - rectRadioButton.Height) / 2&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; IsDBNull(e.Value) &lt;font color=blue&gt;OrElse&lt;/font&gt; e.Value = &lt;font color=blue&gt;False&lt;/font&gt; &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ControlPaint.DrawRadioButton(e.Graphics, rectRadioButton, ButtonState.Normal)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Else&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ControlPaint.DrawRadioButton(e.Graphics, rectRadioButton, ButtonState.Checked)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Handled = &lt;font color=blue&gt;True&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;As you can see we have used the &lt;strong&gt;ControlPaint&lt;/strong&gt; class to draw the RadioButton / OptionButton.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Download:&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://cid-c23746227866488d.skydrive.live.com/self.aspx/Public/DGVCheckBoxAsRadioButton.zip"&gt;DGVCheckBoxAsRadioButton.zip&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-6302020754815978227?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/rHm6hO0umWcXj0XQDLjN6UOAPjQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/rHm6hO0umWcXj0XQDLjN6UOAPjQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/rHm6hO0umWcXj0XQDLjN6UOAPjQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/rHm6hO0umWcXj0XQDLjN6UOAPjQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/YsPv2S-IygE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/6302020754815978227/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=6302020754815978227" title="12 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/6302020754815978227?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/6302020754815978227?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/YsPv2S-IygE/using-datagridview-checkbox-column-as.html" title="Using DataGridView CheckBox Column as RadioButton (OptionButton)" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_HfnUI4smnYA/SL-Mj1VS48I/AAAAAAAAAE8/4nvEl5LJR9E/s72-c/DGVCheckBoxAsRadioButtonForm.jpg" height="72" width="72" /><thr:total>12</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/09/using-datagridview-checkbox-column-as.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A04FRHo9eCp7ImA9WxRUE04.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-1510277354323481896</id><published>2008-09-03T11:42:00.004+06:00</published><updated>2008-11-22T12:45:15.460+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-11-22T12:45:15.460+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Threading" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Intermediate Topics" /><category scheme="http://www.blogger.com/atom/ns#" term="BackgroundWorker" /><category scheme="http://www.blogger.com/atom/ns#" term="Asynchronous Working" /><title>Asynchronous Data Loading using TableAdapter with Cancel Feature</title><content type="html">&lt;span style="font-weight:bold;"&gt;Level:&lt;/span&gt; Intermediate&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Knowledge Required:&lt;/span&gt;&lt;ul&gt;&lt;li&gt;BackgroundWorker Control&lt;/li&gt;&lt;li&gt;DataGridView Control&lt;/li&gt;&lt;li&gt;Data Binding&lt;/li&gt;&lt;li&gt;TableAdapter&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight:bold;"&gt;Description:&lt;/span&gt;&lt;br /&gt;In my previous post&lt;br /&gt;&lt;br /&gt;&lt;a href="http://arsalantamiz.blogspot.com/2008/07/implement-sqlcommand-cancel-with.html"&gt;Implement SQLCommand Cancel with Threading&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I have discussed how to implement the SQLCommand Cancel using Threading. In this post we shall see, how we can implement the same feature in a TableAdapter.&lt;br /&gt;&lt;br /&gt;As we know that TableAdapter is used as a middle level man standing between Physical Database and Typed DataSet. Which gets the data from Physical Database and loads into Typed DataSet. Sometimes this loading of data takes time (for example due to complex query). Therefore we can provide the user an ability to cancel the on going command by clicking on Cancel Button.&lt;br /&gt;&lt;br /&gt;I have created a Test Database (TestDB) and have added a table (tbl_Student). Also I have added a Stored Procedure (GetStudent). Take a look at this Stored Procedure:&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;CREATE PROCEDURE&lt;/font&gt; [dbo].[GetStudent] &lt;br /&gt;&lt;font color=blue&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;    SET NOCOUNT ON&lt;/font&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;font color=green&gt;-- here we are simulating a very long query&lt;br /&gt;    -- which takes 3 seconds to complete&lt;/font&gt;&lt;br /&gt;    &lt;font color=blue&gt;WAITFOR DELAY&lt;/font&gt; &lt;font color=darkred&gt;'00:00:03'&lt;/font&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;font color=blue&gt;SELECT&lt;/font&gt; *&lt;br /&gt;    &lt;font color=blue&gt;FROM&lt;/font&gt; tbl_Student;&lt;br /&gt;&lt;font color=blue&gt;END&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In this procedure I have added a &lt;span style="font-weight:bold;"&gt;WAITFOR DELAY&lt;/span&gt; SQL Statement. Which simulates a long query that takes time 3 seconds. We will call this procedure from our TableAdapter to fill the DataTable.&lt;br /&gt;&lt;br /&gt;Now we will add the SQLCommand Cancel stuff in our TableAdapter. This can be achieved by creating a Partial Class of TableAdapter,&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;Namespace&lt;/font&gt; TestDataSetTableAdapters&lt;br /&gt;    &lt;font color=blue&gt;Partial Class&lt;/font&gt; StudentTableAdapter&lt;br /&gt;        &lt;font color=blue&gt;Public Sub&lt;/font&gt; CancelSelect()&lt;br /&gt;            &lt;font color=blue&gt;Me&lt;/font&gt;.Adapter.SelectCommand.Cancel()&lt;br /&gt;        &lt;font color=blue&gt;End Sub&lt;br /&gt;    End Class&lt;br /&gt;End Namespace&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As you see I have given the name of method &lt;span style="font-weight:bold;"&gt;CancelSelect&lt;/span&gt; which actually cancels the SelectCommand of Adapter. Note that I haven’t implemented any checking here (for example is command executing or NOT). This is upto the user of TableAdapter class. He/She will be responsible to check before executing this method.&lt;br /&gt;&lt;br /&gt;Here we have done with our TableAdapter. Now next thing is to implement the loading in the different Thread. For this purpose I have used the BackgroundWorker control which is quite simple. The overall idea is,&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Create 2 buttons&lt;/li&gt;&lt;ul&gt;&lt;li&gt;LoadButton&lt;/li&gt;&lt;li&gt;CancelButton&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;When LoadButton is clicked&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Execute the BackgroundWorker&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;In BackgroundWorker DoWork Event Handler&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Note somewhere that Execution is started&lt;/li&gt;&lt;li&gt;Execute the Fill() Method of TableAdapter&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;When CancelButton is clicked&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Check if execution is started then&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Execute the CancelSelect() method of TableAdapter&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Update Sep-4-2008:&lt;/span&gt;&lt;br /&gt;Here is the code of &lt;span style="font-weight:bold;"&gt;Form1&lt;/span&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;Public&lt;/font&gt; &lt;font color=blue&gt;Class&lt;/font&gt; Form1&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' ErrorText will contain the error information if occurred&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' while loading data in different thread&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; _ErrorText &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;String&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' this boolean variable will be true when command is executed&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; _CommandExecuted &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Boolean&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; btnLoad_Click(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; System.&lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.EventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; btnLoad.Click&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' first we will disable the Load button so it should NOT be clicked again&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.btnLoad.Enabled = &lt;font color=blue&gt;False&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' then we will enable the Cancel button so the command can be canceled&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.btnCancel.Enabled = &lt;font color=blue&gt;True&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' set the status text&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Call&lt;/font&gt; &lt;font color=blue&gt;Me&lt;/font&gt;.SetStatusText(&lt;font color=darkred&gt;&amp;quot;Loading...&amp;quot;&lt;/font&gt;)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' clear the Error Text&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;._ErrorText = &lt;font color=darkred&gt;&amp;quot;&amp;quot;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' here we have first clear the currently loaded data&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' from DataSet, because the Adapter will fill the DataSet&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' in different thread, therefore it will create problems&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' while displaying the same data (which is being modified&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' in different thread) in DataGridView&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.TestDataSet.Clear()&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' start the asynchronous task&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.BackgroundWorker1.RunWorkerAsync()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; BackgroundWorker1_DoWork(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.ComponentModel.DoWorkEventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; BackgroundWorker1.DoWork&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Try&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' first set the executed flag&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;._CommandExecuted = &lt;font color=blue&gt;True&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' execute the command&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.StudentTableAdapter.Fill(&lt;font color=blue&gt;Me&lt;/font&gt;.TestDataSet.Student)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Catch&lt;/font&gt; ex &lt;font color=blue&gt;As&lt;/font&gt; Exception&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_ErrorText = ex.Message&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Try&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; BackgroundWorker1_RunWorkerCompleted(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.ComponentModel.RunWorkerCompletedEventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; BackgroundWorker1.RunWorkerCompleted&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' ok our data is loaded now&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' so enable the Load button&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.btnLoad.Enabled = &lt;font color=blue&gt;True&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' disable the Cancel button&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.btnCancel.Enabled = &lt;font color=blue&gt;False&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' if NO error was occurred in thread&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; &lt;font color=blue&gt;Me&lt;/font&gt;._ErrorText = &lt;font color=darkred&gt;&amp;quot;&amp;quot;&lt;/font&gt; &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.SetStatusText(&lt;font color=blue&gt;Me&lt;/font&gt;.TestDataSet.Student.Count &amp;amp; &lt;font color=darkred&gt;&amp;quot; Records loaded&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Else&lt;/font&gt; &lt;font color=green&gt;' else (some error was occurred)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.SetStatusText(&lt;font color=blue&gt;Me&lt;/font&gt;._ErrorText)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' since data is changed in different thread&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' therefore by reseting the Bindings the DataGridView&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' will refresh itself and will display all the data&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.StudentBindingSource.ResetBindings(&lt;font color=blue&gt;False&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; btnCancel_Click(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; System.&lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.EventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; btnCancel.Click&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' if command is executed then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; &lt;font color=blue&gt;Me&lt;/font&gt;._CommandExecuted &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' cancel it&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.StudentTableAdapter.CancelSelect()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; SetStatusText(&lt;font color=blue&gt;ByVal&lt;/font&gt; sText &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;String&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.lblStatus.Text = sText&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Class&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Download:&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://cid-c23746227866488d.skydrive.live.com/self.aspx/Public/TableAdapterWithCancel.zip"&gt;TableAdapterWithCancel.zip&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Note: The zip file contains &lt;strong&gt;TestDBScript.sql&lt;/strong&gt; T-SQL Script which will,&lt;br /&gt;1) Create a database "TestDB"&lt;br /&gt;2) Create a table "tbl_Student" in that database&lt;br /&gt;3) Create a Stored Procedure "GetStudent"&lt;br /&gt;4) Put some data in tbl_Student&lt;br /&gt;&lt;br /&gt;Please execute this script before running the project.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-1510277354323481896?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/PSb0kz2jOeFNfwN4jJs4WEJwGLg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/PSb0kz2jOeFNfwN4jJs4WEJwGLg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/PSb0kz2jOeFNfwN4jJs4WEJwGLg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/PSb0kz2jOeFNfwN4jJs4WEJwGLg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/cQvjkmXiWi4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/1510277354323481896/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=1510277354323481896" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/1510277354323481896?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/1510277354323481896?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/cQvjkmXiWi4/asynchronous-data-loading-using.html" title="Asynchronous Data Loading using TableAdapter with Cancel Feature" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>1</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/09/asynchronous-data-loading-using.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ENQnc6cCp7ImA9WxRQE0g.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-9147324614437333336</id><published>2008-08-12T13:02:00.000+06:00</published><updated>2008-10-07T12:08:13.918+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-07T12:08:13.918+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="ADO.net" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005" /><title>Query Timeout in ADO.Net but NOT in SQL Server Management Studio</title><content type="html">I was facing this issue for a while that I have created a Stored Procedure in SQL Server 2005 Database that performs a search on a table (according to the given parameters). It was happening that when this procedure was executed from ADO.net (my front-end Visual Basic Application) Timeout exception was occurring. But at the same time when we execute this procedure from SQL Server Management Studio it executes immediately.&lt;br /&gt;&lt;br /&gt;The Timeout in Visual Basic Application was NOT coming immediately it was coming after some time when users execute the procedure many times. Whenever time out occurred, I went in the SQL Server Management Studio and just did ALTER (without changing a single thing in procedure). And the problem resolved for some time NOT permanently.&lt;br /&gt;&lt;br /&gt;So I put this question on MSDN Forums and a member told me to put the &lt;strong&gt;WITH RECOMPILE&lt;/strong&gt; in the procedure. He told me that&lt;br /&gt;&lt;br /&gt;"I think the problem is with the cache. When you ALTER the procedure from Management Studio it is recompiled and the problem is fixed. this is my guess."&lt;br /&gt;&lt;br /&gt;It sure fixed my problem and the Timeout never came afterwords. Here is the link to that post.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3068506&amp;SiteID=1&amp;pageid=0"&gt;http://forums.microsoft.com/MSDN/ShowPost.aspx...&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-9147324614437333336?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/xGxgCcVvN5xcHYme_tZhiCzn3Vk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xGxgCcVvN5xcHYme_tZhiCzn3Vk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/xGxgCcVvN5xcHYme_tZhiCzn3Vk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xGxgCcVvN5xcHYme_tZhiCzn3Vk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/RXiDt_y17pE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/9147324614437333336/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=9147324614437333336" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/9147324614437333336?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/9147324614437333336?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/RXiDt_y17pE/query-timeout-in-adonet-but-not-in-sql.html" title="Query Timeout in ADO.Net but NOT in SQL Server Management Studio" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>4</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/08/query-timeout-in-adonet-but-not-in-sql.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ENQnY8fip7ImA9WxRQE0g.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-3164835046726435217</id><published>2008-07-30T12:11:00.006+06:00</published><updated>2008-10-07T12:08:13.876+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-07T12:08:13.876+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Beginner Topics" /><category scheme="http://www.blogger.com/atom/ns#" term="ADO.net" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><title>Another look at Exception Handling</title><content type="html">&lt;strong&gt;Level:&lt;/strong&gt; Beginner&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;ul&gt;&lt;li&gt;Exception Handling&lt;/li&gt;&lt;li&gt;ADO.net&lt;/li&gt;&lt;/ul&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;As we have used the Exception Handling (Try...Catch) many times. But in this post we shall understand an important aspect. Consider the following code,&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; InsertSomeData()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; conn &lt;font color=blue&gt;As&lt;/font&gt; SqlClient.SqlConnection = &lt;font color=blue&gt;Nothing&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; cmd &lt;font color=blue&gt;As&lt;/font&gt; SqlClient.SqlCommand&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; tran &lt;font color=blue&gt;As&lt;/font&gt; SqlClient.SqlTransaction = &lt;font color=blue&gt;Nothing&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Try&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' initialize connection&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;conn = &lt;font color=blue&gt;New&lt;/font&gt; SqlClient.SqlConnection(&lt;font color=darkred&gt;&amp;quot;Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' open connection&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;conn.Open()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' begin transaction&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tran = conn.BeginTransaction&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' create a Command&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cmd = conn.CreateCommand()&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' setup command to insert data in 1st table&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cmd.CommandText = &lt;font color=darkred&gt;&amp;quot;INSERT INTO MyTable1(Column1) VALUES('Data in MyTable1.Column1')&amp;quot;&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cmd.ExecuteNonQuery()&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' setup command to insert data in 2nd table&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cmd.CommandText = &lt;font color=darkred&gt;&amp;quot;INSERT INTO MyTable2(Column1) VALUES('Data in MyTable2.Column1')&amp;quot;&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cmd.ExecuteNonQuery()&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' ok now commit the changes&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tran.Commit()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Catch&lt;/font&gt; ex &lt;font color=blue&gt;As&lt;/font&gt; Exception&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' an exception is occurred&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' check if transaction has been initialized then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp&lt;font color=green&gt;' rollback&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; tran &lt;font color=blue&gt;IsNot&lt;/font&gt; &lt;font color=blue&gt;Nothing&lt;/font&gt; &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tran.Rollback()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' again throw the exception&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' this is because we want the caller to be notified&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Throw&lt;/font&gt; ex&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Finally&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' finally block will be executed anyway&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' whether the exception is occurred or NOT&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' ================---------------------&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' Note: we are again throwing the exception in Catch block &lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; but still the following code will be executed&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; conn &lt;font color=blue&gt;IsNot&lt;/font&gt; &lt;font color=blue&gt;Nothing&lt;/font&gt; &lt;font color=blue&gt;AndAlso&lt;/font&gt; _&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; conn.State = ConnectionState.Open &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' if connection was established then close it&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;conn.Close()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' =====================-------------------------&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Try&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' this line however will NOT be executed if&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=green&gt;' exception is occurred&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Debug.Print(&lt;font color=darkred&gt;&amp;quot;Outside the Try..Catch block&amp;quot;&lt;/font&gt;)&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;As you can see this method will be inserting data in 2 tables. Our goal is,&lt;br /&gt;&lt;br /&gt;Begin the Transaction&lt;br /&gt;Try to Insert the Data in Tables&lt;br /&gt;If no error occurs on Insertion then Commit the Transaction&lt;br /&gt;If error occurred then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Rollback the Transaction&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Again throw the Exception (so the caller is being notified)&lt;br /&gt;End If&lt;br /&gt;At the end (whether exception is occred or NOT) Close the connection&lt;br /&gt;&lt;br /&gt;&lt;div style="background-color:lightyellow;"&gt;Note that if Exception occurs then in &lt;strong&gt;Catch block&lt;/strong&gt; we are &lt;strong&gt;throwing&lt;/strong&gt; the same exception again. At this point it seems that control will immediately &lt;strong&gt;exit&lt;/strong&gt; from this method, but fortunately this does NOT happen in this way. Instead control first jumps to &lt;strong&gt;Finally block&lt;/strong&gt; then after that it exits from method.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-3164835046726435217?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Q0mMzrkCsXbIngqBbKAq7puGR_E/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Q0mMzrkCsXbIngqBbKAq7puGR_E/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Q0mMzrkCsXbIngqBbKAq7puGR_E/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Q0mMzrkCsXbIngqBbKAq7puGR_E/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/C62oqT--emE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/3164835046726435217/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=3164835046726435217" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/3164835046726435217?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/3164835046726435217?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/C62oqT--emE/another-look-at-exception-handling.html" title="Another look at Exception Handling" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/07/another-look-at-exception-handling.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ENQnYzfCp7ImA9WxRQE0g.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-5388667664678789958</id><published>2008-07-30T10:58:00.005+06:00</published><updated>2008-10-07T12:08:13.884+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-07T12:08:13.884+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Win Forms" /><category scheme="http://www.blogger.com/atom/ns#" term="Beginner Topics" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><title>Enter Key to Set Focus in Next Control (for Data Entry Forms)</title><content type="html">&lt;strong&gt;Level:&lt;/strong&gt; Beginner&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;br /&gt;Windows Forms&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;In a Windows Application, we use &lt;strong&gt;Tab&lt;/strong&gt; key to move Focus from current control to next control, but in a &lt;strong&gt;Data Entry&lt;/strong&gt; applications, usually &lt;strong&gt;Enter Key&lt;/strong&gt; is used for this purpose.&lt;br /&gt;&lt;br /&gt;There are different techniques to accomplish this. I think the following is the simplest one.&lt;br /&gt;&lt;br /&gt;First set the Form's &lt;strong&gt;KeyPreview&lt;/strong&gt; property to True. Then put the following code in the KeyPress Event Handler:&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; Form1_KeyPress(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; &lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.Windows.Forms.KeyPressEventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; &lt;font color=blue&gt;Me&lt;/font&gt;.KeyPress&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; e.KeyChar = Chr(Keys.Return) &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Me&lt;/font&gt;.SelectNextControl(&lt;font color=blue&gt;Me&lt;/font&gt;.ActiveControl, &lt;font color=blue&gt;True&lt;/font&gt;, &lt;font color=blue&gt;True&lt;/font&gt;, &lt;font color=blue&gt;True&lt;/font&gt;, &lt;font color=blue&gt;True&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-5388667664678789958?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/54PhN43MeClJwuR0w2nhwpfYjso/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/54PhN43MeClJwuR0w2nhwpfYjso/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/54PhN43MeClJwuR0w2nhwpfYjso/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/54PhN43MeClJwuR0w2nhwpfYjso/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/_2xcVCqwBRo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/5388667664678789958/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=5388667664678789958" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/5388667664678789958?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/5388667664678789958?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/_2xcVCqwBRo/enter-key-to-set-focus-in-next-control.html" title="Enter Key to Set Focus in Next Control (for Data Entry Forms)" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>1</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/07/enter-key-to-set-focus-in-next-control.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A08MRnc8eip7ImA9WxRUE04.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-1480049484326053588</id><published>2008-07-29T13:30:00.010+06:00</published><updated>2008-11-22T12:44:47.972+05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-11-22T12:44:47.972+05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Threading" /><category scheme="http://www.blogger.com/atom/ns#" term="ADO.net" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Advanced Topics" /><category scheme="http://www.blogger.com/atom/ns#" term="Asynchronous Working" /><title>Implement SQLCommand Cancel with Threading</title><content type="html">&lt;strong&gt;Level:&lt;/strong&gt; Advanced&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;ul&gt;&lt;li&gt;ADO.net&lt;/li&gt;&lt;li&gt;Threading&lt;/li&gt;&lt;/ul&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;In a data driven application, sometimes we built queries which take time. In these type of scenarios usually developers put the query execution on different thread, so the User Interface keep responsive.&lt;br /&gt;&lt;br /&gt;A more user friendly applications provide a flexibility to cancel the currently executing command. This can be achieved by using &lt;strong&gt;SQLCommand.Cancel()&lt;/strong&gt; method.&lt;br /&gt;&lt;br /&gt;So to implement it, &lt;ul&gt;&lt;li&gt;Create a Thread&lt;/li&gt;&lt;li&gt;Execute Query in that Thread&lt;/li&gt;&lt;li&gt;Meanwhile if user clicks the Cancel button we will call the SQLCommand.Cancel method&lt;/li&gt;&lt;/ul&gt;Cancel method actually &lt;strong&gt;tries&lt;/strong&gt; to cancel the in-process query. When the attempt to cancel the query succeeds then an exception is occured at the same point where Command was executed i.e. SQLCommand.ExecuteReader (or other Execute method) was called. When the cancellation is failed then no exception occurs and command continues its execution. Therefore we also need to handle the Exception at the same point where we have executed the query. Here is an example:&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; DoSomeWork()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;objCon = &lt;font color=blue&gt;New&lt;/font&gt; SqlConnection(&lt;font color=darkred&gt;&amp;quot;Data Source=.;Integrated Security=True&amp;quot;&lt;/font&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;objCmd = &lt;font color=blue&gt;New&lt;/font&gt; SqlCommand(&lt;font color=darkred&gt;&amp;quot;WAITFOR DELAY '00:00:05';&amp;quot;&lt;/font&gt;, objCon)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;objCon.Open()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Try&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;objCmd.ExecuteReader()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Catch&lt;/font&gt; ex &lt;font color=blue&gt;As&lt;/font&gt; SqlException&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Debug.Print(ex.Message)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Try&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;objCon.Close()&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;The above method just executes a &lt;strong&gt;WAITFOR DELAY&lt;/strong&gt; query which simulates a very long query that takes 5 seconds to complete. Note that objCon and objCmd are Module Level variables. This method will be executed in a different thread using the following code,&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;Dim&lt;/font&gt; t &lt;font color=blue&gt;As&lt;/font&gt; Threading.Thread&lt;br /&gt;t = &lt;font color=blue&gt;New&lt;/font&gt; Threading.Thread(&lt;font color=blue&gt;AddressOf&lt;/font&gt; DoSomeWork)&lt;br /&gt;t.Start()&lt;/pre&gt;&lt;br /&gt;While this query is being executed we will perform a query cancel on a button click event as,&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; CancelButton_Click(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; System.&lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.EventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; btnCancel.Click&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;If&lt;/font&gt; objCmd &lt;font color=blue&gt;IsNot&lt;/font&gt; &lt;font color=blue&gt;Nothing&lt;/font&gt; &lt;font color=blue&gt;Then&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;objCmd.Cancel()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;If&lt;/font&gt;&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;See Also:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://arsalantamiz.blogspot.com/2008/09/asynchronous-data-loading-using.html"&gt;Asynchronous Data Loading using TableAdapter with Cancel Feature&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-1480049484326053588?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/QI3UIUUrDRfQFwCbfAvJuLrPdmM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/QI3UIUUrDRfQFwCbfAvJuLrPdmM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/QI3UIUUrDRfQFwCbfAvJuLrPdmM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/QI3UIUUrDRfQFwCbfAvJuLrPdmM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/8atsROG__n0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/1480049484326053588/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=1480049484326053588" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/1480049484326053588?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/1480049484326053588?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/8atsROG__n0/implement-sqlcommand-cancel-with.html" title="Implement SQLCommand Cancel with Threading" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>3</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/07/implement-sqlcommand-cancel-with.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ENQnY8fyp7ImA9WxRQE0g.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-4150134718809822743</id><published>2008-07-28T23:32:00.003+06:00</published><updated>2008-10-07T12:08:13.877+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-07T12:08:13.877+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Beginner Topics" /><category scheme="http://www.blogger.com/atom/ns#" term="Custom Controls" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><title>Draw Focus Rectangle using ControlPaint Class</title><content type="html">&lt;strong&gt;Level:&lt;/strong&gt; Beginner&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;br /&gt;GDI+&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;Sometimes while creating a User Control from scratch. It is required to display the Focus Rectangle (the dotted border). This can be easily achieved by using&lt;br /&gt;&lt;br /&gt;System.Windows.Forms.ControlPaint&lt;br /&gt;&lt;br /&gt;E.g.:&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;font color=blue&gt;Private&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt; Form1_Click(&lt;font color=blue&gt;ByVal&lt;/font&gt; sender &lt;font color=blue&gt;As&lt;/font&gt; System.&lt;font color=blue&gt;Object&lt;/font&gt;, &lt;font color=blue&gt;ByVal&lt;/font&gt; e &lt;font color=blue&gt;As&lt;/font&gt; System.EventArgs) &lt;font color=blue&gt;Handles&lt;/font&gt; &lt;font color=blue&gt;Me&lt;/font&gt;.Click&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=blue&gt;Dim&lt;/font&gt; g &lt;font color=blue&gt;As&lt;/font&gt; Graphics&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;g = &lt;font color=blue&gt;Me&lt;/font&gt;.CreateGraphics&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ControlPaint.DrawFocusRectangle(g, &lt;font color=blue&gt;New&lt;/font&gt; Rectangle(10, 10, 100, 100))&lt;br /&gt;&lt;font color=blue&gt;End&lt;/font&gt; &lt;font color=blue&gt;Sub&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;ControlPaint class contains other useful routines for control drawing you can explore the list using intellisense.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-4150134718809822743?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/9xA0ZOPkev7KzRTuIK2wSFfGvYM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9xA0ZOPkev7KzRTuIK2wSFfGvYM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/9xA0ZOPkev7KzRTuIK2wSFfGvYM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9xA0ZOPkev7KzRTuIK2wSFfGvYM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/v0Qtt9K2DQc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/4150134718809822743/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=4150134718809822743" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/4150134718809822743?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/4150134718809822743?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/v0Qtt9K2DQc/draw-focus-rectangle-using-controlpaint.html" title="Draw Focus Rectangle using ControlPaint Class" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>2</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/07/draw-focus-rectangle-using-controlpaint.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ENQnc6fip7ImA9WxRQE0g.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-7473703406743809882</id><published>2008-07-26T16:47:00.002+06:00</published><updated>2008-10-07T12:08:13.916+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-07T12:08:13.916+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Beginner Topics" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.net" /><category scheme="http://www.blogger.com/atom/ns#" term=".net 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="VB 2005" /><title>Dynamically Loading Data in TreeView control ASP.net</title><content type="html">&lt;strong&gt;Level:&lt;/strong&gt; Beginner&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;ul&gt;&lt;li&gt;ASP.net&lt;/li&gt;&lt;li&gt;Typed DataSet&lt;/li&gt;&lt;li&gt;TableAdapter&lt;/li&gt;&lt;li&gt;SQL Server 2005&lt;/li&gt;&lt;/ul&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;In my post &lt;a href="http://arsalantamiz.blogspot.com/2008/06/dynamically-loading-data-in-treeview.html"&gt;Dynamically Loading Data in TreeView Control&lt;/a&gt;, I have discussed how to load the data on runtime in TreeView control. This post is for Windows Application (Windows Forms) platform. Recently I was asked to have the same code for ASP.net Web Application. I think there are lots of articles available on Internet about this kind of stuff. But I am also submitting my own.&lt;br /&gt;&lt;br /&gt;This is a very simple project demonstrates how to dynamically load data when TreeView's Node is expanded.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Download:&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://cid-c23746227866488d.skydrive.live.com/self.aspx/Public/DynamicTreeview.zip"&gt;DynamicTreeview.zip&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Note: The zip file contains &lt;strong&gt;DynamicTreeViewDatabaseSetup.sql&lt;/strong&gt; T-SQL Script which will,&lt;br /&gt;1) Create a database "MyDatabase"&lt;br /&gt;2) Create a table "tbl_Category" in that database&lt;br /&gt;3) Create a Stored Procedure "CategorySelect"&lt;br /&gt;4) Put some data in tbl_Category&lt;br /&gt;&lt;br /&gt;Please execute this script before running the project.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-7473703406743809882?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/agnMF6BE_IobWSTOVnsUzrMw79Y/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/agnMF6BE_IobWSTOVnsUzrMw79Y/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/agnMF6BE_IobWSTOVnsUzrMw79Y/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/agnMF6BE_IobWSTOVnsUzrMw79Y/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/2envdTgnO5U" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/7473703406743809882/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=7473703406743809882" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/7473703406743809882?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/7473703406743809882?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/2envdTgnO5U/dynamically-loading-data-in-treeview.html" title="Dynamically Loading Data in TreeView control ASP.net" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>1</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/07/dynamically-loading-data-in-treeview.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ENQnYyeyp7ImA9WxRQE0g.&quot;"><id>tag:blogger.com,1999:blog-6837250508009744375.post-3227807731981346632</id><published>2008-07-19T15:08:00.007+06:00</published><updated>2008-10-07T12:08:13.893+06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-07T12:08:13.893+06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Useful Functions" /><category scheme="http://www.blogger.com/atom/ns#" term="T-SQL" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005" /><category scheme="http://www.blogger.com/atom/ns#" term="Intermediate Topics" /><title>Function to Trim any Character/String from Left of varchar in SQL Server</title><content type="html">An Extended Left Trim (LTRIM) Function for SQL Server which removes given string/character from Left of another varchar (String)&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Knowledge Required:&lt;/strong&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;T-SQL&lt;/li&gt;&lt;li&gt;SQL Server 2005&lt;/li&gt;&lt;li&gt;SQL Server User Define Functions&lt;/li&gt;&lt;/ul&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;The SQL Server's built-in &lt;strong&gt;LTrim()&lt;/strong&gt; function removes &lt;strong&gt;only&lt;/strong&gt; the blanks (spaces) from Left of a varchar (string). Here is a User Defined function which can remove &lt;strong&gt;any&lt;/strong&gt; character.&lt;br /&gt;&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;span style="color:blue;"&gt;CREATE FUNCTION&lt;/span&gt; [dbo].[LTrimString]&lt;br /&gt;(&lt;br /&gt; &lt;span style="color:green;"&gt;-- Add the parameters for the function here&lt;/span&gt;&lt;br /&gt; @StringToTrim &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;(&lt;span style="color:magenta;"&gt;max&lt;/span&gt;),&lt;br /&gt; @CharToTrim &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;(10)&lt;br /&gt;)&lt;br /&gt;&lt;span style="color:blue;"&gt;RETURNS varchar&lt;/span&gt;(&lt;span style="color:magenta;"&gt;max&lt;/span&gt;)&lt;br /&gt;&lt;span style="color:blue;"&gt;AS&lt;br /&gt;BEGIN&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:green;"&gt;-- Declare the return variable here&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:blue;"&gt;DECLARE&lt;/span&gt; @Result &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;(&lt;span style="color:magenta;"&gt;max&lt;/span&gt;);&lt;br /&gt;    &lt;span style="color:blue;"&gt;DECLARE&lt;/span&gt; @i &lt;span style="color:blue;"&gt;int&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color:blue;"&gt;DECLARE&lt;/span&gt; @l &lt;span style="color:blue;"&gt;int&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color:blue;"&gt;DECLARE&lt;/span&gt; @sl &lt;span style="color:blue;"&gt;int&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:blue;"&gt;SET&lt;/span&gt; @i = 1;&lt;br /&gt;    &lt;span style="color:blue;"&gt;SET&lt;/span&gt; @l = &lt;span style="color:magenta;"&gt;Len&lt;/span&gt;(@StringToTrim);&lt;br /&gt;    &lt;span style="color:blue;"&gt;SET&lt;/span&gt; @sl = &lt;span style="color:magenta;"&gt;Len&lt;/span&gt;(@CharToTrim);&lt;br /&gt;    &lt;span style="color:blue;"&gt;While&lt;/span&gt; @i &lt; @l&lt;br /&gt;    &lt;span style="color:blue;"&gt;Begin  &lt;br /&gt;            If&lt;/span&gt; SubString(@StringToTrim, @i, @sl) &lt;&gt; @CharToTrim&lt;br /&gt;            &lt;span style="color:blue;"&gt;Begin&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:blue;"&gt;SET&lt;/span&gt; @Result = &lt;span style="color:magenta;"&gt;SubString&lt;/span&gt;(@StringToTrim, @i, @l - @i + 1);&lt;br /&gt;                &lt;span style="color:blue;"&gt;Break&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color:blue;"&gt;End&lt;br /&gt;        SET&lt;/span&gt; @i = @i + @sl;&lt;br /&gt;    &lt;span style="color:blue;"&gt;End&lt;br /&gt;&lt;br /&gt;    SET&lt;/span&gt; @Result = &lt;span style="color:magenta;"&gt;IsNull&lt;/span&gt;(@Result, @StringToTrim);&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:green;"&gt;-- Return the result of the function&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:blue;"&gt;RETURN&lt;/span&gt; @Result;&lt;br /&gt;&lt;span style="color:blue;"&gt;END&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Usage:&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;&lt;span style="color:blue;"&gt;Print&lt;/span&gt; dbo.LTrimString(&lt;span style="color:red;"&gt;'00000005221'&lt;/span&gt;, &lt;span style="color:red;"&gt;'0'&lt;/span&gt;)&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre style="padding:1em 1em 1em 1em; overflow: auto; color: black;font-family:Monospace,Courier New,Courier;font-size:85%;background-color:whitesmoke;"&gt;5221&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6837250508009744375-3227807731981346632?l=arsalantamiz.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/EdDkeQXidk_hX6LWoTQ2JoVX-MY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/EdDkeQXidk_hX6LWoTQ2JoVX-MY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/EdDkeQXidk_hX6LWoTQ2JoVX-MY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/EdDkeQXidk_hX6LWoTQ2JoVX-MY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/VisualBasicDevelopmentIssues/~4/3p5S8XyVEZ4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://arsalantamiz.blogspot.com/feeds/3227807731981346632/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=6837250508009744375&amp;postID=3227807731981346632" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/3227807731981346632?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6837250508009744375/posts/default/3227807731981346632?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/VisualBasicDevelopmentIssues/~3/3p5S8XyVEZ4/function-to-trim-any-characterstring.html" title="Function to Trim any Character/String from Left of varchar in SQL Server" /><author><name>Arsalan Tamiz</name><uri>http://www.blogger.com/profile/03883319971948565803</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://arsalantamiz.blogspot.com/2008/07/function-to-trim-any-characterstring.html</feedburner:origLink></entry></feed>

