Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Dev Blog - Johan Danforth

I'm Johan Danforth and this is my dev blog - a mix of .NET, ASP.NET, Rest, Azure and some other random coding stuff.

  • EQL - Emotional Query Language

    If computers had emotions... wouldn't it spice up life as a programmer if you could get better performance out of the boxes from being a bit nice to them? Imagine SQL Server having feelings, and you would have to ask it nicely to make it give you what you really want. Say you just installed the Emotional Query Language version and you need a list of names from the authors table in Pubs, and you tell SQL to:

    SELECT * FROM authors

    Server: Msg 2812123, Level 12, State 123, Line 1
    You forgot the magic word 'PLEASE'.

    Aha, you think. The bitch is in a bad mood today. So, ok, you change your query to something like:

    PLEASE SELECT * FROM authors

    Server: Msg 1231256, Level 534, State 341, Line 1
    Be more specific, you don't need all those columns, do you?

    Aww, come on! I know I don't need all those columns, but I might need them in the future! Ok, ok, I decide be more specific and change the query again (cross my fingers and hopefully it will even use a clustered index scan):

    PLEASE SELECT au_id, au_fname FROM authors

    Server: Msg 12335, Level 11, State 897, Line 1
    Busy serving IUSR_JENNY.

    What??! Jenny is using my database and you prefer to serve her? In a fit of rage you pull out your box of Oracle 9i CDs, all 23 of them, and wave them in a really threatfull way in front of the screen, screaming: "See all these Oracle disks!!? Yeah? Start serving me or I'll uninstall you, you miserable excuse for a program!"

    Which will probably make the situation worse... Come to think of it, EQL maybe isn't such a good idea after all.

  • Go Joseph Go

    A couple of days old, but still one of the funniest blog-posts I've read this morning. Joseph Duemer takes on "an infantile egoist", which has spammed his blog. Check it out here.

  • Formatting of Date and Time in .NET

    I don't know about you, and perhaps this is just an issue in non-english speaking countries, but the formatting of date and time has always been an issue in our old applications built with ASP and COM components. Especially VB6 components. Some developers settled with setting the default region of the server OS to whatever language the web application was supposed to be read in and then used VB functions such as Now, Time and Date to get and display the date or time formatted according to the regional settings. Well, this worked well until you had to move the application to another box or when some administrator or other changed the default regional settings to something else.

    The solution to this was to build or format the date and time strings yourself, either by concatenating or by using the built-in Format method. But now the Framework library contains a number of powerful features, which help the application developer present date and time strings in the correct format for the user. This is called Globalization, and achieved by setting the specific “culture” of the running process or the current thread. There is no longer need to build date and time strings manually, and I recommend web developers to use one of the following methods for setting the correct culture in their ASP.NET applications:

    1. Set the culture in Web.Config
    2. Set the culture of the current request thread
    3. Set the culture parameter of the ToString method

    I guess you can also set the culture of a Console or WinForm application in the application configuration file just as in the web.config, but I've not tried that yet.

    I always keep date and time data in a Date or a DateTime structure. The Date class is only available in VB.NET but the compiler will translate that into a DateTime structure. The DateTime contains a few convenient methods for formatting date and time strings, which I use most of the time:

    • ToShortDateString
    • ToShortTimeString
    • ToLongDateString
    • ToLongTimeString

    The value of the DateTime instance is formatted using different format characters and passed through the ToString method. As the .NET documentation for DateTime says, the return value for the ToShortDateString is identical to the value returned by ToString ("d", null).

    If a more fine-grained control is needed, you can use the overloaded ToString(format) methods or the static Format method from the Microsoft.VisualBasic namespace, which should be familiar to VB6 developers. I still prefer the different ToXxxxString methods provided by the DateTime class.

    To get the current date and time in VB.NET, you can use either the .NET DateTime or the Date class, as I wrote above. Or, you can use the Now property, which is part of the DateAndTime module from the Microsoft.VisualBasic namespace and also returns a Date object containing the current date and time. Something like this:


            Dim Date1 As String = DateTime.Now.ToShortDateString()
            Dim Date2 As String = Now.ToShortDateString()
            Dim Date3 As String = Date.Now.ToShortDateString()
     
    To get the current date and time in C#, you must use the DateTime structure, since the Date class doesn't exist: 


           string Date1 = DateTime.Now.ToShortDateString();
           DateTime dt1 = DateTime.Now;
           string Date2 = dt1.ToShortDateString();

    I guess you could import the Microsoft.VisualBasic namespace and use the Now function from it, but hey... :)

    Have to rush home now, so I'll post some samples of how to set the culture tomorrow or so.

  • Not much of a revolution

    GAHH, I've just witnessed two of the worst speakers in modern history at the Oracle/Java seminar. Ok, the first guy could talk, but it was so boring people starting to drift off after a couple of minutes. You've got this big theater with 400 programmers sitting there waiting for some cool code tricks, and this guy spends 30 minutes bragging about his company! Not a single row of code, not even a simple if-statement! The other guy was just embarrassing, no one had any idea what he was talking about, least himself. Poor guy... I guess it was worth it, since we all got to see the last Matrix movie afterward ;)

    Not sure why they call it Matrix Revolutions though, there wasn't much of a revolution in it. It had a great deal of action, and the big fight in the hangars of Zion was just amazing. I must admit I didn't really understand the ending. If someone can explain to me about the Oracle and that old guy in the end, please post a comment or two...

    Ah, what the heck, it was good action anyway :D

    Tomorrow, it's back to writing .NET guidelines. See if I can post something useful for once...

  • Sleeping with the enemy...

    This is my first post on the ASP.NET Weblogs, so first, thanks to Scott for letting me in ;)

    I'm soon off to enemy territory, a free seminar held by Oracle and Sun and lots of Java talk. The fact that they will end the seminar by showing the new Matrix movie has absolutely nothing to do with me going there ;)

    I'll let you know what I thought about the movie later tonight. Hopefully it'll be better than the second.