tag:blogger.com,1999:blog-54665239199811603552009-02-21T04:39:54.975+05:00ASP .NET Hacks, Tips and TricksProvides ASP .NET tutorials, tips and tricks.Rafay Bin Alinoreply@blogger.comBlogger14125tag:blogger.com,1999:blog-5466523919981160355.post-15910346138452712892009-01-10T04:05:00.000+05:002009-01-10T04:06:07.302+05:00Get Google Search Results in RSS Format<p>Here is my attempt to transform Google Search Results into a RSS feed. This functionality is missing from Google. Test it out and provide some feedback. I have published the feed as a web service at</p><br /><p><a href="http://www.ecubicle.net/gsearch_rss.asmx" title="http://www.ecubicle.net/gsearch_rss.asmx" mce_href="http://www.ecubicle.net/gsearch_rss.asmx">http://www.ecubicle.net/gsearch_rss.asmx</a></p><br /><p>There is a single function called GetSearchResults with the following signature</p><br /><p><b>GetSearchResults(byval searchPage As string, byval gQuery As String, byval numOfResults As String)</b></p><br /><ul><br /><li>searchPage provides the google search page that runs in multiples of 10 with 10 results each page (default on Google). The value for first page is 0</li><br /><li>gQuery is google query. You can use any set of Google operators here to tune the query</li><br /><li>numOfResults is the number of results to return per page. Default is 15.</li><br /></ul><br /><p>The returned XML is RSS 2.0 compliant. As such, any feed reader can also read the returned XML document.</p><br /><p> </p><br /><p><font size="4"><b>Using in a Feed Reader</b></font> </p><br /><p>To call this in a feedreader, here is the GET syntax for the web service</p><br /><p><a href="http://www.ecubicle.net/gsearch_rss.asmx/GetSearchResults?searchPage=0&gQuery=tutorial+asp+net&numOfResults=15" title="http://www.ecubicle.net/gsearch_rss.asmx/GetSearchResults?searchPage=0&gQuery=tutorial+asp+net&numOfResults=15" mce_href="http://www.ecubicle.net/gsearch_rss.asmx/GetSearchResults?searchPage=0&gQuery=tutorial+asp+net&numOfResults=15">http://www.ecubicle.net/gsearch_rss.asmx/GetSearchResults?searchPage=<b><font color="#990000">0</font></b>&gQuery=<font color="#990000"><b>tutorial+asp+net</b></font>&numOfResults=<font color="#990000"><b></b></font></a><font color="#990000"><b><a>15</a></b></font></p><br /><p>In the above, replace the parameter values shown in red with the your query values.</p><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-1591034613845271289?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-81951734660288091582009-01-05T10:21:00.003+05:002009-01-05T10:29:04.953+05:00Read Google Maps KML using LINQ in ASP .NET to Show Driving DirectionsWe can use LINQ to query the Google Maps KML File. The magic piece is the Google Maps URL to retrieve KML.<br /><br /><strong>http://maps.google.ca/?q=From 25 King St. West, Toronto, ON to Niagara Falls, Ontario&output=kml</strong><br /><br /><div style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; OVERFLOW: scroll; BORDER-LEFT: gray 1px solid; BORDER-BOTTOM: gray 1px solid"><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color:#0000ff;">Dim</span><span style="color:#000000;"> fromAddress </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">25 King St. West, Toronto, ON</span><span style="color:#800000;">"</span><span style="color:#000000;"><br /></span><span style="color:#0000ff;">Dim</span><span style="color:#000000;"> toAddress </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">Niagara Falls, ON</span><span style="color:#800000;">"</span><span style="color:#000000;"><br /></span><span style="color:#0000ff;">Dim</span><span style="color:#000000;"> urlString </span><span style="color:#0000ff;">As</span><span style="color:#000000;"> </span><span style="color:#0000ff;">String</span><span style="color:#000000;"> </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">http://maps.google.ca/?q=From </span><span style="color:#800000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">&</span><span style="color:#000000;"> fromAddress </span><span style="color:#000000;">&</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;"> to </span><span style="color:#800000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">&</span><span style="color:#000000;"> toAddress </span><span style="color:#000000;">&</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">&output=kml&view=text</span><span style="color:#800000;">"</span><span style="color:#000000;"><br /></span><span style="color:#0000ff;">Dim</span><span style="color:#000000;"> xmlDataSource2 </span><span style="color:#0000ff;">As</span><span style="color:#000000;"> </span><span style="color:#0000ff;">New</span><span style="color:#000000;"> XmlDataSource()<br />xmlDataSource2.DataFile </span><span style="color:#000000;">=</span><span style="color:#000000;"> urlString<br /><br /></span><span style="color:#0000ff;">Dim</span><span style="color:#000000;"> xdoc </span><span style="color:#0000ff;">As</span><span style="color:#000000;"> XDocument </span><span style="color:#000000;">=</span><span style="color:#000000;"> XDocument.Parse(xmlDataSource2.GetXmlDocument().InnerXml)<br /></span><span style="color:#0000ff;">Dim</span><span style="color:#000000;"> ns </span><span style="color:#0000ff;">As</span><span style="color:#000000;"> XNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> XNamespace.Get(</span><span style="color:#800000;">"</span><span style="color:#800000;">http://earth.google.com/kml/2.0</span><span style="color:#800000;">"</span><span style="color:#000000;">)<br /></span><span style="color:#0000ff;">Dim</span><span style="color:#000000;"> ele </span><span style="color:#000000;">=</span><span style="color:#000000;"> xdoc.Element(ns </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">kml</span><span style="color:#800000;">"</span><span style="color:#000000;">).Element(ns </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">Document</span><span style="color:#800000;">"</span><span style="color:#000000;">).Elements(ns </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">Placemark</span><span style="color:#800000;">"</span><span style="color:#000000;">).Elements(ns </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">name</span><span style="color:#800000;">"</span><span style="color:#000000;">)<br /><br /><br /></span><span style="color:#000000;">GridView1.DataSource </span><span style="color:#000000;">=</span><span style="color:#000000;"> ele.Take(ele.Count </span><span style="color:#000000;">-</span><span style="color:#000000;"> </span><span style="color:#800080;">1</span><span style="color:#000000;">)<br />GridView1.DataBind()<br /></span></div><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-8195173466028809158?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-8382280361157311952009-01-05T02:40:00.002+05:002009-01-05T02:43:03.751+05:00Youtube Downloading - Web ServiceHere is an interesting web service that allows applications to download youtube videos. Applications that require interaction with youtube can obviously use the YouTube API's to provide exhaustive youtube functionality right from their web sites. However, the only API function that is missed is the ability to download from Youtube.<br /><br />The Youtube web service provides this missing functionality.<br /><br />The service is located at http://www.ecubicle.net/youtubedownloader.asmx<br /><br />What the service does is provide the download URL for any video on Youtube. .NET applications can then use any method to access the download URL and download the file. For example, WebClient.DownloadFile() method can be used to download the file directly if the returned URL is passed to DownloadFile.<br /><br />Very interesting.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-838228036115731195?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-84602523726287968582008-07-10T04:15:00.001+06:002008-07-10T04:15:19.406+06:00Command Window in Visual Studio 2008<p align="justify">As we develop applications in a particular IDE, it becomes imperative that we come to terms with the technical details of the IDE itself. Features like shortcuts can prove big time saves when working in huge projects. In this post, I want to discover the command window feature in Visual Studio 2005 and 2008. </p> <p align="justify">Command window allows working with the IDE using commands, instead of using the mouse and the IDE menu. Some times it is just quicker to pop open the command window and perform routine tasks, such as opening and saving files, creating aliases for other windows, reading and setting the value of a variable during debugging. </p> <blockquote> <p align="justify">Once in a while my IDE would get messed up and I would lose my solution explorer. By having an alias for it, I simply open up the command window and get straight to any window that I need. Now, solution explorer does have its shortcut (<strong>Ctrl-Alt-L</strong>), but I am not very keen about these "3keyed shortcuts". For those of us that are used to the more traditional Dos Prompt Style, command window makes us feel right at home with its command features. </p> </blockquote> <p align="justify">Command window can be accessed using the shortcut <strong>Ctrl-Alt-A.</strong></p> <p align="justify">Lets get down and dirty and see some useful features that have helped me. I am sure you would find them useful as well.</p> <h4><strong>Creating Aliases</strong></h4> <p>Aliases allow us to define a shorter name for a command. So, for example, we can define an alias to open up Server Explorer using the alias syntax like this:</p> <p><strong><font color="#800000">alias se View.ServerExplorer</font></strong></p> <p><a href="http://lh3.ggpht.com/rafayali/SHU4bRwwuSI/AAAAAAAAACQ/0z9cqj1IZYY/s1600-h/alias1%5B41%5D.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="alias1" src="http://lh4.ggpht.com/rafayali/SHU4cabxt9I/AAAAAAAAACU/aTPPxjrIK14/alias1_thumb%5B37%5D.jpg?imgmax=800" width="287" height="162" /></a> </p> <p> Now, lets have a look at our created alias. Simply type alias and press Enter to see a list of all aliases defined in the environment.</p> <p><a href="http://lh3.ggpht.com/rafayali/SHU4cxKisFI/AAAAAAAAACY/8MQaVOYAF0E/s1600-h/alias2%5B13%5D.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" border="0" alt="alias2" src="http://lh3.ggpht.com/rafayali/SHU4dekEUmI/AAAAAAAAACc/JKR8JohK3AU/alias2_thumb%5B9%5D.jpg?imgmax=800" width="341" height="447" /></a> </p> <p></p> <p>The alias command gives us a list as show in the image above. Notice our newly created alias highlighted. </p> <p>To run this alias, simply type se on an empty line in the command window and the Server Explorer opens up for us. </p> <p>To delete an alias, type in: <font color="#800000"><strong>alias se /delete</strong> </font><font color="#000000">as shown below.</font></p> <p><a href="http://lh4.ggpht.com/rafayali/SHU4dj_IC7I/AAAAAAAAACg/7-_2pA9UH9k/s1600-h/alias3%5B6%5D.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" border="0" alt="alias3" src="http://lh3.ggpht.com/rafayali/SHU4dyMukHI/AAAAAAAAACk/EeDn3WAfGsg/alias3_thumb%5B2%5D.jpg?imgmax=800" width="343" height="180" /></a> </p> <p>Command window has other useful uses as well. I will leave those for another post.</p> <div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-8460252372628796858?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-38094997304051530532008-07-07T22:50:00.001+06:002008-07-07T22:50:31.797+06:00FTPing with ASP .NET<p>The framework provides us with various techniques to connect with FTP servers and interact with them. In this post, I will go over some of the ways and we will see how to add functionality to a FTP client. </p> <p>Here is a limited class for our FTP. It has three methods: listFiles, makeDirectory and deleteDirectory. </p> <p><pre style="background-color: white"><div style="border:1px solid gray;overflow:scroll;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080;"> 1</span> <span style="color: #0000FF;">Imports</span><span style="color: #000000;"> Microsoft.VisualBasic<br /></span><span style="color: #008080;"> 2</span> <span style="color: #000000;"></span><span style="color: #0000FF;">Imports</span><span style="color: #000000;"> System.IO<br /></span><span style="color: #008080;"> 3</span> <span style="color: #000000;"></span><span style="color: #0000FF;">Imports</span><span style="color: #000000;"> System.Net<br /></span><span style="color: #008080;"> 4</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 5</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 6</span> <span style="color: #000000;"></span><span style="color: #0000FF;">Public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Class</span><span style="color: #000000;"> ftpclientcode<br /></span><span style="color: #008080;"> 7</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Protected</span><span style="color: #000000;"> myftprequest </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> FtpWebRequest<br /></span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Protected</span><span style="color: #000000;"> myftpresponse </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> FtpWebResponse<br /></span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Private</span><span style="color: #000000;"> _myserver </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"><br /></span><span style="color: #008080;">10</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Private</span><span style="color: #000000;"> _myuser </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"><br /></span><span style="color: #008080;">11</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Private</span><span style="color: #000000;"> _mypw </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"><br /></span><span style="color: #008080;">12</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">13</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;">(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> myserver </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">, </span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> myuser </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">, </span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> mypw </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">)<br /></span><span style="color: #008080;">14</span> <span style="color: #000000;"> _myserver </span><span style="color: #000000;">=</span><span style="color: #000000;"> myserver<br /></span><span style="color: #008080;">15</span> <span style="color: #000000;"> _myuser </span><span style="color: #000000;">=</span><span style="color: #000000;"> myuser<br /></span><span style="color: #008080;">16</span> <span style="color: #000000;"> _mypw </span><span style="color: #000000;">=</span><span style="color: #000000;"> mypw<br /></span><span style="color: #008080;">17</span> <span style="color: #000000;"> myftprequest </span><span style="color: #000000;">=</span><span style="color: #000000;"> WebRequest.Create(_myserver)<br /></span><span style="color: #008080;">18</span> <span style="color: #000000;"> myftprequest.Credentials </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> NetworkCredential(_myuser, _mypw)<br /></span><span style="color: #008080;">19</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">20</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">21</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">22</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">23</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Function</span><span style="color: #000000;"> ListFiles() </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> Stream<br /></span><span style="color: #008080;">24</span> <span style="color: #000000;"> myftprequest.Method </span><span style="color: #000000;">=</span><span style="color: #000000;"> WebRequestMethods.Ftp.ListDirectory<br /></span><span style="color: #008080;">25</span> <span style="color: #000000;"> myftpresponse </span><span style="color: #000000;">=</span><span style="color: #000000;"> myftprequest.GetResponse()<br /></span><span style="color: #008080;">26</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Return</span><span style="color: #000000;"> myftpresponse.GetResponseStream()<br /></span><span style="color: #008080;">27</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">28</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Function</span><span style="color: #000000;"><br /></span><span style="color: #008080;">29</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">30</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> MakeDirectory(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> mydirname </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">)<br /></span><span style="color: #008080;">31</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">32</span> <span style="color: #000000;"> myftprequest.Method </span><span style="color: #000000;">=</span><span style="color: #000000;"> WebRequestMethods.Ftp.MakeDirectory<br /></span><span style="color: #008080;">33</span> <span style="color: #000000;"> myftpresponse </span><span style="color: #000000;">=</span><span style="color: #000000;"> myftprequest.GetResponse()<br /></span><span style="color: #008080;">34</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">35</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">36</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">37</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Shared</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> Close()<br /></span><span style="color: #008080;">38</span> <span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;"> myftpresponse.Close()</span><span style="color: #008000;"><br /></span><span style="color: #008080;">39</span> <span style="color: #008000;"></span><span style="color: #000000;"><br /></span><span style="color: #008080;">40</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">41</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">42</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> DeleteDirectory(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> mydirname </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">)<br /></span><span style="color: #008080;">43</span> <span style="color: #000000;"> myftprequest.Method </span><span style="color: #000000;">=</span><span style="color: #000000;"> WebRequestMethods.Ftp.RemoveDirectory<br /></span><span style="color: #008080;">44</span> <span style="color: #000000;"> myftpresponse </span><span style="color: #000000;">=</span><span style="color: #000000;"> myftprequest.GetResponse()<br /></span><span style="color: #008080;">45</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">46</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">47</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">48</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">49</span> <span style="color: #000000;"></span><span style="color: #0000FF;">End Class</span></div></pre><br /><br /></p><br /><br /><p>And here is the Default.aspx page to work with our class</p><br /><p><pre style="background-color: white"><div style="border:1px solid gray;overflow:scroll;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080;"> 1</span> <span style="color: #0000FF;">Imports</span><span style="color: #000000;"> System.Net<br /></span><span style="color: #008080;"> 2</span> <span style="color: #000000;"></span><span style="color: #0000FF;">Imports</span><span style="color: #000000;"> System.IO<br /></span><span style="color: #008080;"> 3</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 4</span> <span style="color: #000000;"></span><span style="color: #0000FF;">Partial</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Class</span><span style="color: #000000;"> _Default<br /></span><span style="color: #008080;"> 5</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Inherits</span><span style="color: #000000;"> System.Web.UI.Page<br /></span><span style="color: #008080;"> 6</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 7</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Private</span><span style="color: #000000;"> servername </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">localhost</span><span style="color: #800000;">"</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Private</span><span style="color: #000000;"> myusername </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">rafayali</span><span style="color: #800000;">"</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Private</span><span style="color: #000000;"> mypassword </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">******</span><span style="color: #800000;">"</span><span style="color: #000000;"><br /></span><span style="color: #008080;">10</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">11</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">12</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> Page_Load(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> sender </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Object</span><span style="color: #000000;">, </span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> e </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> System.EventArgs) </span><span style="color: #0000FF;">Handles</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Me</span><span style="color: #000000;">.Load<br /></span><span style="color: #008080;">13</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> myftp </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> ftpclientcode(</span><span style="color: #800000;">"</span><span style="color: #800000;">ftp://</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">&</span><span style="color: #000000;"> servername, myusername, mypassword)<br /></span><span style="color: #008080;">14</span> <span style="color: #000000;"> Label1.Text </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">""</span><span style="color: #000000;"><br /></span><span style="color: #008080;">15</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> datastream </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> Stream </span><span style="color: #000000;">=</span><span style="color: #000000;"> myftp.ListFiles()<br /></span><span style="color: #008080;">16</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> reader </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> StreamReader(datastream)<br /></span><span style="color: #008080;">17</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">While</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Not</span><span style="color: #000000;"> reader.EndOfStream<br /></span><span style="color: #008080;">18</span> <span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">Response.Write(reader.ReadLine() & "<br/>")</span><span style="color: #008000;"><br /></span><span style="color: #008080;">19</span> <span style="color: #008000;"></span><span style="color: #000000;"> Label1.Text </span><span style="color: #000000;">=</span><span style="color: #000000;"> Label1.Text </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">&nbsp;&nbsp;?&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">&</span><span style="color: #000000;"> reader.ReadLine() </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;"><br/></span><span style="color: #800000;">"</span><span style="color: #000000;"><br /></span><span style="color: #008080;">20</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">While</span><span style="color: #000000;"><br /></span><span style="color: #008080;">21</span> <span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">myftp.Close()</span><span style="color: #008000;"><br /></span><span style="color: #008080;">22</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">23</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">24</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> createdir_Click(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> sender </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Object</span><span style="color: #000000;">, </span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> e </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> System.EventArgs) </span><span style="color: #0000FF;">Handles</span><span style="color: #000000;"> createdir.Click<br /></span><span style="color: #008080;">25</span> <span style="color: #000000;"> Panel1.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">False</span><span style="color: #000000;"><br /></span><span style="color: #008080;">26</span> <span style="color: #000000;"> Panel2.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">False</span><span style="color: #000000;"><br /></span><span style="color: #008080;">27</span> <span style="color: #000000;"> Panel3.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">True</span><span style="color: #000000;"><br /></span><span style="color: #008080;">28</span> <span style="color: #000000;"> TextBox1.Focus()<br /></span><span style="color: #008080;">29</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">30</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">31</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> Button1_Click(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> sender </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Object</span><span style="color: #000000;">, </span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> e </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> System.EventArgs) </span><span style="color: #0000FF;">Handles</span><span style="color: #000000;"> createbutton.Click<br /></span><span style="color: #008080;">32</span> <span style="color: #000000;"> Panel3.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">False</span><span style="color: #000000;"><br /></span><span style="color: #008080;">33</span> <span style="color: #000000;"> Panel1.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">True</span><span style="color: #000000;"><br /></span><span style="color: #008080;">34</span> <span style="color: #000000;"> Panel2.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">True</span><span style="color: #000000;"><br /></span><span style="color: #008080;">35</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> myftp </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> ftpclientcode(</span><span style="color: #800000;">"</span><span style="color: #800000;">ftp://</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">&</span><span style="color: #000000;"> servername </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">/</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">&</span><span style="color: #000000;"> TextBox1.Text, myusername, mypassword)<br /></span><span style="color: #008080;">36</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"><br /></span><span style="color: #008080;">37</span> <span style="color: #000000;"> myftp.MakeDirectory(TextBox1.Text)<br /></span><span style="color: #008080;">38</span> <span style="color: #000000;"> Page_Load(sender, e)<br /></span><span style="color: #008080;">39</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Catch</span><span style="color: #000000;"> ex </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> Exception<br /></span><span style="color: #008080;">40</span> <span style="color: #000000;"> Response.Write(ex.Message.ToString())<br /></span><span style="color: #008080;">41</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Finally</span><span style="color: #000000;"><br /></span><span style="color: #008080;">42</span> <span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">myftp.Close()</span><span style="color: #008000;"><br /></span><span style="color: #008080;">43</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"><br /></span><span style="color: #008080;">44</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">45</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">46</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> cancelbutton_Click(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> sender </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Object</span><span style="color: #000000;">, </span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> e </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> System.EventArgs) </span><span style="color: #0000FF;">Handles</span><span style="color: #000000;"> cancelbutton.Click, Button2.Click<br /></span><span style="color: #008080;">47</span> <span style="color: #000000;"> Panel1.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">True</span><span style="color: #000000;"><br /></span><span style="color: #008080;">48</span> <span style="color: #000000;"> Panel2.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">True</span><span style="color: #000000;"><br /></span><span style="color: #008080;">49</span> <span style="color: #000000;"> Panel3.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">False</span><span style="color: #000000;"><br /></span><span style="color: #008080;">50</span> <span style="color: #000000;"> Panel4.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">False</span><span style="color: #000000;"><br /></span><span style="color: #008080;">51</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">52</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">53</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> removedir_Click(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> sender </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Object</span><span style="color: #000000;">, </span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> e </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> System.EventArgs) </span><span style="color: #0000FF;">Handles</span><span style="color: #000000;"> removedir.Click<br /></span><span style="color: #008080;">54</span> <span style="color: #000000;"> Panel1.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">False</span><span style="color: #000000;"><br /></span><span style="color: #008080;">55</span> <span style="color: #000000;"> Panel2.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">False</span><span style="color: #000000;"><br /></span><span style="color: #008080;">56</span> <span style="color: #000000;"> Panel4.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">True</span><span style="color: #000000;"><br /></span><span style="color: #008080;">57</span> <span style="color: #000000;"> TextBox2.Focus()<br /></span><span style="color: #008080;">58</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">59</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">60</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> Button1_Click1(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> sender </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Object</span><span style="color: #000000;">, </span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> e </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> System.EventArgs) </span><span style="color: #0000FF;">Handles</span><span style="color: #000000;"> Button1.Click<br /></span><span style="color: #008080;">61</span> <span style="color: #000000;"> Panel4.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">False</span><span style="color: #000000;"><br /></span><span style="color: #008080;">62</span> <span style="color: #000000;"> Panel1.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">True</span><span style="color: #000000;"><br /></span><span style="color: #008080;">63</span> <span style="color: #000000;"> Panel2.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">True</span><span style="color: #000000;"><br /></span><span style="color: #008080;">64</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">65</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> myftp </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> ftpclientcode(</span><span style="color: #800000;">"</span><span style="color: #800000;">ftp://</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">&</span><span style="color: #000000;"> servername </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">/</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">&</span><span style="color: #000000;"> TextBox2.Text, myusername, mypassword)<br /></span><span style="color: #008080;">66</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"><br /></span><span style="color: #008080;">67</span> <span style="color: #000000;"> myftp.DeleteDirectory(TextBox2.Text)<br /></span><span style="color: #008080;">68</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">69</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Catch</span><span style="color: #000000;"> ex </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> Exception<br /></span><span style="color: #008080;">70</span> <span style="color: #000000;"> Response.Write(ex.Message.ToString())<br /></span><span style="color: #008080;">71</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">72</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"><br /></span><span style="color: #008080;">73</span> <span style="color: #000000;"> Page_Load(sender, e)<br /></span><span style="color: #008080;">74</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">75</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">76</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> Page_Unload(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> sender </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Object</span><span style="color: #000000;">, </span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> e </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> System.EventArgs) </span><span style="color: #0000FF;">Handles</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Me</span><span style="color: #000000;">.Unload<br /></span><span style="color: #008080;">77</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">78</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">79</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span><span style="color: #000000;"><br /></span><span style="color: #008080;">80</span> <span style="color: #000000;"></span><span style="color: #0000FF;">End Class</span><span style="color: #000000;"><br /></span><span style="color: #008080;">81</span> <span style="color: #000000;"></span></div></pre></p> <div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-3809499730405153053?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-68977200555095387242008-07-07T09:00:00.001+06:002008-07-07T09:09:45.132+06:00Web.Config Code Snippets<p align="justify">I was going through some old projects yesterday and came across some interesting web.config settings that I thought would be a good read. So, without further ado, here are some useful code snippets.</p> <p align="justify"> </p> <h4><strong>Set The Upload File Size to 128KB for a Particular Page</strong></h4> <p></p> <pre style="background-color: white"><div style="border:1px solid gray;overflow:scroll;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">1</span> <span style="color: #000000"><</span><span style="color: #000000">configuration</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #008080">2</span> <span style="color: #000000"> </span><span style="color: #000000"><</span><span style="color: #000000">location path</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">myFileUploadPage.aspx</span><span style="color: #800000">"</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #008080">3</span> <span style="color: #000000"> </span><span style="color: #000000"><</span><span style="color: #000000">system.web</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #008080">4</span> <span style="color: #000000"> </span><span style="color: #000000"><</span><span style="color: #000000">httpRuntime maxRequestLength</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">128</span><span style="color: #800000">"</span><span style="color: #000000">/></span><span style="color: #000000"><br /></span><span style="color: #008080">5</span> <span style="color: #000000"> </span><span style="color: #000000"></</span><span style="color: #000000">system.web</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #008080">6</span> <span style="color: #000000"> </span><span style="color: #000000"></</span><span style="color: #000000">location</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #008080">7</span> <span style="color: #000000"></span><span style="color: #000000"></</span><span style="color: #000000">configuration</span><span style="color: #000000">></span></div></pre><br /><br /><h4><strong>Bringing an Application Offline</strong></h4><br /><pre style="background-color: white"><div style="border:1px solid gray;overflow:scroll;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">1</span> <span style="color: #000000"><</span><span style="color: #000000">configuration</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #008080">2</span> <span style="color: #000000"></span><span style="color: #000000"><</span><span style="color: #000000">system.web</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #008080">3</span> <span style="color: #000000"> </span><span style="color: #000000"><</span><span style="color: #000000">httpRuntime enable</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">false</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">/></span><span style="color: #000000"><br /></span><span style="color: #008080">4</span> <span style="color: #000000"></span><span style="color: #000000"></</span><span style="color: #000000">system.web</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #008080">5</span> <span style="color: #000000"></span><span style="color: #000000"></</span><span style="color: #000000">configuration</span><span style="color: #000000">></span></div></pre> <div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-6897720055509538724?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-58711763472894670752008-07-07T08:35:00.001+06:002008-07-07T08:55:15.252+06:00Encrypting Database Passwords using MD5<p align="justify">Today, I will show you a very basic technique to encrypt database passwords for storage using MD5 algorithm. MD5 is a one-way algorithm; decrypting is almost next to impossible. One very important characteristic of this algorithm is that a particular text value would always result in the same encrypted value. Therefore, even though the encrypted value cannot be decrypted directly, we do have a way to verify such passwords.</p> <p>Let us get down straight to code and see for ourselves:</p> <p><div class="Indent" style="border:1px solid gray;overflow:scroll;"> <pre style="background-color: white"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080;"> 1</span> <span style="color: #0000FF;">Dim</span><span style="color: #000000;"> connectionString </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Data Source=.\SQLEXPRESS;AttachDbFilename=D:\rafay\projects\certification\encrypt\App_Data\members.mdf;Integrated Security=True;User Instance=True</span><span style="color: #800000;">"</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 2</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 3</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> sqlStatement </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">INSERT INTO users VALUES(@myuser,@mypassword)</span><span style="color: #800000;">"</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Using</span><span style="color: #000000;"> objConn </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> SqlConnection </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> SqlConnection(connectionString)<br /></span><span style="color: #008080;"> 5</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Using</span><span style="color: #000000;"> objComm </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> SqlCommand </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> SqlCommand(sqlStatement, objConn)<br /></span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> userNameParameter </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> SqlParameter(</span><span style="color: #800000;">"</span><span style="color: #800000;">@myuser</span><span style="color: #800000;">"</span><span style="color: #000000;">, Data.SqlDbType.NVarChar, </span><span style="color: #800080;">15</span><span style="color: #000000;">)<br /></span><span style="color: #008080;"> 7</span> <span style="color: #000000;"> userNameParameter.Value </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Test</span><span style="color: #800000;">"</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 8</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> passwordParameter </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> SqlParameter(</span><span style="color: #800000;">"</span><span style="color: #800000;">@mypassword</span><span style="color: #800000;">"</span><span style="color: #000000;">, Data.SqlDbType.Binary, </span><span style="color: #800080;">16</span><span style="color: #000000;">)<br /></span><span style="color: #008080;">10</span> <span style="color: #000000;"> passwordParameter.Value </span><span style="color: #000000;">=</span><span style="color: #000000;"> encryptPasswordWithMD5(</span><span style="color: #800000;">"</span><span style="color: #800000;">TestAccountPassword</span><span style="color: #800000;">"</span><span style="color: #000000;">)<br /></span><span style="color: #008080;">11</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">12</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> objCommParameterArray() </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> SqlParameter </span><span style="color: #000000;">=</span><span style="color: #000000;"> {userNameParameter, passwordParameter}<br /></span><span style="color: #008080;">13</span> <span style="color: #000000;"> objComm.Parameters.AddRange(objCommParameterArray)<br /></span><span style="color: #008080;">14</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">15</span> <span style="color: #000000;"> objConn.Open()<br /></span><span style="color: #008080;">16</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> result </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Integer</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> objComm.ExecuteNonQuery()<br /></span><span style="color: #008080;">17</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Using</span><span style="color: #000000;"><br /></span><span style="color: #008080;">18</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Using</span><span style="color: #000000;"><br /></span><span style="color: #008080;">19</span> <span style="color: #000000;"></span></div></pre> <br /> </div></p><br /><p align="justify">This is our very basic example of connecting to SQL Server Express database file called members.mdf. We set up a sql statement and two parameters for username and password. The important part to look at is that the password is first encrypted using our helper function called encryptPasswordWithMD5. Another thing to note is that the database field for password is defined as a binary type of length 16. Once the parameters are defined and their values set, a parameter array is formed and then added to the SQLCommand object. The record is finally inserted with a call to ExecuteNonQuery.</p><br /><p>Let us now have a look at our encryptPasswordWithMD5 function. </p><br /><p><div class="Indent" style="border:1px solid gray;overflow:scroll;"><br /> <pre style="background-color: white"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080;"> 1</span> <span style="color: #0000FF;">Private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Function</span><span style="color: #000000;"> encryptPasswordWithMD5(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> stringToEncrypt </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">) </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Byte</span><span style="color: #000000;">()<br /></span><span style="color: #008080;"> 2</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> MD5Encrypter </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> MD5CryptoServiceProvider()<br /></span><span style="color: #008080;"> 3</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> stringBytes() </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Byte</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> convertStringToBytes(stringToEncrypt)<br /></span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> bufferToHoldEncryptedData() </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Byte</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Nothing</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 5</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> bufferToHoldEncryptedData </span><span style="color: #000000;">=</span><span style="color: #000000;"> MD5Encrypter.ComputeHash(stringBytes)<br /></span><span style="color: #008080;"> 7</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Return</span><span style="color: #000000;"> bufferToHoldEncryptedData<br /></span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Function</span><span style="color: #000000;"><br /></span><span style="color: #008080;">10</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">11</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Function</span><span style="color: #000000;"> convertStringToBytes(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> stringToConvert </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">) </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Byte</span><span style="color: #000000;">()<br /></span><span style="color: #008080;">12</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Return</span><span style="color: #000000;"> Text.Encoding.UTF8.GetBytes(stringToConvert)<br /></span><span style="color: #008080;">13</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Function</span></div></pre> <br /> </div><br /> </p><br /><br /><p align="justify">Line 3 of the code above is of particular interest. Since the ComputeHash method of MD5CryptoServiceProvider class takes in a byte value, our very first task is to convert the password string into a byte. This is accomplished using our second helper function convertStringToBytes (also defined in the listing above). What convertStringToBytes does is to take a string value and return the bytes using GetBytes method of the Encoding class in System.Text namespace. Once this is done, we calculate the hash and store in a buffer and return this buffer.</p><br /><p>So, how do we actually verify the credentials since there is no way to decrypt? Well, since MD5 always results in the same hash for the same text, we can encode the text again and compare this against the value in the database. Code sample follows:</p><br /><p><div class="Indent" style="border:1px solid gray;overflow:scroll;"><br /> <pre style="background-color: white"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080;"> 1</span> <span style="color: #0000FF;">Public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Function</span><span style="color: #000000;"> checkPassword(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> userName </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">, </span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> myPassword </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">) </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Boolean</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 2</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> connectionString </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Data Source=.\SQLEXPRESS;AttachDbFilename=D:\rafay\projects\certification\encrypt\App_Data\members.mdf;Integrated Security=True;User Instance=True</span><span style="color: #800000;">"</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 3</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> result </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Integer</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> sqlStatement </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">SELECT count(*) from users Where myuser=@myuser AND mypassword=@mypassword</span><span style="color: #800000;">"</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 5</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Using</span><span style="color: #000000;"> objConn </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> SqlConnection </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> SqlConnection(connectionString)<br /></span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Using</span><span style="color: #000000;"> objComm </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> SqlCommand </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> SqlCommand(sqlStatement, objConn)<br /></span><span style="color: #008080;"> 7</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> userNameParameter </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> SqlParameter(</span><span style="color: #800000;">"</span><span style="color: #800000;">@myuser</span><span style="color: #800000;">"</span><span style="color: #000000;">, Data.SqlDbType.NVarChar, </span><span style="color: #800080;">15</span><span style="color: #000000;">)<br /></span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> userNameParameter.Value </span><span style="color: #000000;">=</span><span style="color: #000000;"> userName<br /></span><span style="color: #008080;"> 9</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">10</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> passwordParameter </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> SqlParameter(</span><span style="color: #800000;">"</span><span style="color: #800000;">@mypassword</span><span style="color: #800000;">"</span><span style="color: #000000;">, Data.SqlDbType.Binary, </span><span style="color: #800080;">16</span><span style="color: #000000;">)<br /></span><span style="color: #008080;">11</span> <span style="color: #000000;"> passwordParameter.Value </span><span style="color: #000000;">=</span><span style="color: #000000;"> encryptPasswordWithMD5(myPassword)<br /></span><span style="color: #008080;">12</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">13</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> objCommParameterArray() </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> SqlParameter </span><span style="color: #000000;">=</span><span style="color: #000000;"> {userNameParameter, passwordParameter}<br /></span><span style="color: #008080;">14</span> <span style="color: #000000;"> objComm.Parameters.AddRange(objCommParameterArray)<br /></span><span style="color: #008080;">15</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">16</span> <span style="color: #000000;"> objConn.Open()<br /></span><span style="color: #008080;">17</span> <span style="color: #000000;"> result </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">CInt</span><span style="color: #000000;">(objComm.ExecuteScalar())<br /></span><span style="color: #008080;">18</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Using</span><span style="color: #000000;"><br /></span><span style="color: #008080;">19</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Using</span><span style="color: #000000;"><br /></span><span style="color: #008080;">20</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"> result </span><span style="color: #000000;">></span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Then</span><span style="color: #000000;"><br /></span><span style="color: #008080;">21</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">True</span><span style="color: #000000;"><br /></span><span style="color: #008080;">22</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Else</span><span style="color: #000000;"><br /></span><span style="color: #008080;">23</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">False</span><span style="color: #000000;"><br /></span><span style="color: #008080;">24</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"><br /></span><span style="color: #008080;">25</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Function</span><span style="color: #000000;"><br /></span><span style="color: #008080;">26</span> <span style="color: #000000;"></span><span style="color: #0000FF;">End Class</span></div></pre> <br /> </div></p><br /><p align="justify">In the first code above, I have hardcoded the username and password values. These would naturally need to be changed and the source of input would be probably be a CreateUserWizard or another composite control with various textboxes. The values were hardcoded for simplicity.</p> <div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-5871176347289467075?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-85257657035121657022008-07-07T02:15:00.001+06:002008-07-07T02:15:04.077+06:00ASP .NET Cheat Sheets<p>Here I have collected various cheat sheets for download. I do not recall the exact sources from where they were downloaded. If you download a sheet and know the source, let me know and I will mention it in this post.</p> <p> </p> <p><a title="regular expressions cheat sheet" href="http://www.ecubicle.net/blogdocs/regular_expressions_cheat_sheet.pdf" target="_blank">Regular Expressions Cheat Sheet</a> <strong>(pdf)</strong></p> <p><a title="page life cycle pdf" href="http://www.ecubicle.net/blogdocs/PAGE%20LIFE%20CYCLE%20-%20ASP%20.NET.PDF" target="_blank">Page Life Cycle</a> <strong>(pdf)</strong></p> <p><a title="Page life cycle image" href="http://www.ecubicle.net/blogdocs/Asp.Net2.0Lifecycle.png" target="_blank">ASP .NET 2.0 Life Cycle</a> <strong>(png)</strong></p> <p><a title="tips and tricks asp.net" href="http://www.ecubicle.net/blogdocs/TipsandTricks.ppt" target="_blank">ASP .NET Tips and Tricks</a> <strong>(ppt)</strong>: Microsoft ASP .NET Team presentation</p> <p><a title="VS Code Snippets" href="http://www.ecubicle.net/blogdocs/vs2005-code-snippets.pdf" target="_blank">VS 2005 Code Snippets</a> <strong>(pdf)</strong></p> <p><a title="health monitoring class diagram" href="http://www.ecubicle.net/blogdocs/health_monitoring_class_diagram.bmp" target="_blank">Health Monitoring Class Diagram</a> <strong>(bmp)</strong>: Scott Mitchell's article on Health Monitoring at 4GuysFromRolla</p> <p><a title="asp.net membership database schema - aspnetdb.mdf" href="http://www.ecubicle.net/blogdocs/aspnet_membership_schema_updated.png" target="_blank">ASP .NET Membership Database Schema</a> <strong>(png)</strong></p> <p><a title="visual studio 2005 key bindings" href="http://www.ecubicle.net/blogdocs/VB_2005_color.pdf" target="_blank">Visual Basic 2005 Default Key Bindings</a> <strong>(pdf)</strong></p> <p><a title="visual studio 2008 default key bindings" href="http://www.ecubicle.net/blogdocs/Visual Basic 2008 Poster.pdf" target="_blank">Visual Basic 2008 Default Key Bindings</a> <strong>(pdf)</strong></p> <p><a title="health monitoring event codes" href="http://www.ecubicle.net/blogdocs/web_events_health_monitoring_class_hierarchy_and_event_codes.png" target="_blank">Health Monitoring Event Codes</a> <strong>(png)</strong></p> <p> </p> <p>I will update these listings, if and, when I come across new sheets. If you know of any, please let me know and I will link them here. </p> <p><font color="#ff0000">This post is accessible from the sidebar in the future.</font></p> <div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-8525765703512165702?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-32017897626599756112008-07-04T04:28:00.001+06:002008-07-04T04:30:50.544+06:00Compressing ViewState<p>Viewstate is a client-side "state management" method. Viewstate allows page and control data to be preserved between postbacks.</p> <blockquote> <p>When client side HTML is generated for the page, the current state and data of the page is serialized into base64-encoded strings and stored as hidden field(s) in the page. Viewstate is page specific; that is, once you navigate away from the page, the viewstate is lost. </p></blockquote> <div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-3201789762659975611?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-34164979454452691592008-07-04T03:28:00.001+06:002008-07-04T04:08:16.469+06:00Hangman Game - An Exercise in Learning<p align="justify">With ample time on hand past Saturday, I decided to dig up some past tutorials just to refresh some material that may have slipped my mind. I have found that going through old stuff once in a while is a handy way to polish ASP .NET skills - or any programming environment for that matter. So, the first time that caught my eye was a Hangman tutorial by Scott Mitchell at <a href="http://aspnet.4guysfromrolla.com/articles/121102-1.aspx" target="_blank">4GuysFromRolla</a>. 4GuysFromRolla is a great place to learn about ASP .NET; I do a tutorial every night or every week (depending on the complexity and the series) to stay current with this ever changing dynamic platform.</p> <p align="justify">I decided to use the Hangman tutorial and re-code it using different classes and logic. Here are parts and pieces of the changed code that I would like to share with the readers here. I have kept the method names same as the one found on 4GuysFromRolla for easy comparison.</p> <p align="justify"></p> <p align="justify"><div class="Indent" style="border:1px solid gray;overflow:scroll;"> <pre style="background-color: white"><div ><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080;"> 1</span> <span style="color: #0000FF;">Protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> generateLetterBoard()<br /></span><span style="color: #008080;"> 2</span> <span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">generates the letters from A-Z and creates linkbuttons </span><span style="color: #008000;"><br /></span><span style="color: #008080;"> 3</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">called in page_PreInit() </span><span style="color: #008000;"><br /></span><span style="color: #008080;"> 4</span> <span style="color: #008000;"></span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 5</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> i </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Integer</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> j </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Integer</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">65</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 7</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">For</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"> </span><span style="color: #0000FF;">To</span><span style="color: #000000;"> </span><span style="color: #800080;">25</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"> j </span><span style="color: #000000;"><</span><span style="color: #000000;"> </span><span style="color: #800080;">92</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Then</span><span style="color: #000000;"><br /></span><span style="color: #008080;">10</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> myLinkButton </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> LinkButton()<br /></span><span style="color: #008080;">11</span> <span style="color: #000000;"> myLinkButton.ID </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">ChrW</span><span style="color: #000000;">(j).ToString()<br /></span><span style="color: #008080;">12</span> <span style="color: #000000;"> myLinkButton.Text </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">ChrW</span><span style="color: #000000;">(j).ToString()<br /></span><span style="color: #008080;">13</span> <span style="color: #000000;"> myLinkButton.CommandArgument </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">ChrW</span><span style="color: #000000;">(j).ToString()<br /></span><span style="color: #008080;">14</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">AddHandler</span><span style="color: #000000;"> myLinkButton.Command, </span><span style="color: #0000FF;">AddressOf</span><span style="color: #000000;"> LetterGuessed<br /></span><span style="color: #008080;">15</span> <span style="color: #000000;"> form1.Controls.Add(myLinkButton)<br /></span><span style="color: #008080;">16</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"><br /></span><span style="color: #008080;">17</span> <span style="color: #000000;"> j </span><span style="color: #000000;">=</span><span style="color: #000000;"> j </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;"><br /></span><span style="color: #008080;">18</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Next</span><span style="color: #000000;"><br /></span><span style="color: #008080;">19</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Sub</span></div></pre> <br /> </div><br /> <br /> </p><br /><br /><p align="justify">As you can see, instead of creating link buttons singularly in the source view, I took the approach of <font color="#800000">dynamically creating the link buttons</font>. What the above snippet does is it declares a variable to loop 26 times for each alphabet in the English language. The inner loop then executes to display letters; letters are extracted from their ASCII values (65 represents an uppercase A, 90 represents an uppercase Z). The inner loop, then, create a link button, and assigns its <font color="#800000">Text</font>, <font color="#800000">ID</font>, and <font color="#800000">CommandArgument</font> properties. It then adds an event handler to handle the Command event. Command event is raised each time that a link button is clicked. Finally, the link button is added to the form's controls collection. </p><br /><br /><p align="justify">Here is an ASCII chart for reference (taken from <a title="http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/files/ascii.htm" href="http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/files/ascii.htm">http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/files/ascii.htm</a>).</p><br /><br /><p align="justify"><a href="http://lh4.ggpht.com/rafayali/SG1LeF5u2QI/AAAAAAAAAB4/r1WEb3vg1UY/s1600-h/ASCII1%5B22%5D.gif"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="ASCII1" src="http://lh3.ggpht.com/rafayali/SG1LeUM42OI/AAAAAAAAACE/vlyTtYMHS8s/ASCII1_thumb%5B20%5D.gif?imgmax=800" width="580" height="620" /></a> </p><br /><br /><p align="justify"> </p><br /><br /><p align="justify">The second piece of code that I changed was <font color="#800000">getRandomWordFromTextFile()</font> method. Here's the implementation:</p><br /><br /> <div class="Indent" style="border:1px solid gray;overflow:scroll;"><br /> <pre style="background-color: white"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080;"> 1</span> <span style="color: #0000FF;">Protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Function</span><span style="color: #000000;"> getRandomWordFromTextFile(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> filePath </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;">) </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 2</span> <span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">Dim myFileStream As New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)</span><span style="color: #008000;"><br /></span><span style="color: #008080;"> 3</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">Dim words As New ArrayList()</span><span style="color: #008000;"><br /></span><span style="color: #008080;"> 4</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">Dim myStreamReader As New StreamReader(myFileStream)</span><span style="color: #008000;"><br /></span><span style="color: #008080;"> 5</span> <span style="color: #008000;"></span><span style="color: #000000;"><br /></span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">While myStreamReader.Peek > -1</span><span style="color: #008000;"><br /></span><span style="color: #008080;"> 7</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;"> words.Add(myStreamReader.ReadLine())</span><span style="color: #008000;"><br /></span><span style="color: #008080;"> 8</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">End While</span><span style="color: #008000;"><br /></span><span style="color: #008080;"> 9</span> <span style="color: #008000;"></span><span style="color: #000000;"><br /></span><span style="color: #008080;">10</span> <span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">myFileStream.Close()</span><span style="color: #008000;"><br /></span><span style="color: #008080;">11</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">myStreamReader.Close()</span><span style="color: #008000;"><br /></span><span style="color: #008080;">12</span> <span style="color: #008000;"></span><span style="color: #000000;"><br /></span><span style="color: #008080;">13</span> <span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">Dim randomNumberGenerator As New Random()</span><span style="color: #008000;"><br /></span><span style="color: #008080;">14</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">Dim randomNumber As Integer = randomNumberGenerator.Next(words.Count)</span><span style="color: #008000;"><br /></span><span style="color: #008080;">15</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">Dim chosenWord As String = words(randomNumber)</span><span style="color: #008000;"><br /></span><span style="color: #008080;">16</span> <span style="color: #008000;"></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">Return chosenWord</span><span style="color: #008000;"><br /></span><span style="color: #008080;">17</span> <span style="color: #008000;"></span><span style="color: #000000;"><br /></span><span style="color: #008080;">18</span> <span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;">THE FOLLOWING USES OLEDBDATAADAPTER TO READ THE TEXT FILE AND RETURN RANDOM WORD</span><span style="color: #008000;"><br /></span><span style="color: #008080;">19</span> <span style="color: #008000;"></span><span style="color: #000000;"><br /></span><span style="color: #008080;">20</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> ds </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> DataSet<br /></span><span style="color: #008080;">21</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> chosenWord </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">String</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">""</span><span style="color: #000000;"><br /></span><span style="color: #008080;">22</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">23</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Not</span><span style="color: #000000;"> Page.IsPostBack </span><span style="color: #0000FF;">Then</span><span style="color: #000000;"><br /></span><span style="color: #008080;">24</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> objConn </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> OleDbConnection(</span><span style="color: #800000;">"</span><span style="color: #800000;">provider=microsoft.jet.oledb.4.0;data source=</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">&</span><span style="color: #000000;"> filePath </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">;extended properties='text;hdr=yes;'</span><span style="color: #800000;">"</span><span style="color: #000000;">)<br /></span><span style="color: #008080;">25</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> objAdapter </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> OleDbDataAdapter(</span><span style="color: #800000;">"</span><span style="color: #800000;">SELECT * FROM WORDS.txt</span><span style="color: #800000;">"</span><span style="color: #000000;">, objConn)<br /></span><span style="color: #008080;">26</span> <span style="color: #000000;"> ds </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> DataSet()<br /></span><span style="color: #008080;">27</span> <span style="color: #000000;"> objAdapter.Fill(ds)<br /></span><span style="color: #008080;">28</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> randomNumberGenerator </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> Random()<br /></span><span style="color: #008080;">29</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> randomNumber </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Integer</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> randomNumberGenerator.Next(ds.Tables(</span><span style="color: #800080;">0</span><span style="color: #000000;">).Rows.Count)<br /></span><span style="color: #008080;">30</span> <span style="color: #000000;"> chosenWord </span><span style="color: #000000;">=</span><span style="color: #000000;"> ds.Tables(</span><span style="color: #800080;">0</span><span style="color: #000000;">).Rows(randomNumber).Item(</span><span style="color: #800080;">0</span><span style="color: #000000;">).ToString()<br /></span><span style="color: #008080;">31</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">32</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"><br /></span><span style="color: #008080;">33</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">34</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">Return</span><span style="color: #000000;"> chosenWord<br /></span><span style="color: #008080;">35</span> <span style="color: #000000;"> <br /></span><span style="color: #008080;">36</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">37</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">38</span> <span style="color: #000000;"> </span><span style="color: #0000FF;">End Function</span></div></pre> <br /> </div><br /><p align="justify"></p><br /><br /> <br /><br /><p align="justify">Instead of using a <font color="#800000">TextReader, </font><font color="#000000">I opted for an <font color="#800000">OleDBDataAdapter</font> to perform the task of connecting to the text file. A random value is then extracted from the dataset; random row is selected and the value in the first column is set as the Hangman word.</font></p><br /><br /><p align="justify">The remainder of the code is similar. </p><br /><br /><p align="justify"></p> <div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-3416497945445269159?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-41728267680445764672008-07-04T03:09:00.001+06:002008-07-04T03:09:04.028+06:00MSDN Hackable URLs<p>An extremely useful tip to know is that the new MSDN2 urls are hackable. What this means is that you can access them via a namespace.class convention.</p> <p>So, for example, lets assume we need documentation for System.Data.OLEDB namespace. We have a lot of options; the first one usually being Google.</p> <p>With the MSDN2, we can also try this:</p> <p><a title="http://msdn2.microsoft.com/en-us/library/system.data.oledb.aspx" href="http://msdn2.microsoft.com/en-us/library/system.data.oledb.aspx">http://msdn2.microsoft.com/en-us/library/system.data.oledb.aspx</a></p> <p>The URL above would take you straight to the documentation. So, what if we wanted to look up OleDbCommand Class? Well, we can just follow the namespace.class convention to look it up using the following URL:</p> <p><a href="http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbcommand.aspx">http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbcommand.aspx</a></p> <p>Pretty Neat, huh? A small but rather neat trick by Microsoft to make life easy when searching for documentation.</p> <div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-4172826768044576467?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-12023434200602426362008-06-30T10:59:00.006+06:002008-12-09T09:26:47.467+05:00Making an application go offline: App_Offline.htm<div style="text-align: justify;"><span style="font-size:100%;"><span style="font-family:verdana;"><span style="font-family:verdana;">The easiest way an application can be made to go offline is by adding a little known file to your ASP .NET project. The file is called <span style="color: rgb(0, 102, 0); font-weight: bold;">App_Offline.htm<span style="color: rgb(0, 0, 0);"> </span></span><span style="color: rgb(0, 102, 0);"><span style="color: rgb(0, 0, 0);">a</span></span><span style="color: rgb(0, 102, 0);"><span style="color: rgb(0, 0, 0);">nd needs to be placed in the root of the application. Just by including this file within your project:</span></span></span></span></span><br /></div><br /><ul style="font-family:verdana;"><li><span style="font-size:100%;"><span style="font-family:verdana;">the ap<span style="font-family:verdana;">pl</span>ication shuts down</span></span></li><li style="font-family:verdana;"><span style="font-size:100%;">all requests then return the app_offline.htm file's contents</span></li></ul><div style="text-align: justify;"><span style="font-size:100%;"><span style="font-family:verdana;">There is one thing to watch out for; and that is the size of this file. If the size is <span style="color: rgb(153, 0, 0); font-weight: bold;">less than 512 bytes, </span><span style="color: rgb(0, 0, 0);">the <span style="font-weight: bold;">Friendly Error Messages</span> must be <span style="font-weight: bold;">turned off</span> in IE for this to work (image shown below). </span></span></span><br /></div><span style="font-size:100%;"><span style="font-family:verdana;"><span style="color: rgb(0, 0, 0);"><br /></span></span></span><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_lSTGUixvPkM/SGjnQPcUaLI/AAAAAAAAAAc/X97-yFIM1ng/s1600-h/iefriendly.jpg"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://3.bp.blogspot.com/_lSTGUixvPkM/SGjnQPcUaLI/AAAAAAAAAAc/X97-yFIM1ng/s320/iefriendly.jpg" alt="" id="BLOGGER_PHOTO_ID_5217674434517952690" border="0" /></a><br /><span style="font-size:100%;"><span style="font-family:verdana;"><span style="color: rgb(0, 0, 0);"><br /></span></span></span><div style="text-align: justify;"><span style="font-size:100%;"><span style="font-family:verdana;"><span style="color: rgb(0, 0, 0);">Otherwise, the file can be enlarged to this size by placing arbitrary data in it. </span></span></span><span style="font-size:100%;"><span style="font-family:verdana;"><span style="color: rgb(0, 0, 0);">Since it is a simple html file, I do not see a reason to include sample code. The file can hold any content you wish to show users when an application goes offline.</span></span></span><br /></div><span style="font-size:100%;"><span style="font-family:verdana;"><span style="color: rgb(0, 0, 0);"><br />There are other methods to shut down an application as well; for example, using the web.config:<br /><br /><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">configuration</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">system.web</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">httpRuntime enable</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">false</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">system.web</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">configuration</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">6</span> </div><br />Or, by using this statement in your code-behind:<br /><br /><span style="font-weight: bold; color: rgb(0, 102, 0);">System.Web.HttpRuntime.ShutDownAppDomain()<br /><br /></span><span style="color: rgb(0, 0, 0);">It must also be remembered that the application is also shut down when one of the following occur:<br /></span></span></span></span><ul style="font-family:verdana;"><li><span style="font-size:100%;"><span style="color: rgb(102, 0, 0); font-weight: bold;">web.config</span> file is changed</span></li><li><span style="font-size:100%;"><span style="color: rgb(102, 0, 0); font-weight: bold;">global.asax</span> file is changed</span></li><li><span style="font-size:100%;">change in the <span style="color: rgb(102, 0, 0); font-weight: bold;">App_Code</span> directory</span></li><li><span style="font-size:100%;">change in the <span style="color: rgb(102, 0, 0); font-weight: bold;">\bin</span> directory</span></li><li><span style="font-size:100%;">when a <span style="font-weight: bold; color: rgb(102, 0, 0);">directory is deleted</span></span></li></ul><br /><span style="font-size:100%;"><span style="font-family:verdana;"><span style="color: rgb(0, 0, 0);"><span style="font-weight: bold; color: rgb(0, 102, 0);"><br /></span></span></span></span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-1202343420060242636?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com1tag:blogger.com,1999:blog-5466523919981160355.post-1173427992679536632008-06-29T07:31:00.007+06:002008-07-04T03:20:09.194+06:00Cookieless sessions and dynamic hyperlinks<div style="text-align: justify"><span style="font-family: verdana">When using cookieless Forms Authentication where session values are embedded and maintained in the URL, the session value must be continuously maintained within the URL for each page request. Otherwise, the session is lost. Once this session value is lost, the user (who has logged in and authenticated) gets logged out and has to pass through the authentication mechanism once more. <br /> <br />This maintenance of session values in the URL is automatically taken care of by ASP .NET for us in all situations where the <span style="font-weight: bold">URL DOES NOT START</span> with either <span style="font-weight: bold">http://</span> <span style="font-weight: bold">or</span> <span style="font-weight: bold">/</span>. That means, most cases are covered.</span> <br /></div> <span style="font-family: verdana"> <br /></span> <div align="justify"><span style="font-family: verdana"></span></div> <div align="justify"><span style="font-family: verdana"></span></div> <div align="justify"><span style="font-family: verdana"></span></div> <div align="justify"><span style="font-family: verdana">However, there are cases when this does not hold true. I came across one such case recently. <br /> <br /> <br /></span></div> <div align="justify"><span style="font-family: verdana"></span></div> <div align="justify"> <div style="text-align: justify"><span style="font-family: verdana">The requirement was to run through the Site Map file and dynamically create hyperlinks within different panel controls on a page. The requirement was simple enough and all that I had to do was to use the SiteMap Class's methods to retrieve child nodes from the CurrentNode and create hyperlink server controls in code. So, the code to create the hyperlink looked something like this:</span> <br /></div> <span style="font-family: verdana"> <br /> <br /></span></div> <div align="justify"><span style="font-family: verdana"></span></div> <div style="font-weight: bold" align="justify"><span style="color: rgb(0,102,0); font-family: verdana"> <div style="border-right: gray 1px solid; padding-right: 5px; padding-left: 5px; border-top: gray 1px solid; padding-bottom: 5px; margin: 5px; border-left: gray 1px solid; padding-top: 5px; border-bottom: gray 1px solid" class="Indent"> <pre style="background-color: white"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">1</span> <span style="color: #0000ff">Dim</span><span style="color: #000000"> contentHyperlink </span><span style="color: #0000ff">as</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> hyperlink()<br /></span><span style="color: #008080">2</span> <span style="color: #000000">contentHyperlink.navigateUrl</span><span style="color: #000000">=</span><span style="color: #000000">mySiteMapNodeCollectionObject.url<br /></span><span style="color: #008080">3</span> <span style="color: #000000">panel1.controls.add(contentHyperlink)</span></div></pre><br /> </div><br /> </span></div><br /><br /><div style="font-weight: bold" align="justify"><br /> <br /><span style="font-family: verdana">However, in this case, ASP .NET does not maintain the session id for us in the URL. If you take a moment to look at the page source, you would find that all URLS are absolute in the dynamically generated hyperlinks. As stated previously, if a URL is absolute (that is, starts with http://), session values would not be appended to the URL automatically. </span></div><br /><br /><div align="justify"><span style="font-family: verdana">To resolve this issue, we need to change the navigateUrl property assignment like this:</span></div><br /><br /><div align="justify"><span style="font-family: verdana"></span></div><br /><br /><div align="justify"><span style="font-weight: bold; color: rgb(0,102,0)">contentHyperlink.navigateUrl=Response.ApplyAppPathModifier(mySiteMapNodeCollectionObject.url)</span></div><br /><br /><div align="justify"><span style="font-family: verdana"></span></div><br /><br /><div align="justify"><span style="font-family: verdana">What <span style="font-weight: bold; color: rgb(0,102,0)">ApplyAppPathModifier</span> does is it takes the hyperlink and embeds session value in it. The method takes virtual path as its parameter and only works if you are using cookieless session. That is, the web.config must declare session configuration as follows: <br /><br /> <br /><br /> <br /></span><span style="font-family: verdana">For a complete reference, visit </span><a href="http://msdn.microsoft.com/en-us/library/system.web.httpresponse.applyapppathmodifier.aspx"><span style="font-family: verdana">MSDN</span></a></div><br /><br /><div align="justify"><span style="font-family: verdana"></span></div> <div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-117342799267953663?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0tag:blogger.com,1999:blog-5466523919981160355.post-42366837284715760112008-06-29T00:26:00.014+06:002008-12-09T09:26:47.622+05:00Show Footer in ASP .NET<span style="font-family:verdana;">Here is a useful tip if you want to show a uniform footer in all pages, including any subdirectories, from a central location.<br /><br /><br /></span><p align="justify"><a href="http://4.bp.blogspot.com/_lSTGUixvPkM/SGaDeb-ShJI/AAAAAAAAAAM/lZw7KaRLbv8/s1600-h/footer1.jpg"><span style="font-family:verdana;"><img style="FLOAT: left; MARGIN: 0px 10px 10px 0px" id="BLOGGER_PHOTO_ID_5217001777283630226" border="0" alt="" src="http://4.bp.blogspot.com/_lSTGUixvPkM/SGaDeb-ShJI/AAAAAAAAAAM/lZw7KaRLbv8/s320/footer1.jpg" /></span></a><span style="font-family:verdana;"><br /><span style="COLOR: rgb(0,153,0)"><strong>Add a new Global Application Class (Global.asax) to your application.</strong></span> </span></p><p align="justify"><span style="font-family:verdana;"><insert><br /></insert></span></p><p align="justify"><insert><span style="font-family:verdana;">If you have Visual Studio, the IDE generates commonly used Application and Session events. Now which event to use?</span></insert></p><br /><p align="justify"><span style="font-family:verdana;"><br /></span></p><p align="justify"><span style="font-family:verdana;"><br /></span></p><p align="justify"><span style="font-family:verdana;">Application_Start and End do not serve our purpose since we want the footer to show on each and every request, not only when the application is starting or ending. Application_Error, obviously is there to tap into any unhandled exceptions at the page level. </span></p><p align="justify"><span style="font-family:verdana;">Lets have a re-look at what our requirements are: </span></p><br /><p align="justify"><span style="font-family:verdana;"><span style="COLOR: rgb(0,102,0)">We would like to be able to display a footer message with each and every request under any subfolder of our site. Think of how shared web hosting companies display banner ads on every user's web pages. As soon as you create a page with them, the banner ad is automatically appended to your page - without you having to write a single line of code to do that. Yes, we would like our application to have the exact same capability.</span> </span></p><br /><p align="justify"><span style="font-family:verdana;">ASP .NET makes this really simple for us. What we need to do is to add a new event declaration and definition in global.asax. Note, new does not mean that we are raising a custom event. What it means is that we need to add an event that has not automatically been added for us by the IDE when we added Global.asax.</span></p><p align="justify"><span style="font-family:verdana;"><br /></span></p><p align="justify"><span style="font-family:verdana;">The event we are interested in is: <strong><span style="COLOR: rgb(0,102,0)">Application_EndRequest</span></strong>.</span></p><p align="justify"><span style="font-family:verdana;"><br /></span></p><p align="justify"><span style="font-family:verdana;">Let's get into code right away and then I will discuss some salient features of this event: </span><br /></p><div style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; OVERFLOW: auto; BORDER-LEFT: gray 1px solid; BORDER-BOTTOM: gray 1px solid" class="Indent"><br /><br /><pre style="BACKGROUND-COLOR: white"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="font-family:verdana;"><span style="COLOR: rgb(0,128,128)"> 1</span> <span style="COLOR: rgb(0,0,255)">Sub</span><span style="COLOR: rgb(0,0,0)"> Application_EndRequest(</span><span style="COLOR: rgb(0,0,255)">ByVal</span><span style="COLOR: rgb(0,0,0)"> sender </span><span style="COLOR: rgb(0,0,255)">As</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">Object</span><span style="COLOR: rgb(0,0,0)">, </span><span style="COLOR: rgb(0,0,255)">ByVal</span><span style="COLOR: rgb(0,0,0)"> e </span><span style="COLOR: rgb(0,0,255)">As</span></span><span style="font-family:verdana;"><span style="COLOR: rgb(0,0,0)"> EventArgs)<br /></span><span style="COLOR: rgb(0,128,128)"> 2</span> <span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">With</span></span><span style="font-family:verdana;"><span style="COLOR: rgb(0,0,0)"> Response<br /></span><span style="COLOR: rgb(0,128,128)"> 3</span> <span style="COLOR: rgb(0,0,0)"> .Write(</span><span style="COLOR: rgb(0,0,255)">String</span><span style="COLOR: rgb(0,0,0)">.Format(</span><span style="COLOR: rgb(128,0,0)">"</span><span style="COLOR: rgb(128,0,0)"><div width='{0}' style='{1}'></span><span style="COLOR: rgb(128,0,0)">"</span></span><span style="font-family:verdana;"><span style="COLOR: rgb(0,0,0)">, Unit.Percentage(</span><span style="COLOR: rgb(128,0,128)">100</span><span style="COLOR: rgb(0,0,0)">), </span><span style="COLOR: rgb(128,0,0)">"</span><span style="COLOR: rgb(128,0,0)">text-align:center;</span><span style="COLOR: rgb(128,0,0)">"</span></span><span style="font-family:verdana;"><span style="COLOR: rgb(0,0,0)">))<br /></span><span style="COLOR: rgb(0,128,128)"> 4</span> <span style="COLOR: rgb(0,0,0)"> .Write(</span><span style="COLOR: rgb(0,0,255)">String</span><span style="COLOR: rgb(0,0,0)">.Format(</span><span style="COLOR: rgb(128,0,0)">"</span><span style="COLOR: rgb(128,0,0)"><img src='{0}' alt='{1}' /></span><span style="COLOR: rgb(128,0,0)">"</span></span><span style="font-family:verdana;"><span style="COLOR: rgb(0,0,0)">, </span>"<span style="COLOR: rgb(128,0,0)">footer2.jpg</span><span style="COLOR: rgb(128,0,0)">"</span><span style="COLOR: rgb(0,0,0)">, </span><span style="COLOR: rgb(128,0,0)">"</span><span style="COLOR: rgb(128,0,0)">Footer</span><span style="COLOR: rgb(128,0,0)">"</span></span><span style="font-family:verdana;"><span style="COLOR: rgb(0,0,0)">))<br /></span><span style="COLOR: rgb(0,128,128)"> 5</span> <span style="COLOR: rgb(0,0,0)"> .Write(</span><span style="COLOR: rgb(128,0,0)">"</span><span style="COLOR: rgb(128,0,0)"><div></span><span style="COLOR: rgb(128,0,0)">"</span></span><span style="font-family:verdana;"><span style="COLOR: rgb(0,0,0)">)<br /></span><span style="COLOR: rgb(0,128,128)"> 6</span> <span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">End</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">With</span></span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="font-family:verdana;"><span style="COLOR: rgb(0,128,128)"> 7</span> <span style="COLOR: rgb(0,0,255)">End Sub</span></span></div><div> </div></pre><br/><br/></div><br /><div class="Indent"><span style="font-family:verdana;">Application_EndRequest, as the name suggests, is the last event where we have an opportunity to modify what is sent to the client. </span></div><br /><div class="Indent"><span style="font-family:verdana;"></span></div><br /><div class="Indent"><span style="font-family:verdana;">What this does is add an image tag and set its source (src) property to the name of the image that we want to display. </span><div class="Indent"></div><br /></div><div class="Indent"><span style="font-family:verdana;"><br /></span></div><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5466523919981160355-4236683728471576011?l=aspnet.eforica.com'/></div>Rafay Bin Alinoreply@blogger.com0