Wednesday, October 31, 2007

Nice way to center HTML element

Just set it's margin css property to "0px auto". Setting "auto" for margin-left and margin-rigth automatically centers elements in all modern browsers except IE5. Here is workaround for IE5.

Wednesday, October 10, 2007

Cute method for logging exceptions

Here is a cute method which returns a string composed of exception (and all its inner exceptions) message and stack trace:


public static String GetExceptionUberStackTrace(Exception e)
{
if (null == e) throw new ArgumentException("Exception parameter shouldn't be null!");
String result = "";

do {
result += String.Format("Message: {0}{1}", e.Message, Environment.NewLine);
result += String.Format(
"Stack trace: {0}{1}{2}",
Environment.NewLine,
e.StackTrace,
Environment.NewLine
);

e = e.InnerException;

if (null != e)
{
result += String.Format("{0}Inner exception:{1}",
Environment.NewLine, Environment.NewLine);
}
else
{
break;
}

} while (true);

return result;
}

Friday, September 14, 2007

BLOB field size && MS SQL

You can use datalength function to get BLOB (or any other field or expression) size in bytes. For example for 2 it will return 4 (2 is integer, which takes 4 bytes on my system), for '2' it will return 1 ('2' is one char, 1 byte-per-char on my system). For BLOB or IMAGE it will return actual size of data stored in this field. Example:

SELECT datalength(ITEM_THUMBNAIL) from members WHERE ID_MEMBER=177;

It is really important to close resouces

It is really important to close resouces that aren't needed anymore - for example JDBC ResultSet's, Statement's and Connection's. It not only frees memory

available for your code but only give ability to make new recource connections :) (For example, MS SQL and other DBMS has limit on connection qunatity - if

your reach this limit you wan't be able to make new connections till old aren't closed). Here is simple example how to properly close JDBC resources after last usage:


Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
... DB access code

} catch (Exception e){
... logging, something else

} finally {
//close ResultSet object
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
Log.error(e);
}

//close Statement object
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
Log.error(e);
}

//close Connection object
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
Log.error(e);
}
}

Wednesday, September 12, 2007

nslookup - cute way to resolve domain name or ip

Till this moment I used ping command to resolve domain name. Of course there are dozens of free services on the web where you can resolve domain name or IP. But it is more convenient to use "nslookup" command which is available on modern Windows and Unix installations.

Friday, September 7, 2007

Cookie and IFRAME issue

Internet Explorer 6.0 (and maybe other browsers or other versions of IE) has a problem with sending cookies to IFRAME document if IFRAME and main documents have different domains. Looks like that no or not all (or not all the time, I haven't understood it completely) cookies will be sent to URL of IFRAME document. Simply remember that you'll have problems in such a case (for example IFRAME document won't recognize you as logged in user)

Tuesday, September 4, 2007

Converting .NET DateTime to Unix timestamp

Here is sample code which you can use to convert from .NET DateTime to Unix timestamp time format. It is based on TimeSpan object properties, TimeSpan object can be got for example when you substract one DateTime object from another:


public static String GetNowTimestamp(bool withMilliseconds)
{
DateTime now = DateTime.Now.ToUniversalTime();
DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

TimeSpan diff = DateTime.Now - start;
double value;
if (withMilliseconds)
{
value = diff.TotalMilliseconds;
}
else
{
value = diff.TotalSeconds;
}

decimal roundedVal = (decimal) Math.Round(value, 0);
return Convert.ToString(Convert.ToInt64(roundedVal));
}

Thursday, August 30, 2007

Re-throwing exceptions

When you want to pass your catched exception to higher level of exception-handling mechanism don't do something like


catch(Exception e){
Trace.WriteLine("{Message}");
throw new Exception("{Message}", e);
}


instead of this use the following construct, which will pass your exception to higher level:


catch(Exception e){
Trace.WriteLine("{Message}");
throw;
}

Wednesday, August 29, 2007

Calling methods from base class in Java

Don't forget that you can call method from the base class when you are overriding this method in derrived class! Simply use "super" operator for this purpose.


public AuthToken createAuthToken(HttpServletRequest request, HttpServletResponse response) throws UnauthorizedException{
...
//calling method from base class
return super.createAuthToken(request, response);
}