WordPress database error: [Got error 28 from table handler]
SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM wp_angel_posts WHERE post_date < '2010-09-07 01:00:46' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC


N00b blunders …

For the past two days, I’d been assigned the task to “clean-up” a search page, written in ASP.NET.

Apparently, the guy who wrote the code was fairly new to ASP.NET, although I’m tempted to say that he must’ve been fairly new to the concept of .. programming in general. Therefore, I decided to jot-down some of the blunders I saw in that code, for the sake any newbie that might ever come across this little blog of mine.

The ellusive NullReference:

This will never go away. Every new programmer will write code like the following.
lblIncId.Text = UINamesDic["Incident_ID"].ToString();

Boy, doesn’t anyone ever think that the value for the given key might be Null? Are they ever so confident that all the correct values will always be present in their IDictionary?

Even so .. the remedy is actually much simpler than the original code - and much more pleasing to the eye I think:
lblIncId.Text = (string)UINamesDic["Incident_ID"];

Redirecting:

Next, I saw this:

private void btFind_Click(object sender, System.EventArgs e)
{
Response.Redirect(....);
}

Jesus … why oh why …

Apparently, what the n00b didn’t understand at that point in time, was that in order for this event handler to be called, the whole page is being posted back to the server, the whole Control tree is being instantiated, events raised, and finally, after a while, the button handler gets called to … redirect to another page!!!

When in the ToolBox, just underneath the Button icon, there is a LinkButton, which is .. a link. You see, I’m not even suggesting writting the Html for the link yourself, I’m assuming you’re new to this, but .. for cryin’ out loud. Try to use some common sence at least.

The curse of the ViewState:

Ok, I do realize that ViewState exists for a very good reason. It’s meant to try and make your pages as stateless ( as in not using the Session ) as possible.

However … the ViewState is also a curse. Imagine a DataSet of 48500 rows, data bound to a DropDown list which has viewstate enabled. The actual values are written twice in the resulting html. Once in the actual <option> items, and once in the viewstate.

The result? A page that is 12Mb in size. What can you do about it? Clearly, you can’t store all that data in the view state. You must retrieve that data set from somewhere on each page call, and populate the drop-down yourself. you also must get the selected value from the Request upon PostBack, and set the selected value yourself.

However, there is an important choice to be made. Do you want to retrieve 48K rows from your database every time the page gets called ? Perhaps not.

Depending on the traffic of your site, you could actually cache the dataset in-memory ( the Application dictionary perhaps? Teh built-in ASP.NET Cache? or the Session, although that’s not such a good idea ). You must be very careful with such choices though. Bottom line is that there will be cases where exploiting your RDBMS to the fullest does make sence.

I’ll be following up this post, with other such goodies I find around in places, lurking underneath slick-looking ASPX pages :)

.. till then, may the source be with you ;)

O:]

6 Responses to “N00b blunders …”

  1. Ellis Says:

    Yeah…well maybe that was because the poor guy could not find any FREE information out there..like for example downloading the ASP manual..like you can do with open source scripting languages..such as PHP ;)

  2. Administrator Says:

    Well, you’ve just forced my hand.

    Apparently, the http://msdn.microsoft.com IMMENSE collection of reference, tutorials, webcasts, articles, how-to’s doesn’t mean much to you, huh ?

    Or perhaps the incomplete documentation you can find on the various out-of-the-beaten-path libraries for PHP is of the same quality? I beg to differ.

    Having used both, I can tell you … Reference and tutorials (completely free) are at their best when you’re talking about Microsoft technologies. A close second is Java, and by objective comparison, the reference you can get on PHP is a cookbook compared to a series of in-depth seminars from a Grand Chef … sorry. Invalid Argument Exception ;)

  3. kostis Says:

    Oh please!!! You can’t be serious mate! MSDN sucks big time. Yeah, i could propably agree that the reference is pretty thorough but the tutorials are aiming at the level of “hello world” kinda scripting.

    PHPs online manual is as detailed and comprehensive as it can get. It’s free, its online and it’s tested and updated daily by thousands of users and developers. And if you aren’t satisfied, try googling the net to find out EXACTLY what you’re looking for or perhaps visit sourceforge, freshmeat or a dozen other sites with free tutorials, source code, php classes…

    P.S. I enjoy the fact that you stress the fact that tutorials in MSDN are completely free. How did they miss that at Seattle HQ? ts ts ts…

  4. anjelinio Says:

    Well, you KNOW that I’m not the kind of person to start a flame. You also know that I will be the one to follow one through though. So … in response:

    “but the tutorials are aiming at the level of “hello world” kinda scripting”. Is that so? You’ll find “hello world” examples, if you’re looking for that sort of thing. If you’re looking for more serious examples, you can find extremely detailed mini-series fo tutorials, and that’s just by looking at MSDN.

    You should know that there is a major shift in direction inside Microsoft, as well as within the developer community. The Java people are coming back with a vengeance, and they’ve already overwhelmed the ranks of Microsoft people. Thus … you now can find detailed tutorials, how-to’s, reference, webcasts and whatnot that will satify even the most demanding developer.

    These days ( and since … forever actually ) Microsoft provides meaningful, proper guidelines to people. Actually, it is to M$’s best advantage to turn people away from old, VBScript-like programming. Better educated people make betterapplications, which in turn is to the advantage of the platform provider, a.k.a. Microsoft.

    Indicatively, I’m including my last two favourite links from the MSDN Web Site. One is about Security, and the other the Patterns and Practices column. Judge for your self the quality of the content.

    http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnpag2/html/tmwa.asp

    http://www.microsoft.com/resources/practices/default.mspx

  5. kostis Says:

    Yeah, i know that you’re not the flaming guy : ) And i’m not the ranting kind of guy (most of the times).

    I wouldn’t know about the shift in direction in Microsoft. Neither in PHP if one occured ’cause it’s quite hard to keep up with everything. I’m sure that the above links are quality stuff cause you say so. What i find troubling though is that besides MSDN, you haven’t noted any other resource for reference. Moreover, you are implying that before the JAVA people made their comeback, things were different in MSDNia (shortage in tutorials, fewer code, less content?)

    I’m with you, also, on the argument that it’s on MS best interest to turn people away from VB scripting-like programming (hear that people?). Better educated people make better application that at the bottom line make our life (the users that is) simpler, better and more efficient.

    And because I can’t help ranting, those two links above don’t validate as well-formed XHTML. Both of them return almost 50 errors each (document type declaration is missing, alt attributes are missing…). That is because .NET 1.0 produces horrendous HTML code, invalid markup and table-based layouts when it comes to forms (to mention a few). Fortunately, the next version of ASP.Net will produce valid XHTML 1.1 by default.

    My 5 cents…

  6. anjelinio Says:

    … jesus. It’s clear that you’ve never even tried Googling for .NET reference and tutorials.

    This is what Google returned me, when searching for ‘Xml Serialization .NET” … just try to convince me…

    “Αποτελέσματα 1 - 10 από περίπου 293.000 για Xml Serialization .NET.”

    Actually, there’s hundreds of sites on .NET, but since you people are clearly biased and talk before you’ve looked into the subject .. I’ll stop here.