ASP - Writing to text files
Created 22 September 2004 00:00
The following code will create a text file called testfile.txt in the root of the C: drive, overwriting an existing file with the same name.
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objStream = fs.CreateTextFile("c:\testfile.txt", True)
objStream.WriteLine("This is a test.")
objStream.Close
Set objStream = Nothing
Set objFS = Nothing
Creating shortcuts using VB6
Created 09 September 2004 00:00
In your VB Project, to create a shortcut you need to add a reference to the Windows Script Host Object Model - if this is not available find the file WSHOM.OCX.

Example: create a shortcut to your application on the desktop

    Dim wsShell As IWshShell_Class
    Dim wsShortcut As IWshShortcut_Class
    Set wsShell = New IWshShell_Class
    Set wsShortcut = wsShell.CreateShortcut(wsShell.SpecialFolders("Desktop") + "test.lnk")
    With wsShortcut
        .Arguments = "1 2 3"
        .Description = "test shortcut"
        .IconLocation = "app.exe,1"
        .TargetPath = "C:me.exe"
        .WindowStyle = 3
        .WorkingDirectory = "C:temp"
        .Save
    End With
    Set wsShortcut = Nothing
    Set wsShell = Nothing
HTTP Requests with Java
Created 23 September 2004 00:00
import java.net.*;
import java.util.*;
import java.io.*;

public class Mine {
  public static void main(String args[]) {

    ObjectInputStream is = null;
    URL url = null;
    String  uri = "http://servername:port/yourservlet";
    HashMap hash = new HashMap();
    try {
      // calling the servlet by passing params
      url = new URL(uri + "?pm1=val1&pm2=val2");
            
      // open input stream and read the hashmap
      // returned by the servlet
      is = new ObjectInputStream(url.openStream());
      hash = (HashMap) is.readObject();
      // print it out
      System.out.println(hash);
    } 
    catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
}
Sending HTTP Requests with .NET
Created 16 September 2004 00:00
This code will send a web request via a proxy server and save it to a text file:
  byte[] buf = new byte[8192];
  int count = 0;
  // send the http request
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
  NetworkCredential cred = new NetworkCredential("username", "password", "DOMAIN");
  request.Proxy.Credentials = cred;
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  Stream resStream = response.GetResponseStream();
  // create the new text file
  FileInfo f = new FileInfo("C:TempTest.txt");
  f.Delete();
  StreamWriter sw = f.CreateText();
  // read contents of stream into text file
  while ((count = resStream.Read(buf, 0, buf.Length)) > 0) {
    string s = Encoding.ASCII.GetString(buf, 0, count);
    sw.Write(s);
  }
  // close streams
  sw.Close();
  resStream.Close(); 
  response.Close();

Remote authorisation

If your request requires remote authorisation, all you need to do is add the network credentials to the request, for example:

  request.Credentials = new NetworkCredential("username", "password");

Handling response codes

With the above method, anything other than a status of OK (200) will raise an error. If you want to check response codes, do the following:

HttpStatusCode statusCode;
HttpWebResponse response = null;
try
{
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/default.asp");
  response = (HttpWebResponse)request.GetResponse();
  statusCode = response.StatusCode;
}
catch (WebException ex)
{
  statusCode = ((HttpWebResponse)ex.Response).StatusCode;
}
finally 
{
  if (response != null) response.Close();
}

if (statusCode != HttpStatusCode.OK) 
{
  throw new Exception("Error!");
}
JavaScript setTimeout
Created 16 September 2004 00:00
The setTimeout method can be used to create your own running threads in a web document.

For example, to create update a text box every second with a new date/time:

<script language="javascript">
function setDateTime() {
  document.myForm.txtDateTime.value = new Date();
  setTimeout("setDateTime()", 1000);
}
</script>

..

<body onload="setDateTime()">

Adding parameters

You can add parameters to functions called by setTimeout as follows:

function myFunc(p1, p2) {
}

var val1 = 'p1value';
var val2 = 'p2value';
setTimeout('myFunc("' + val1 + '","' + val2 + '")', 1000);
Oracle : Converting Hex Strings to Binary
Created 22 September 2004 00:00
CREATE PROCEDURE ConvertHexStrToBinary 
  @hexstr varchar(1000),
  @binValue binary(8) OUTPUT
AS
  DECLARE @index tinyint,
          @result bigint,
          @digit int,
          @convstr varchar(15)
	
  SELECT  @index = LEN(@hexstr),
          @result = 0,
          @convstr = '123456789ABCDEF'
	
  WHILE (@index > 2)
  BEGIN
    SELECT @digit = CHARINDEX(SUBSTRING(@hexstr,@index,1),@convstr)
    SELECT @result = @result + @digit * POWER(CONVERT(numeric, 16), LEN(@hexstr) - @index)
    SELECT @index = @index - 1
  END

  SELECT @binValue = CONVERT(binary(8),@result)
GO
Dynamic WHERE and ORDER BY Clauses in SQL Server
Created 24 September 2004 00:00
SELECT *
    FROM MyTable
WHERE
    Col1 LIKE 
      CASE Col2
        WHEN '1' THEN 'X'
        WHEN '2' then 'Y'
        ELSE 'Z'
      END               
CREATE PROCEDURE GetEmployees ( 
    @ColName varchar(100) 
  ) 
  AS
    SELECT Col1, Col2
    FROM Employees
    ORDER BY
      CASE 
        WHEN @ColName='X' THEN CONVERT(char(50), Col1) + CONVERT(char(50), Col2)
        WHEN @ColName='Y' THEN CONVERT(varchar(50), Col1)
        WHEN @ColName='Z' THEN Z
      END