Analytics


Google

Friday, September 26, 2008

AJAX in Framework 3.5

AJAX is now built into Framework 3.5. Using it is quite straight forward:


First add
<asp:ScriptManager runat="server">
</asp:ScriptManager>
within the form tag:

The form tag is normally within the <body> </body> tags:
As in:
<head>
...
</head>
<body>
...
<form id="form1" runat="server">
....
</form>
</body>


Next use put your controls within the Update Panel. All controls within the Update Panel are automatically AJAX. However, make sure to put it within the ContentTemplate!!


<asp:UpdatePanel ID="uPane1" runat="server">
<ContentTemplate>
...
</ContentTemplate>
</asp:UpdatePanel>

Sunday, September 21, 2008

Invalid Path when consuming Webservice

This will happen if you are consuming a webservice which generates xml as return value. What happens is when the webservice works correctly, it will return a well formed xml as a reply.

However, if you are using IIS to serve the webservice and when there is an error, IIS will return the error in HTML. Since the routine (proxy class) that consumes the webservice expects a well formed xml and it gets a html response (which is typically an invalid xml), it will return "Invalid Path".

Unfortunately, in most cases the error is very misleading.

Friday, September 19, 2008

Creating Database link in Oracle to without defining remote database in sqlnet

Normally, when we create a database link to a remote database, we will define the alias in SQLNet and then create the database link as follows:

create database link dbtest connect to scott identified by tiger using 'dbtest';


However, it is possible to connect directly by using what is defined in the sqlnet as follows:

create database link dbtest connect to scott identified by tiger using '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = db.blogspot.COM)(PORT = 1521)))(CONNECT_DATA =(SID = orcl)))';

Tuesday, September 16, 2008

Hack your Canon Powershot

Looks like programming has gone everywhere. Pretty soon, we should be able to hack into our car and make it do fancy things but for now, I found this interesting article on how to hack into your Cannon Powershot camera - found here.

Sunday, September 14, 2008

Working with Netbeans

Two of the most popular free Java IDE available are Eclipse and Netbeans. Personally, I prefer Netbeans because it is a lot similar to Visual Studio.

I have been using it since version 3.x and it is currently version 6.1.

.jarContent
In version, 5 and below, it has its only proprietary way of compiling into jar file. It has a a binary file with the extension .jarContent. This file content contains all the information needed to build build and create the jar package.

Pro:
I am able have many java files in one folder and create multiple .jarcontent file to create different .jar application with it (by right clicking on the .jarContent file and select build jar).

Con:
I cannot build the .jar file without netbeans since the manifest file is with the .jarcontent file and I need a special editor to access it.


Ant
In Netbeans 6.0 was converted to use ant instead. This proved have its own challenges. One thing is migrating my old code to the new environment. One option is to create project from existing sources but this did not work very well with me because I was not able to put my folder in the right place.

A much easier way is to:
  1. Create a new Java Project.
  2. Create the package with the src folder.
  3. Copy the original code into the package folder. So for example the package is com.blogspot.programmersjournal, the folder will be src\com\blogspot\programmersjournal\ inside the project folder.
I use SourceSafe to keep all my codes, I found that the critical files needed for the project are:
  • build.xml (which is in the root folder of the project)
  • nbproject folder (which contains the settings needed by netbeans).
  • src folder (which contains the source code).
The other folders are not necessary.
  • dist folder is deleted and recreated when you do clean and build.
  • libraries contains the .jar files you refer to.
  • Test Package is when you want to do unit test.




Other Netbeans 6.x Info

When you create an application, the manifest file is automatically generated for you. However, when you create a library, the manifest file is not automatic. You will need to create it manually (as a blank file) and then add the line inside the project.properties file just after meta.inf.dir entry:

manifest.file=${src.dir}/manifest.mf

I always like to make sure there is a manifest file in all my jar files so that I can check on the version.


By default, if you add reference to other .jar files, netbeans will create a lib folder below the dist folder and put a copy of those .jar files there. At the same, time, it will add lib/... inside the manifest file.

You can override it by placing your own Class-Path: inside the manifest file then the NetBeans will not override it. I do this because I am used to putting my reference library in the parent folder rather then in the child folder.

A few additional things I like about Netbeans are:
  • The plugins. It has a very rich library of plugins include UML, JavaFaces etc.
  • You can easily switch between different versions of Java - by clicking Tools->Platform.
  • It supports cvs, mecurial and subversion (Even though I use Visual SourceSafe in the office but I use subversion at home).
  • Its intellisense is as good as the ones in Visual Studio.
  • Formatting capability is good.
  • It doesn't add reference to strange libraries. JDeveloper from Oracle does that when you create a windows application.
Things I don't like:
  • With the new ant build, I have to create a different folder for each application project I am working on (even if it only contains one .java file.
  • Loading is slow (it chews up a lot of memory), so not useful if your computer is slow.
  • I have not been able to get the debugging to work correctly with line parameters. (it works fine without parameters).

Wednesday, September 10, 2008

Enabling Command line Completion

This is obtained from this site.

Command completion allows you to type a portion of a file name, and then have the system complete the rest by pressing the specified key. This is similar to the UNIX shell command completion feature.

Open your registry and find the key below. Create a new DWORD value, or modify the existing value, called 'CompletionChar' and set it to equal the hex value of the required control character. For example to use the TAB key set the value to '9', to use BackSpace set the value to '8', to use Ctrl-D set the value to '4' and for Ctrl-F set the value to '6'.

Exit your registry, you may need to restart Windows for the change to take effect.

Now when you type the first part of a filename then press the specified key, Windows will complete the filename for you. Pressing the key multiple times will scroll through all the options.

|NameTypeData|
|(Default)REG_SZ(value not set)|
|CompletionCharREG_DWORD0x00000009 (9)|
-
|HKEY_CURRENT_USER\Software\Microsoft\Command Processor|
-
Registry Settings
User Key: [HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
System Key: [HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor]
Value Name: CompletionChar
Data Type: REG_DWORD (DWORD Value)
Value Data: Hex Control Character

Disclaimer: Modifying the registry can cause serious problems that may require you to reinstall your operating system. We cannot guarantee that problems resulting from modifications to the registry can be solved. Use the information provided at your own risk.

Monday, September 8, 2008

Classic ASP utilities - Sep 2008

The following are some functions that will facilitate coding in Classic ASP:
Serial Date

function serialdate(pdate)
' Parameter pdate is a date variable
' returned value is YYYYMMDD HH24MI
' uses function leadzero fill in leading zero
DIM tstr

tstr = year(pdate) & leadzero(month(pdate)) & leadzero(day(pdate)) & _
" " & leadzero(hour(pdate)) & leadzero(minute(pdate))
serialdate = tstr
end function


CheckBox
'==================================================================
' Subroutine to create a checkbox and decide if it should be selected or not
'==================================================================

Sub getCheck(cInput, cVal)

' Parameters are as follows:
' cInput - checkbox name
' cVal - status of checkbox (on or not)

Response.Write "<input type=""checkbox"" name="""
Response.Write cInput
Response.Write """"
if (cVal = "on") then
Response.Write " checked"
end if
Response.Write ">"
End Sub


To use above, you may create a checkbox as follows:



Check Buffer then flush the buffer
'==================================================================
' For checking the buffer count to decide if a response.flush is required
'==================================================================

function bufCheck(tCount)
tCount = tCount + 1
if (tCount > BuffLim) then
response.flush
bufCheck = 0
else
bufCheck = tCount
end if
end function


To use the above, declare a counter and a bufferlimit:

Dim cnt
Dim BuffLimit
BuffLimit = 50 'or whatever number you want



and then increment each time you do a response.write and then call the routine as follows:

cnt = cnt + 1
bufCheck(cnt)


This will reduce the amount of memory used on the server by flushing the buffer to the client once the limit is hit, otherwise it will wait until the asp is fully executed before sending the content to the client.

' =============================================================================
' Function to pad the string so that it has the length of tlen
' =============================================================================

function sp_pad(tString, tlen)
sp_pad = tString + space(tlen - len(tString))
end function


Radio Button
'============================
' For setting default value
'============================
sub defval(pfld, pval)
if pfld = "" then
pfld = pval
end if
end sub

'==================================================================
' check to see if the word checked should be written - used in conjunction
' with a radiobutton
'==================================================================

sub checked(pVar, pVal)
if pVar = pVal then
Response.Write("checked")
end if
end sub


Example of use is as follows:

<%
Dim cSel
cSel = request.form("carType")
defval cSel, "saloon"
%>

<input type="Radio" name="carType" value="saloon" <% checked cSel, "saloon" %>> Saloon
<input type="Radio" name="carType" value="4wd" <% checked cSel, "4wd" %>> 4 Wheel Drive

The above will look like this:

Thursday, September 4, 2008

Adding a dailog box to a webcontrol

To create a dialog box in a asp.net page, we need to use javascript to run on the browser and to add a javascript to a web control, we use webcontrol.attributes.add as in btnDelete.attributes.add("onclick", "return confirm('Are you sure you want to delete?');")

It is important when placing the javascript to end it with a ";". This is because, if the webcontrol autopostback attribute is set to true, the .Net will automatically add "__doPostBack(..." javascript to the control. The ; will be a valid divider to allow both javascripts to coexist and your added javascript will run first.

As for the rest of the java script, you can either add into your asp.net page via the literal webcontrol or code it directly into your aspx file.

One example to remind the person to add skirting in an automobile purchasing website could be as follows:

rbCarOpt.Attributes.Add("onclick", "alert('Did you remember to add skirting?');")

When the person selects Car (as opposed to truck), it will pop up a dialog asking if the customer remember to add the skirting option.

Addendum

If you want to have the alert appear in two lines then add \n to add new line. As in:

rbCarOpt.Attributes.Add("onclick", "alert('Did you remember to add skirting?\nThis will make your car look nicer.');")

will appear like this:


Wednesday, September 3, 2008

Java Configuration File vs .Net Configuration File

.Net

When using ASP.Net, the configuration file is known as web.config and is located in the root of the application folder (virtual folder designated application in IIS). For applications, the config file will follow the name of the application so if your Application is called StartProg.exe the config file will be StartProg.exe.config.

The config file is a xml file and the content will look something like this:
<configuration>
<appSettings>
<add key="orString" value="TestApp is logsmith;aaxx" />
</appSettings>
</configuration>
There are other sections in .Net configuration but I am only going to focus on the AppSettings.

To retrieve the information, you use: System.Configuration.ConfigurationSettings.AppSettings("orString").

In Framework 2.0, this has been change:
System.Configuration.ConfigurationManager.AppSettings("orString").

The value in the parameter is case sensitive. To retrieve the value, you do as follows:

Dim tmpStr As String

tmpStr =
System.Configuration.ConfigurationSettings.AppSettings("orString")

Java

Java does not have a default file naming convention, you can name it anyway you want. But to ease my finding, I will follow the .Net method which is to name the config file similar to the .jar file. So if the jar file is StartProg.jar, I will name my config file as StartProg.config.

In Java, your config file can be a xml or not. Here is a little program, I wrote to test the concept:

package com.testJavaApp;

import java.io.IOException;

/**
*
* @author strovek
*/
public class clsTest {

static String fName = "clsFile.config";
static String fName2 = "clsFile.config2";

public String getVer() {
String vVer = new String();

java.lang.Package p = this.getClass().getPackage();
vVer = p.getImplementationVersion();

return vVer;
}

public void writeProp() throws java.lang.Exception {
try {
java.util.Properties vProp = new java.util.Properties();
String tmpStr = new String();

tmpStr = "TestApp is logsmith;aaxx";

vProp.setProperty("orString", tmpStr);
java.io.FileOutputStream configFile = new java.io.FileOutputStream(fName);
vProp.storeToXML(configFile, "Configuration File");
configFile.close();
configFile = new java.io.FileOutputStream(fName2);
vProp.store(configFile, "Second File");
configFile.close();


} catch (java.lang.Exception ex) {
throw new java.lang.Exception("[clsTest01]" + ex.getMessage());
}
}

public void readProp() throws java.lang.Exception {
try {
java.util.Properties vProp = new java.util.Properties();
java.io.FileInputStream iFile = new java.io.FileInputStream(fName);

vProp.loadFromXML(iFile);
String tmpStr = new String();
tmpStr = vProp.getProperty("orString");
System.out.println("ConnString is \n" + tmpStr);

vProp = new java.util.Properties();
iFile = new java.io.FileInputStream(fName2);
vProp.load(iFile);
tmpStr = vProp.getProperty("orString");
System.out.println("ConnString is \n" + tmpStr);



} catch (java.lang.Exception ex) {
throw new java.lang.Exception("[readProp01]" + ex.getMessage());
}

}
}



The config file generated are as follows:

XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Configuration File</comment>
<entry key="orString">TestApp is logsmith;aaxx</entry>
</properties>


nonXML

#Second File
#Fri Aug 29 11:43:43 SGT 2008
orString=TestApp is logsmith;aaxx

As you can see from the sample program, java.util.Properties is the class to read and write the configuration file. A few things to note:
  • use .loadFromXML if the configuration file is in XML format.
  • use .load for nonXML.
  • use FileInputStream to read, then you can specify the config file name to load
  • use FileOutputStream to write the configuration file (the whole configuration).
  • use .storeToXML to create a XML configuration file.
  • use .store to create a nonXML configuration file.
I use the above sample file to provide me a template to create subsequent configuration file for my other projects.