Sys.Application is null or not an object 
So recently I started getting the error "Sys.Application is null or not an object" when I debug my .NET website application. A javascript error? Since when do applications die on a javascript error?

I found a few references on the web to similar problems. But the best solution seemed to be to create a blank website project and line-by-line compare the web.config file with my broken one. The error was in fact in the document's root tag. Here was the error:

Broken File:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

Corrected File:

<configuration>

Apparently it had something to do with a long lasting project that got upgraded from 2.0 to 3.5.
  |  permalink  |  related link  |   ( 2.9 / 198 )
Generic Equals Function for Comparing Two Objects in .NET 
So I have two objects in .NET. They are loaded from my database with a bunch of values as members (fields, in .NET parlance). I'd like to know if the two objects are identical.

In .NET, two objects only == each other if they are the same reference. So a pointer to string A won't equal a pointer to string B if the contents of the two are the same but they reside in a different place in memory. Instead, you must do this:


string a = "test";
string b = "test";
if (a.Equals(b)) { something }


This is all well and good, but what happens when I have two RegionObjects loaded from my database? One way to do this would be to write a custom Equals function that looks like this:


class RegionObject
{
public string country;
public string postal_code;

public override bool Equals(RegionObject obj)
{
if (country.Equals(obj.country) && postal_code.Equals(obj.postal_code)) return true;
return false;
}
}


This works great until I start adding new fields to the RegionObject. Then, I have to remember to add a new test each time I add a new field. I don't like that.

Instead, here's a generic function that gets the job done for any compound object using .NET reflections:


public static bool AreEqual(object o1, object o2)
{
// Make sure both classes are of the same type
Type _t = o1.GetType();
if (!o2.GetType().Equals(_t)) return false;

// Compare all properties and ensure they're the same
PropertyInfo[] props = _t.GetProperties();
for (int i = 0; i < props.Length; i++) {
object value1 = props[ i ].GetValue(o1, new object[] { });
object value2 = props[ i ].GetValue(o2, new object[] { });
if (!value1.Equals(value2)) {
return false;
}
}

// Compare all fields (members) and ensure they're the same
FieldInfo[] fields = _t.GetFields();
for (int i = 0; i < fields.Length; i++) {
object value1 = fields[ i ].GetValue(o1);
object value2 = fields[ i ].GetValue(o2);
if (!value1.Equals(value2)) {
return false;
}
}

// Got this far - they must be identical!
return true;
}


  |  permalink  |  related link  |   ( 3 / 78 )
Sending an Email with an Absolute URL in ASP.NET 
So I'm writing an authentication system with notifications. I want to send an email to the user and say "Click on this link to authorize your account."

The best way I know of to do this is to create a GUID, store it in the database, and email a link to the user with that GUID. When the user clicks on the link, verify that the GUID is correct and then flag their account as authenticated.

Since I always forget how to do the easy things, here's how to construct an absolute URL for use with your email message.


string url = HttpContext.Current.Request.Url.Scheme + "://"
+ HttpContext.Current.Request.Url.Host + ":"
+ HttpContext.Current.Request.Url.Port
+ HttpContext.Current.Request.ApplicationPath + "/AuthorizeYourAccount.aspx?userid=USERIDTOAUTHENTICATE&securitykey=GUID";


  |  permalink  |  related link  |   ( 3 / 255 )
Nifty Button Trick 
I hate the way the default HTML buttons look in Internet Explorer. They stretch and they're awful. Here's a great trick for new buttons:

http://www.oscaralexander.com/tutorials ... h-css.html

And if you're using C# in ASP.NET like I am, here's how to define the button he cites:


<asp:LinkButton ID="login" CssClass="formbutton" runat="server" width="200" Text="<span>Login</span>" OnClientClick="this.blur();" OnClick="login_click" />


Here are the CSS definitions you'll want:


.formclear { overflow: hidden; width: 100%; }
a.formbutton { font-family: Arial, Helvetica, Sans-Serif; background: transparent url('../images/button_right.gif') no-repeat scroll top right; color: #444; display: block; float: left; height: 24px; margin-right: 6px; padding-right: 18px; text-decoration: none; }
a.formbutton span { font-family: Arial, Helvetica, Sans-Serif; background: transparent url('../images/button_background.gif') no-repeat; display: block; line-height: 14px; padding: 5px 0 5px 18px; }
a.formbutton:active { background-position: bottom right; color: #000; outline: none; }
a.formbutton:active span { background-position: bottom left; padding: 6px 0 4px 18px; }

  |  permalink  |  related link  |   ( 3.1 / 153 )
Simple Javascript IsNumeric function 
I like the simple tips the best. I found a great IsNumeric function here - http://www.codetoad.com/javascript/isnumeric.asp - and streamlined it a little bit.


function IsNumeric(sText)
{
var ValidChars = "0123456789.";
for (i = 0; i < sText.length; i++) {
if (ValidChars.indexOf(sText.charAt(i)) == -1) {
return false;
}
}
return true;
}

  |  permalink  |  related link  |   ( 3.2 / 192 )

Back Next