A personal repository of technical notes. - CSC

DBNull Handling and Conversions

Invalid Conversions of DBNull.Value

string myStr = (string)dbRecord["DBCOLUMN"]; // Unable to cast object of type 'System.DBNull' to type 'System.String'.
int myInt = Convert.ToInt32(dbRecord["DBCOLUMN"]); // Object cannot be cast from DBNull to other types.
bool myBln = (bool)dbRecord["DBCOLUMN"]; // Specified cast is not valid.
bool myBln = Convert.ToBoolean(dbRecord["DBCOLUMN"]); // Object cannot be cast from DBNull to other types.


Valid Conversions of DBNull.Value

string myStr = dbRecord["DBCOLUMN"] as string; // Converts to null
string myStr = Convert.ToString(dbRecord["DBCOLUMN"]); // Converts to empty string

int myInt = (dbRecord["DBCOLUMN"] == DBNull.Value) ? 0 : (int)dbRecord["DBCOLUMN"]; // Integer to 0 if DBNull
int myInt = (dbRecord["DBCOLUMN"] == DBNull.Value) ? (short)0 : (short)dbRecord["DBCOLUMN"]; // Cast SQL smallint to C# int
bool myBln = (dbRecord["DBCOLUMN"] == DBNull.Value) ? false : (bool)dbRecord["DBCOLUMN"]; // Boolean to false if DBNull
myBln = (dbRecord["DBCOLUMN"] == DBNull.Value) ? false : (dbRecord["DBCOLUMN"].ToString().ToUpper() == "Y"); // String to Boolean with DBNull check
DateTime myDateTime = (dataReader["DBCOLUMN"] == DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(dataReader["DBCOLUMN"]); // DateTime to DateTime

References
?: Operator (C# Reference)
http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

Convert Class (System)
http://msdn.microsoft.com/en-us/library/system.convert(VS.80).aspx

as (C#)
http://msdn.microsoft.com/en-us/library/cscsdfbt(VS.80).aspx

Firefox IFrame Height

<style type="text/css">
HTML,BODY
{
height: 100%;
}
#MainFrame
{
width: 75%;
height: 90%;
}
</style>
<iframe id="MainFrame" src="about:blank"></iframe>

Can't Delete Firefox Bookmark Tags

Workaround to delete tag that will not go away:

1) Remove tag from all known bookmarks
2) Type tag in location bar (for me, an old bookmark appeared with tag applied)
3) Select location
4) Bookmark location by clicking on the star
5) Click on star again and select "Remove Bookmark" button.

CSS Display vs Visibility

display: none;
display: block;
display: inline;


VS

visibility: visible;
visibility: hidden;


Unlike display:none;, with visibility: hidden; objects that are not visible still reserve the same physical space in the content layout as they would if they were visible.

References
display Property (A, ABBR, ACRONYM, ...)
http://msdn.microsoft.com/en-us/library/ms530751(VS.85).aspx

visibility Property (A, ADDRESS, APPLET, ...)
http://msdn.microsoft.com/en-us/library/ms531180(VS.85).aspx

C# Casting

() Operator (C# Reference)
http://msdn.microsoft.com/en-us/library/0z4503sa.aspx

double x = 1234.7;
int a;
a = (int)x; // Cast double to int


Remarks: A cast explicitly invokes the conversion operator from one type to another; the cast fails if no such conversion operator is defined.


as (C# Reference)
http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx

string s = someObject as string;
if (s != null)
{
// someObject is a string.
}


The as operator is similar to a cast operation; however, if the conversion is not possible, as returns null instead of raising an exception. More formally, an expression of the form,

expression as type


Convert.ToInt32 Method (System)
http://msdn.microsoft.com/en-us/library/system.convert.toint32(VS.80).aspx

Return Value: A 32-bit signed integer equivalent to the value of value, or zero if value is a null reference

Terminal Services Remote Desktop Command Line

References
Terminal Services commands (WinXP Help and Support Center)
mk:@MSITStore:C:\WINDOWS\Help\ntcmds.chm::/ts_cmds.htm

Terminal Services query commands: Terminal Services
http://technet.microsoft.com/en-us/library/cc755411.aspx

Examples
C:\>query user /server:123.123.123.123
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
user1 0 Disc 23 7/23/2008 3:11 PM
user2 rdp-tcp#107 1 Active 8 7/22/2008 10:23 AM
user1 rdp-tcp#102 3 Active 23 7/24/2008 10:20 AM

C:\>query session /server:123.123.123.123
SESSIONNAME USERNAME ID STATE TYPE DEVICE
user1 0 Disc rdpwd
rdp-tcp 65536 Listen rdpwd
console 4 Conn wdcon
rdp-tcp#107 user2 1 Active rdpwd
rdp-tcp#102 user1 3 Active rdpwd

C:\>reset session 0 /v /server:123.123.123.123
Resetting session ID 0
Session ID 0 has been reset

mstsc.exe /console
Connects to the console session of the specified Windows 2000 Server.


DOS BAT file utility source code for querying session:

@echo off
echo Server=%1
echo.
query session /server:%1
echo.
query user /server:%1
echo.
echo Reset syntax:
echo reset session # /v /server:%1
echo.
pause

Create shortcut to call bat file and pass IP address in target. Example:
"C:\MyUtilities\query session.bat" "123.1.2.3"

shell:folder Shortcuts

From Start/Run or from Windows Explorer address bar in WinXP:
shell:sendto
shell:desktop


Some registry locations of shell folders:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders

References
Note: I could not find this documented on any Microsoft web site.