Thursday, April 27, 2006

Today's .Net Nuances (Crystal, Old IIS, SQL @@ERROR )

Well to me anyway.

1. When using the latest version of Crystal Reports, save yourself the 2 hours I wasted today learning not to put multiple datatables in my report data schema. This caused great confusion for the report tool, even though one of the overloads to the SetDataSource() function is with a DataSet parameter [by definition means can have many tables]. My day went better after simply joining the parent and child tables on the SQL Server and letting the report tool clean up the mess of grouping at the proper levels and computing subtotals.

2. When upgrading a website on an IIS Server from the .Net Framework 1.x to 2.x, remember that if the site is only a Virtual Directory, not a full Site, then its Framework version MUST match that of the other Virtual Directories. UNLESS, you go to the trouble of creating Application Pools for the different Virtual's which allows you to upgrade in the chaotic fashion I have unwittingly employed.

3. Remember to check @@ERROR in your stored procedures on SQL Server. This can be helpful for say, when you want to know if you should COMMIT or ROLLBACK a transaction. I am quite embarassed about forgetting this, but luckily in only one small place in my current app.

Monday, April 24, 2006

Thursday, April 20, 2006

Some obscure .NET tips

1. Avoid declaring page class variables as STATIC. This creates all kinds of confusion, because they end up shared across the whole application for every rendering of that page. A coworker of mine did this and we kicked it around for a few minutes before I convinced her to use VIEWSTATE or SESSION as a persistence mechanism.

2. Don't assume pressing enter will cause a BUTTON object to get a click event when you are inside of a textbox. A POSTBACK, yes, but a button click no. I trap that event and send it to the button in a javascript snippet, but am sure there are other ways to do it as well.

3. Use the AS casting technique for object variables. If you use the double parens (DataSet) for example, and the object is not hydrated, an exception will be thrown. Using the AS (myThing AS DataSet) for example, the Framework will convert your object to NULL. Traditional cast or a CONVERT function is the way for Value types.

4. In C# use the USING wrapper around DISPOSABLE types. For example :
Using (SQLConnection myConnection = new SQLConnection) {
bunch of code
}
// This is just cool and the right way to get disposal. I am new to this style.

5. And read this somewhere in Google, but useful for maintaining sanity, forget all about using .EQUALS for comparing VALUE types. It reads badly and runs slower because the system looks all around for an overridden implementation of the EQUALS comparison function.

6. Use STRING.EMPTY or use STRING.LENGTH for comparison to string variables instead of the dreaded double-quotes. Performance, Localization and readability.

My $0.02 on some things I am seeing lately, if I may be so bold.

Wednesday, April 19, 2006

Figuring out the whole Image Upload thing



My favorite episode of South Park, Eric Cartman with the Mexican Staring Frog of Southern Sri Lanka.

(Needed to make the uploading page a Trusted Site in IE)

Poor man's code generator

Much is being said lately about CodeSmith, which I am sure is an awesome tool for building the data access layer for your application, and maintaining it.

I only talk to SQL Server databases lately, so I am most familiar with its handy INFORMATION_SCHEMA.Columns view. This can give you the column listing for any table. A companion to this is the SYSOBJECTS table which harbors a list of the parameters in your Stored Procedures. Remember the XTYPE='P' in your where clause.

As the picture begins to render on how to do this, I want to tell somebody really bad about using CodeDom namespace for building the actual code. You could use StringBuilder to concatenate a big bunch of text, but the CodeDom does it more elegantly.

Getting started it always the hardest with new white canvas so I post the opener :


// Declare a Class
CodeTypeDeclaration ct = new CodeTypeDeclaration("data");
ct.IsClass = true;


Another tricky thing for me was making the code concise by having the declaration and the instancing of the variables on one line :


// Declare a Connection
csne = new CodeSnippetExpression("this.ConnectionString");
// You need a Param array of things passed to a constructor.
CodeExpression[] cParams = { csne };
coc = new CodeObjectCreateExpression("SqlConnection", cParams);
// Then .. all the instancing and declaring on one line :
codeVar =
new CodeVariableDeclarationStatement("SqlConnection", "sqlConnection", coc);
cmm.Statements.Add(codeVar);


Finally, building the actual class code from your heap of objects in memory could be elusive since you have been buried in the CodeDom bits for a few hours :


// This could be a VB thing as well.
CodeDomProvider prov = new Microsoft.CSharp.CSharpCodeProvider();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
TextWriter tw = new StringWriter(sb);
prov.GenerateCodeFromMember(ct, tw, null);


HTH
- Brian

Thursday, April 13, 2006

First Bee in my bonnet (Working with Trusted Sites..)

I have long struggled with security in Internet Explorer, having been infected a few times since my Windows 95 days by various viruses. Not to say they were all the fault of IE, but some were. Since then, I am very careful which sites I allow to run scripts or ActiveX controls on my machine.
Because I still like Internet Explorer better than Mozilla and really appreciate the "Trusted Sites" zone feature, I created a solution to all the dialog boxes required to get a new site added to the zone. It involves a small piece of script that is run from a custom context menu item I add to the standard ones. Internet Explorer allows for adding a link to an HTML page you write via a registry entry which I add with the following :


' BRIAN ONEIL
Dim fsDim sh
Set sh = CreateObject("WScript.Shell")

sh.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\ MenuExt\ Add To &Trusted Sites\","file:\\C:\Program Files\TrustedSiteContextMenu\TrustedSiteAdder.HTML"

sh.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\ MenuExt\ Add To &Trusted Sites\Flags",1,"REG_DWORD"
Set sh = Nothing
MsgBox "Registry Entries Created."


The HTML file referenced by the registry contains the following script for adding the current site you are viewing to your Trusted Sites list :


// Brian O'Neil

// alert("Adding the current page to Trusted Sites...");
var parentwin = external.menuArguments;
var doc = parentwin.document;

var urlParts = doc.URL.split("/");
var httpColon = urlParts[0].substring(0,urlParts[0].indexOf(":"));

var domain = urlParts[2];
var domainParts = domain.split(".");
var domainName = domainParts[domainParts.length-2] +
"." + domainParts[domainParts.length-1];
var prefix =
domain.substring(0,domain.indexOf(domainName)-1);

Sh = new ActiveXObject("WScript.Shell");
var key =
"HKEY_CURRENT_USER\\ Software\\Microsoft\\Windows\\ CurrentVersion\\ Internet Settings\\ ZoneMap\\ Domains\\"
key = key + domainName;
key = key + "\\" + prefix;
Sh.RegWrite(key + "\\" + httpColon, 2, "REG_DWORD");

Sh = null;
parentwin = null;
doc = null;

alert("To Undo this action, remove key "
+ key
+ " from the Registry.");
window.close();


All of the above assumes a directory called
C:\Program Files\TrustedSiteContextMenu


If anybody is interested, I have a WinZip file which decompresses all that is needed for this into the proper folders. Another caveat I forgot to mention is the browser needs to have permissions to execute script on the "Local Intranet Zone".

Had to have my own one of these (re: Blogging)

I have named this Blog after some technology used in my family's business. To learn more about foundry, visit my brother's site http://www.okfoundrycompany.com. I have to imagine if the foundry business were less industrial and more boutique, cafe' kind of thing, there would be a restaurant called "The Cope and Drag". Or at least a shady night club by that name.

My name is Brian and I work in the software business in Sterling, VA near my home of Reston, VA. My language of choice is C# and the platform is Microsoft .Net. I often have a craving to tell somebody about some of the things I learn to do in developing ASP. Net code or javascript, or just refer somebody to a site where I read such an instruction. Maybe this Blog will work well for that. Let's see. If not, I will post some of my other interests and worries here.

Welcome!