Saturday, November 17, 2007

Friday, November 16, 2007

Stopping form from submitting

Let's assume you have a form on a web page. It has an textarea and a form submit button. You want to alert the user that the textarea is required before submitting the form. You don't want the form to submit if validation fails, otherwise submit the form as normal. Here is the sample of how to handle this. NOTE: This works for ASP.Net also. Besure to include the return keyword in the onclick of the button. <html> ... <body> <form ... > function validate() { var myTextArea = document.getElementById('MyTextArea'); var val = myTextArea.value; if (val == "") { alert("The text area is required. Please enter text and try again."); return false; } else { return true; } } <textarea id="MyTextArea" id="MyTextArea" cols="20" rows="2"></textarea> <input id="Submit1" type="submit" value="submit" onclick="return validate();"/> </form> </html>

Thursday, November 8, 2007

Query by date (no time) in SQL Server

This is a simple one. This technique can be used to search for rows where the MyDateColumn has the date specified (as a string) and the time doesn't matter. select * from MyTable where Convert(VARCHAR, MyDateColumn, 101) = '10/31/2007' It can also be used in the select statement to format the output of MyDateColumn without the time portion. select Convert(VARCHAR, MyDateColumn, 101) = '10/31/2007' from MyTable

Tuesday, November 6, 2007

Highlighting matches using Regular Expressions in C#

Below is sample code on how to find matches to a particular regular expression in some body of text and surround each match with some html tags. For example, if the body of text is: string body = "This is just a test and is the body of text that we are trying to search"; After calling HighlightExpression(body); body will be This is just a <span class="highlight">test</span> and is the body of text that we are trying to search public string HighlightExpression(string body) { try { string expression = "test"; body = Regex.Replace(body, expression, new MatchEvaluator(HighlightUrls)); } catch (ArgumentException ex) { // Syntax error in the regular expression } } // this is a delegate that gets called for each match public string HighlightMatch(Match m) { return "<span class=\"highlight\">" + m.Value + "</span>"; }

Friday, November 2, 2007

The coolest zip tool ever!

7Zip is the coolest compression tool I have seen, and the best part is that it is FREE! Check it out at www.7-zip.org. The interface is pretty nice, but the format support is just awesome. It even supports .iso images.
  • Packing / unpacking: 7z, ZIP, GZIP, BZIP2 and TAR
  • Unpacking only: RAR, CAB, ISO, ARJ, LZH, CHM, Z, CPIO, RPM, DEB and NSIS
  • It is even integrated into Windows Explorer.

    Opening up Scheduled Tasks for remote servers from the command line

    Imagine you have Scheduled Tasks on many servers. While it is easy enough to open a Window in Windows Explorer and type something like file:////myserver/Scheduled to open the Scheduled Tasks window for a specified server, this does not work if you type this into the command prompt or in a batch file. What I found is that you can open a shortcut from the command line though. So, open the Scheduled Tasks as described above, and then drag the icon in the left part of the address bar to your desktop (or other directory). This will create a shortcut. You can then type this into a command line or batch file and the Scheduled Tasks Window will open for the specified server. This is not a huge time saver, but it is convenient to have a list of icons to click instead of trying to remember all the servers, type them in, etc. If you have lots of servers that need to be updated it can be very slow waiting for the Scheduled Tasks windows to appear. I recommend putting them in a batch file with the name of the shortcut (you will need to enclose in double quotes if the name has spaces in it) on each line. Here is an example of a batch file that opens 3 servers one after another. "Scheduled Tasks on server1.lnk" "Scheduled Tasks on server2.lnk" "Scheduled Tasks on server3.lnk" Now when you need to update the scheduled tasks all you do is run this batch file, take a few minute break, come back and you will have all your Scheduled Tasks windows open and ready for editing. This beats doing it one at a time if you ask me. :) TIP: You really only need to create one shortcut. Then make a copy of it, and open it in notepad. It is fairly cryptic looking. Just look for the server name and change it to the server you want it to point to. Save the text file. That is it. No you have a shortcut that works, and you didn't have to wait to connect to Scheduled Tasks to create it.