Epoch Time in C# / ASP.NET 
I had a flash developer request the "Epoch" time from my web service. What is it? Epoch time is a Unix convention - the number of seconds elapsed since Jan 1, 1970. As good a time as any. Here's a function to produce it:

public static int MakeEpochTime(DateTime t)
{
TimeSpan ts = (t.ToUniversalTime() - new DateTime(1970, 1, 1));
return ts.TotalSeconds;
}


Thanks goes to Brad Abrams, who wrote nearly exactly the code I wanted. The code above is a slight modification of his.
  |  permalink  |  related link  |   ( 3 / 383 )
Installing Windows Server 2003 R2 - "Post-Setup Security Updates" 
The article on Microsoft Technet

Something I always forget about during the Windows Server setup process is the post-setup security updates. It tells you that all incoming network connections are disabled until you're finished patching the server with the latest and greatest fixes.

The best way to fix this is to run the Security and Configuration Wizard, which takes a bit of time but guarantees that your server is setup according to your preferences.
  |  permalink  |  related link  |   ( 3 / 232 )
Visual Studio "Cassini" won't recompile your AJAX PageMethods 
So, you're working on a .ASPX web page that includes AJAX PageMethods. You're trying to debug it, but every time you make a change, you get Javascript errors that tell you "PageMethods is not defined". What gives?

The answer is that Cassini (the mini-web-server bundled with Visual Studio) and Internet Explorer don't work together well to recompile your page. For some reason, IE keeps your old web page cached, which had the old version of your PageMethods in memory.

Save yourself some time and do all your PageMethods development early - at least, you need to define the functions and name them. If you don't need to change your PageMethods' names, or their parameter lists, then you won't have this problem often.

Just in case, here's a checklist to make sure your PageMethods are properly set up.

1) You must include a reference to System.Web.Services.

2) You must have a [WebMethod] tag on your function.

3) Your function must be static and public.

This is the bare minimum .ASPX.CS source code that you need to make your PageMethods work:

using System.Web.Services
...
[WebMethod]
public static string MoveNode(int node_id, int new_parent_id)
{
...
}


4) Make sure your ScriptManager is flagged to enable page methods.

<asp:ScriptManager ID="ScriptManager1" \runat="server" EnablePageMethods="true" />


5) If all else fails, you can follow these four steps to guarantee that your PageMethods will be properly recompiled.

First, stop debugging and close all Cassini instances.

Second, in Internet Explorer, click Tools | Internet Options. Click on "Delete..." in the Browsing History section. Click on "Delete Files", then click "Yes" to confirm. This forces Internet Explorer to empty your page cache, regardless of what setting you use for your temporary Internet files flag.

Third, tell Visual Studio to do a "Rebuild All".

Fourth, launch your app again and all should be well.
  |  permalink  |  related link  |   ( 2.9 / 212 )
Split CSV line into an array 
Something that happens more often than I'd like to admit: let's say I have a CSV file that needs to be broken out into an array. Here's a function to do it.

static string[] SplitCSV(string s)
{
List<string> list = new List<string>();
StringBuilder work = new StringBuilder();
for (int i = 0; i < s.Length; i++) {
char c = s[ i ];
if (c == '"') {
int p2;
while (true) {
p2 = s.IndexOf('"', i + 1);
work.Append(s.Substring(i + 1, p2 - i - 1));
i = p2;

// If this is a double quote, keep going!
if (((p2 + 1) < s.Length) && (s[p2 + 1] == '"')) {
work.Append('"');
i++;

// otherwise, this is a single quote, we're done
} else {
break;
}
}
} else if (c == ',') {
list.Add(work.ToString());
work.Length = 0;
} else {
work.Append(c);
}
}
list.Add(work.ToString());
return list.ToArray();
}


However, be warned! CSV files are often broken. You have to decide in advance how you're going to handle situations like the following:

Jim,Robertson,says "Hi!" to everyone
Nina,Smith,doesn"t always use the correct single apostrophe
Bob,Alfalfa,tries"," really"," to use quotes properly
Jane,Kipling,"isn't ""bashful"" about using quotes - but forgets to end them


You can save yourself a lot of hassle if you know in advance what types of errors to expect, but I don't know of any particular CSV algorithm that works correctly in all cases.
  |  permalink  |  related link  |   ( 2.9 / 232 )
C# Property Accessors 
Let's say you've got an object and the name of a property you want. How do you get that programmatically? You could easily write something like this:

Animal a;
string propname;

if (propname.Equals("size")) {
return a.size;
} else if (propname.Equals("color")) {
...
}


However, to put it bluntly, that sucks. In Javascript you can just do "Eval("obj." + propname);". How do you do this in C#?

The answer is that we use the System.Reflection library.

using System.Reflection;
...
PropertyInfo pi = obj.GetType().GetProperty(propname);
object o = pi.GetValue(obj, new object[] { });


Now the variable o has the object's property value. Nifty! You can also set the appropriate parameter similarly:

pi.SetValue(obj, value, new object[] { });

  |  permalink  |  related link  |   ( 3 / 201 )

Back Next