Code Nuggets: A Side-effect of Using DropDownList AppendDataBoundItems With Databound Items

Picture this: You have a webform with two DropDownLists, both of them are databound to some data you get from a database. Now the first DropDownList’s selection determines the contents of the second dropdown. So you simply do a AutoPostBack = true for the first dropdown and populate the second dropdown in the handler function. Right?

But wait. What if you are required to put in a static item in the dropdown? Something like “– Please Select –“ as the first item in the list to force the user to make a conscious choice. Hmmm, so you look around and find the nice little property named AppendDataBoundItems that will take care of that. All you have to do is declare the first (static) item in the Items collection in the designer (or put a <asp:ListItem> tag inside your <asp:DropDownList> tags) and set AppendDataBoundItems to true. This nice little property tells the DropDownList to add the databound items after the statically declared items, so you can have your happy little “– Please Select –” in your dropdown.

The Side-effect:

The side-effect becomes evident when you play around with your two dropdowns. Its immediately clear that something is not quite right. The AppendDataBoundItems property forces your dropdown’s items from the previous postback to be treated as static objects onthis display. Sort of where you get an ever-growing second dropdown with a hangover from the postback, which is clearly not what you wanted in the first place!

Continue Reading >>

Code Nuggets: Getting two SQL column values in a single column

Well this type of thing doesn’t come up very frequently (at least to me) and if you’re like me working on multiple databases it gets very frustrating when it does come up. Since I don’t use it very frequently, I tend to forget how to do it properly in the database system I am using at the time.

So it goes something like this: Suppose you have a simple SQL table called Users. Now this table has three columns, id (int), firstName (varchar) and lastName (varchar)…

Continue Reading >>

(Tech)LifeDotCom

Though I moved from Blogger quite a while ago, but just for old times’ sake I had kept my account active. For those who are new to this blog, I maintained a blog at Blogger quite some time, which can still be found here. I have now decided to turn into the technical version of LifeDotCom, aptly titled (Tech)LifeDotCom.
(I believe that’s apt enough, don’t know what you guys would think.)

Initially I wanted a separate dynamic page on this WordPress blog, but it only allows you to have static pages, if you need. Means you can have only one blog-like dynamic page and several static pages (which you can see if you look at the top of this page). So putting posts on a static page in the manner of a blog was a major pain in the backside, which was gawking at me. The solution I finally arrived on? Reuse Blogger.

I gather that Blogger has quite mended its ways with the glitches that were apparent in the Beta stage. So (Tech)LifeDotCom would mainly contain some thoughts about the tech landscape, with some snippets of code, and possibly (some) full-fledged tutorials. I only wanted to build a repository of snippets and thoughts and efficient ways of (technically) doing things which would be accessible to me, but I realize that shunning the community on little goodies is not a good thing. I wouldn’t promise to update that blog as regularly as I do this one (does that mean never at all?), but I’ll be posting updates now and then.

Most of the people that (still) read my blog are not technical at all, but hey, I updated the brief description at the top!

My Something Worthwhile…

Before coming to Singapore, when I was free in my last days in Karachi after leaving my job, I was involved for a while in the redesign of ApnaKarachi.com. A major part of the overhaul was the new section of ApnaKarachi Maps which provide street-level maps of the city of Karachi. I was in charge of developing the back end of the maps section and as well as managing the people involved in it. We planned to finish and deploy the site for the public before I’d leave for Singapore, but tweaks here and there made maps a little late. I completed the major chunk of the work when I was in Pakistan, and while in Singapore I generally just made things work accommodating the “wish list”.

The ApnaKarachi Maps are now live for sometime now. The site itself has been redesigned and different sections added to it. Its now “bigger and better” in the true sense.

The basic motivation behind the Maps was that Pakistan per se doesn’t have any worthwhile representation in major map sites like Google or Yahoo! Maps, so we intended to provide that kind of information and facility to our people. We have just started with Karachi, other cities would definitely on the way.

As everybody knows that I’m a pretty unassuming guy and I generally don’t do any kind of self-embellishment, but the purpose of this post is to solicit feedback and ideas to make the Maps better and more useful. It’s seldom that we do something worthwhile for our country/people, and while such an initiative is going on and you can be a part of it, then you must.

Please also spread the word around. This service is solely provided for the benefit of people, so the more people use it, the more we would be in the position to improve it.

PS: Don’t forget to say some nice words to me!

Elections, Selections?

After getting pretty much pissed off at Geo live streaming which I am simply starting to detest, I tried checking out the election results online. Pretty much to my horror, the first result on Google is about Politics of Yemen from Wikipedia. The second result is from some apparent “seer” who tries to predict future events and had written something about the elections on his blog a few days back.

At first glance, the first page of results doesn’t look really promising. There is this elections.com.pk link with the title of Pakistan Elections 2008 results, but you know how search engines work and you definitely check out the full link of the result; which apparently is results2002.php. So that result should go in the bin also but a little probing reveals that it is supposed to be a valid website with current results (I think so).

The whole motivation for this post was that people around the world aren’t generally taking interest in elections of our little homeland. Checking out Google Trends for the phrase “pakistan elections” tells you promptly that:

“Your terms – pakistan elections – do not have enough search volume to show graphs.

I feel that it’s only being treated like a chutti in Pakistan. I mean, even Benazir Bhutto assassination made it to the top searches in Google at least for that day. No such thing for elections. Nobody is interested in our elections. We don’t even have a proper website for elections. Okay there may be a proper website but its not Web 2.0. Its not glossy and shiny. Even the Pakistani population is not checking the results out online. It all comes down to the same conclusion: farcical elections.

Let’s see what happens when results are really consolidated. Who gets in and who is being kicked out. Who would become the master of a volatile nation, if its not decided already.

We are not cool…

The 7th Up

The Windows Internet Explorer 7 has got to be one of the dumbest browser I have ever used. Yep, dumber than Konqueror or Safari. I mean, I just spent the last five minutes searching for the “History” button.Internet Explorer 7 I could not find it anywhere on the toolbars, nowhere on the menus, and as despair was starting to hit home, there it was, a little arrow to the right of the “Forward” button. Pretty nifty, eh? NOT AT ALL! As far as I am concerned, being a “user”, I need the basic functionality visible to me, and I wouldn’t want to grope around to find the things I am looking for.

And the menu-bar-appears-when-alt-is-pressed mechanism is totally uncool. I am definitely not comfortable with web pages sliding down.

Buh, the aero look is a little cheesy. My advice: I really don’t see any reason for anyone to upgrade to IE 7. They’d be better off using Firefox. If any one is out there who is still loyal to Microsoft, then they should download the service pack 2 for IE 6. That’d make IE 6 better than its successor, and of course, without the sugary coat.

Note to self (dynamic controls ASP .NET)

…when populating a dynamic control (such as a dropdownlist dynamically), keep in mind the states of the page. Add that population before the Page_Init event for it to work correctly…

Blogger Babes

It turns out Blogger beta isn’t a wasted effort at all! For one thing, you get the "dog" (looks to me so) replacing the B of the Blogger. I realized only today that using the beta is a privilege accorded only to a few lucky ones (I didnt notice because privileges are so natural for me *snicker*).

As I said somewhere, it is good for the non-tech people, as it uses those AJAX enabled snippets to arrange layouts according to the user’s taste. The general population (who doesn’t even have the desire) wouldn’t now have to get their hands dirty with HTML and CSS. Adding custom code for statistics is just drag-and-drop, keeping the flow intuitive.

Close to hell for us techies, ’cause you cannot live if you cannot customize; and that’s just what Blogger doesn’t let us do at the moment.

The better thing is that, with the beta release, Blogger marks the shift to the dynamic web. Wonder why it has taken them so long, all the rest of the services are long established on it.

By dynamism of the web, I mean that you no longer have to publish your blog every time you make a change in the template. Old blogger used to publish pages of your blog to its server, and serve the appropriate pages whenever a request arrived. With the beta, the pages would be created on the fly, no more publishing.

All the bitching was due to the fact that I had recently delved into the Blogger codebase, and had come up with really interesting tricks like customizing the individual pages, recent comment box on the sidebar, intuitive layouts; and then *poof*… Blogger beta strips me of any right to modify my template.

These snippets are available if any of the old Blogger people (or the beta users who haven’t switched to layouts) want them for their blogs.

The Craze Beta

What is with everyone coming up with the beta editions of their offerings these days? Starting with Gmail Beta, we get Windows Live Mail Beta, Yahoo! Mail Beta, Yahoo! Photos Beta, Flickr Beta, and now, sweet Blogger goes beta too!

For many services, going beta just means getting AJAX-ified, with little or no improvement in features (Windows Live Mail leads the contest), only getting a face-lift and incorporating oh-those-cool-functions on the page that have nothing whatsoever to do with cutting the round trip time…!

With a sense of loyalty to the technology and the tech community (not to mention the need to be the first in trying things out – izzat ka maamla), I have officially switched to the new Blogger Beta. Switched – ’cause, ummm, beta could be exciting; officially – ’cause Blogger Help says you cannot opt out of the Beta!

So what does this beta has to offer over the old version? For one, the “privileged ones” now have a separate, red carpeted entrance at http://beta.blogger.com. Ordinary mortals should stick to their filthy swamps and keep using the lowly Blogger home page to log in.

The Blogger people have added support for tags – or “labels” as they call them. Der ayed durust ayed is the only thing that comes to mind.

And there is this supposedly cool Layout feature, that help you manage the “template” of your blog. Beware, the “template” word is hushed now, if you’re cool, you’ll have a layout.

So the layouts help users “drag and drop” widgets, or little snippets (previous posts, profile, description etc) on the page. Intuitive it is, but couldn’t achieve its full potential until the “Edit HTML” option starts working. Gives us “power users” that creepy feeling of helplessness!

That means my little blog page has some AJAX elements in it. How I have been dying to have a little Ajax, that would keep my dishes clean… Who said God doesn’t listen?!

Oh, and by the way, you’ll have to comment by choosing the “Other” or “Anonymous” option on the comments page, your logged in nick wouldn’t work here. Of course, what did you think? Its Beta!

Besides that, all the functionality is unchanged. They have tried to spruce up the Dashboard a little, with cramming the various blog-specific options, and the Blogger Buzz posts. Heck, they even forgot to fix the bug that occurs in the Rich Text Editor that it doesnt convert line breaks to hard line breaks :\ So it’d be better to stick to posting via e-mail!

Buss ooper say make-up kiya hai, under the hood its all the same!

Returning to the Craze Beta, I forgot to tell you: Flickr has gone a step ahead, its now Flickr Gamma!

And that gives an excuse for procrastinating office work too. Whenever somebody wants to take you to task, just claim that you’ve gone beta!

Sending mail with System.Web.Mail (.Net 1.1)

If you thought that it was a serious shortcoming that .Net framework 1.1 wasn’t able to send email through SMTP servers that used authentication, you are in grave ignorance. On the surface of it, yes, you cannot send an email with the standard System.Web.Mail.MailMessage class; but you can of course play tricks on the Exchange server (yeah, that’s what I get out of it that this things works only on an Exchange server) to make it authenticate you.

There is a namespace called http://schemas.microsoft.com/cdo/configuration that does those funny (and highly interesting) tricks. First we would check how to use a simple SMTP server (no authentication), and then we will use authentication to send a message.

//using System.Web;
//using System.Web.Mail;

//The simple SMTP Server
MailMessage message = new MailMessage();
message.From = “src@someone.com”;
message.To = “dest@someother.com”;
message.BodyFormat = MailFormat.Html;
message.Subject = “This is a spooky mail!”;
message.Body = “Heheh, just kidding!”;

SmtpMail.SmtpServer = “your.server.address”
SmtpMail.Send(message);

Now lets check out a server that uses authentication. Microsoft provides the CDOSYS object to achieve just that. You wouldn’t think they were so dumb that they didnt incorporate some lousy authentication mechanism! Add the following lines before the SmtpMail lines.


message.Fields[
"http://schemas.microsoft.com/cdo/configuration/smtpserver&#8221;

] = “your.server.address”;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/smtpserverport”%5D = 25;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/sendusing”%5D = 2;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”%5D = 1;

//Most of the times the @domain is necessary with the username
message.Fields[“http://schemas.microsoft.com/cdo/configuration/sendusername”%5D = “username@domain.com”;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/sendpassword”%5D = “urpassword”;

//If only the server uses SSL, in this case, the smtpserverport field would be different too
message.Fields[“http://schemas.microsoft.com/cdo/configuration/smtpusessl”%5D = true;

And that would do just fine. There you go, I just saved your time and money!

[P.S. To those whom these lines look like Greek, I must say that this post appears as a record!]