Google Kills Google Wave, Why did it Jump the Gun?

Okay, let me say I was wrong. Google built up so much excitement around Wave that when I first looked at it, frankly I was disappointed. So I wrote on Wave, ‘What will I use it for?’… That was almost an year ago.

Now I think that it wasn’t because Google didn’t have a solid product, or they didn’t have a good idea, but because they were trying to wean a generation of email users away from something they always used. Then they built so much hype around it that people expected a miracle. Sadly that takes a while to deliver and nobody, including Google had patience.

This sort of thing should be allowed to grow on people, with more and more people using it and recommending it. All the growth should have been organic, but that wasn’t so and now Google has killed an excellent product. Frankly it hurts to see wave go.

After an year of rejecting wave I implemented it in my organisation some days ago and I was surprised at how brilliant it is for the purpose of collaboration. It does beat email hands down, and I was really upbeat about the productivity benefits it could bring me. Over the past few days I’ve been talking about wave to a lot of people, I am sure there are others..

All the people in my company are now using Google Wave for collaboration and monitoring projects, and now we feel let down by Google.

So while we know it’s probably not going to work I want to send Google a msg on behalf of all the people who love and use Wave everyday.

Don’t kill it Google, maintain it. It will grow to become an awesome product if you give it just some more time.

MySQL Throws NullReferenceException in Finalize… How I coped with this.

There are times when I believe that I am the only programmer in the world who leaves bugs in his code and it certainly is a humiliating feeling to feel like a sloppy coder, watching all the ultra-cool code go by working efficiently every inch of the way. So now and then when I see a bug in a professional application’s code it irritates me, but it also brings me a warm glow — I am not so alone after all. The latest victim in this ego-boosting series is MySql. Yes that excellent open source data project that is now owned by Oracle (so you know where it’s heading… NoSQL Anyone?).

I am migrating one of my websites to MySQL from SQL Server and ran into a bug in the MySQL .Net connector code.

MySQL .Net connector (at least till 6.23) gives a NullReferenceException if you have used the MySQLHelper classes to initialize a MySqlDataReader object when the Application tries to run the finalizer on the connection.

The error can occur at totally random places in your code because it happens in the finalizer. This kept me stumped for a little while because I was searching for the error in my own code. Finally I deduced from the stack trace that the error was in MySQL .Net Connector.

I did realize that this would be a bug, but I didn’t know exactly what part of my code caused this bug to surface. So I checked the stack once more and discovered that there was a call to MySQLDataReader earlier in the trace. So it was clear then that it was definitely something related to the MySQLDataReader.

At this point I did a search on Google and found the details of this bug report. It sounded like this one alright, and it hadn’t been fixed yet. I wasn’t completely sure how I could fix this or why it was occurring so I posted a question on Stackoverflow to see if anyone else had faced the same. The insightful answer from Evegyn which reminded me that if I had used ‘using’ then the finalizer wouldn’t be called on the connection object made it clear that it had to be the MySQLHelper object which was responsible because I am very particular about releasing any resources I have grabbed and I never miss a ‘using’ statement.

MySQLHelper in the MySQL .Net connector takes the responsibility of releasing the connection so I wasn’t using `using` :) .

Acting on the hunch I removed the reference to MySQLHelper wherever I was using the MySqlDataReader and pronto, things started working again.

So don’t use MySqlHelper to spawn MySqlDataReader if you are working on .Net on a 64 bit Windows machine for version 6.22, 6.23 (might be fixed in later versions).

Why I Love the MySqlHelper Class in MySql .Net Connector

Which code do you like better?

1:

       SqlConnection cnn = new SqlConnection(Globals.CONNSTRINGJYOTISH);
        cnn.Open();

        using (cnn)
        {
            string qry = "UPDATE [Order] SET PaymentDone=1 WHERE OrderCode=@ordcode";
            SqlCommand cmd = new SqlCommand(qry, cnn);
            cmd.Parameters.AddWithValue("ordCode", orderCode);
            int res = cmd.ExecuteNonQuery();
            if (res > 0) return true; else return false;
        }

Or

2:

            return MySqlHelper.ExecuteNonQuery(Globals.CONNSTRINGJYOTISH, "UPDATE `order` SET PaymentDone=1 WHERE orderCode=?ordCode",
                new MySqlParameter[] { new MySqlParameter("ordCode", orderCode) }) > 0;

The second version does exactly the same job as the first version. I admit I got a little carried away with terseness in directly comparing the result of the ExecuteNonQuery to an integer, but skip that and leave the rest of the code. Not only is it compact, it is also easier to read and quicker to write.

The MySQLHelper class is an awesome piece of work and I haven’t been using the traditional command execution scheme ever since I first used it. If you are using MySQL with .Net, then check it out.

How to Submit JSON Data to an ASP.Net MVC Action, and then Deserialize it

I just had some negative fun (negative fun is something that you enjoy in a painful kind of a way) trying to submit some simple JSON data to my ASP.Net MVC action method without having to create a Custom Model Binder for it.

Basically all I wanted was a simple Dictionary object, and since I’ve been serializing Dictionary to JSON so easily all this while using JSONResult I just wanted to do the reverse in a single, easy to use step. Unfortunately it didn’t turn out to be so simple and it was hard finding the right resource because most of the blogs online are trying to convince you very hard to use a Custom Model Binder (which I am adamant I won’t use this time).

I needed to submit the data from multiple forms (collected using Ajax) bunched together in one huge JSON object. A custom model binder would be very hard to program because there are so many objects with insane hierarchy while a Dictionary is god-send for something like this.

Here’s the code.

Javascript side:

        function processPay(payData) {
            var encod = JSON.stringify({ birthD: birthData, language: langSel, paymethod: payMethod, paySettings: payData });

            $.post("/horoscope/horoscopeajax/", { encod: encod },
                function (res) {
                    return res;
                });
        }

I am using JSON.stringify function which is a part of the JSON in Javascript library by Dave Ward. This function converts a JSON object into string.

At the server:

        [HttpPost]
        public ActionResult HoroscopeAjax(string encod)
        {
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();

            Dictionary<string, object> ts = (Dictionary<string, object>)js.DeserializeObject(encod);
            return null;
        }

encod is a simple string which the .Net’s inbuilt javascript serializer can deserialie(No need of JSON.Net). That’s it, a single line of code just like I wanted.

How to backup your server data, (MySql, SQL Server, Directories) with Powershell

How to backup your server data, (MySql, SQL Server, Directories) with Powershell

Not a very long time ago I lost some data on my Windows server. It was a totally unexpected failure of the hard disk. Back then I had a local backup system which failed along with the hard-disk failure. So later when I got a second server to host some of my websites I decided to create a backup system that would take a total backup of each server on the second server.

That included MySQL databases, SQL Server databases and some folders. There were some custom solutions available for taking MySQL and SQL Server backups, but nothing that would nicely package all my files into one unit, so I decided to roll my own using Powershell, that awesome new toy for Windows Scripting. It was a chance to learn and work with something new, so how could I let it pass me by?

So dear reader, for all three of you (hi dad!) who actually read my blog, here’s TOTAL SERVER BACKUP through Powershell.

1. Taking MySQL Backup through Powershell

That should have been trivial, after all, all I need to do is script the commandline executable mysqldump which is already present in the bin folder of the Mysql installation on the server. How hard could that have been?

It turns out that was the hardest part of the job. Some of my websites are in Hindi (using Unicode), while mysqldump worked just fine from the command line, it just didn’t encode my content right from within Powershell. I tried every encoding method I could fine, and almost all the options (Unicode, UTF8), but I was still getting gibberish.

I gave up at one stage and wrote a backup script in C# .net. It worked just fine with some encoding settings and that told me it was possible. So I went back to Powershell and dug a little more. Thank you Rene Saarsoo for this gem of wisdom which told me that the correct encoding setting wasn’t UTF8, or Unicode which I had been battling with, but ‘oem’ (whatever that is supposed to mean).

So here’s finally my correct Mysql backup function that works.

function Backup-MySqlDB([string]$dbname)
{
"Backing up mysql " + $dbName + "..."
#Backup all mysql DBs

#Set-Location D:\MySQL\bin
Set-Location 'C:\Program Files\Parallels\Plesk\Databases\MySQL\bin'
$bkfile = 'C:\autobackup\Databases\' + $dbname + '.sql'

cmd /c mysqldump -u username -ppassword $dbname | Out-File $bkfile -encoding oem

Write-Host $dbName + "backup complete"
}

Notice that this is a function which accepts the database name as the argument. I have set up a new admin level user with powers over all the databases for the backup purpose.

Calling this function is as easy as this:

Backup-MySqlDB('blogvani')

2. SQL Server Backup Through Powershell
Some of the content on my server is on legacy (yeah! I don’t use SQL Server anymore) SQL Server database. I’ve got the SQL Server Express on my servers which is free, but not powerful and I really can’t automate backups. But Powershell can tap into .Net, use .Net DLLs and the full .Net Framework, so you can do practically anything with Powershell, including writing your own .Net library and consuming that within Powershell. That should teach those BASH script kiddies a volume or two!

Back to business. So basically because you can tap into .Net from Powershell it isn’t very hard to use the SQL Server backup objects exposed in .Net. Here’s the code which I collected and modified from the Internet.

function Backup-SqlDB([string]$db)
{
	"Backing Up Sql Server Databases..."
    #Backup SQL Server DBs
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

    $backupDir = 'c:\autobackup\databases\'
    $server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") '.\SQLEXPRESS'
    Write-Host $server
    $smoBackup = New-Object("Microsoft.SqlServer.Management.Smo.Backup")

    Write-Host $db

    $smoBackup.Action = "Database"
    $smoBackup.BackupSetDescription = "Full backup of " + $db
    $smoBackup.BackupSetName = $db + " Backup"
    $smoBackup.Database = $db
    $smoBackup.MediaDescription = "Disk"

    Write-Host ($backupDir + $db)

    $smoBackup.Devices.AddDevice(($backupDir + $db) + ".bak", "File")

    $smoBackup.SqlBackup($server) #Make the backup

    Write-Host $db "Sql Server Backup Finished"
}

Like the Mysql backup function this function too takes the name of the database as an argument and backs the db up in a predefined location which I have hard-coded because I don’t want to change it.

3. Zip the DBs up
Even in the backed up state the DBs are pretty large, not to mention they are all different files. So if I want to upload all the DBs to the second server that’s a problem. So I decided to zip everything up in a single file. There could be other solutions, but the one I decided to implement was using that cute command line zip utility Infozip which I have been using since I was a teenager. All I needed to do was set up a command line call. Another little function for you:

function Zip-DBs()
{
"Zipping the files..."
    #Zip the backup files
    Set-Location C:\autobackup\databases
    ./zip backup.zip *.sql *.bak | out-null

    remove-Item *.bak
    remove-Item *.sql
	"Files zipped."
}

Note that I called ‘Set-Location’ commandlet to set the location of the Powershell prompt to the folder where I have zip.exe and the SQL server (.bak files) and Mysql (.sql files) backup. After zipping them up I deleted the actual backup files using the ‘remove-item’ commandlet.

4. Backup Directories Through Powershell
We just finished backing up MySQL and SQL Server DBs through powershell, how hard can backing up a directory be? Since I have a zip file already set up I just wanted to add those website folder to my zip file. Here’s how the code works:

   function BackupDir([string]$bkdir)
   {
	    Set-Location C:\autobackup\databases
		./zip -r backup.zip $bkdir

		"Directory zipped: " + $bkdir
   }

This one is a function too. We can just pass it the folder name, and it will zip it nicely up in the backup.zip file. Notice the ‘-r’ argument sent to zip.exe, that’s to make it recurse the folder and collect all the sub-directories too.

5. Upload it all
Wow! We just backed up all our DBs, and all our website folders in one zip file through Powershell. It’s resting nicely in a separate folder on our hard disk, but what good is that if we have another hard drive failure? We need to upload it our second server. At first I considered doing this from Powershell using the ftp program which is a part of Windows and then the .Net Webclient class. I even wrote some code

   function Ftp-Upload {
	$File = "c:\autobackup\databases"
	$ftp = "ftp://userlogin:userpass@blogsvani.com/httpdocs/Server1Backup.zip"

	"ftp url: $ftp"

	$webclient = New-Object System.Net.WebClient
	$uri = New-Object System.Uri($ftp)

	"Uploading $File..."

	$webclient.UploadFile($uri, $File)
   }

This is the webclient code, I can’t provide the ftp program code cause I deleted it, but it’s easy. Unfortunately that didn’t work for me for some reason and when I ran out of patience I decided to use that wonderful program Cobian Backup which automates backup and ftp upload. I set it up to copy my zip file to my second server using ftp everyday at a specific time.

6. Setting up Powershell to run daily
Did you miss the last piece of the puzzle? To have a completely automated backup we need to ensure that the Powershell script that we wrote so painstakingly runs daily at a certain time. We can do this using the Windows Task Scheduler. This wasn’t as easy as it was supposed to. To run the script correctly you need to run powershell, and pass it the name of the script you want to run along with some other arguments using the ‘Add Arguments’ field. Here are the arguments:

–Noninteractive –Noprofile -command "& 'c:\scripts\serverback.ps1'"

Notice that before I pass the script name I prepend ‘&’, and that I have the entire thing in double quotes. If you don’t have that your script won’t run. I found this online from another webpage helpfully provided by Kiwi Si.

Oh and yes, don’t forget to run the command – “set-executionpolicy RemoteSigned” on your Powershell command prompt once to set up the necessary permissions or the script execution won’t work.

So this finally concludes the automated backup system for our server through Powershell.

Now wasn’t that a piece of cake?

Easy CS S Trick – How to Create CSS Tabs – 2 Easy ways

Finally I have some breathing time away from the projects I was working on from January, so yesterday I fixed an issue on Codebix and in the night I worked on Blogvani. I wanted to improve the bad looking tabs in the side-bar, making the look like full-fledged tabs instead of white button-spaces.

The only problem in the exercise was to get he bottom line below the tabs to look right. The line should be gone from below the active tab, and should be visible below the inactive ones. Like in Blogvani

tabs

It wasn’t too hard, but I had to spin the CSS around a bit. I did this in a particular way (explained later), and later discovered that Google does it in another, excellent way. In this article I am going to explain how to do this in both the ways.

The individual tab items are li elements. Here’s the code

<ul>
 <li>
  <a id="buzzhot" href="" class="">हॉट</a>
 </li>
 <li>
  <a class="sel" id="buzzliked" href="">पसंद किये गये</a>
 </li>
  <li>
    <a id="buzzread" href="">पढ़े गये</a>
  </li>
  <li>
    <a id="buzzcomment" href="">टिप्पणी प्राप्त</a>
  </li>
 </ul>
 

The selected element has a ‘sel’ class which specifies the border, and changes the background color to white. The li elements have generous padding, and I’ve set a min-width of 50 so that no tab is too narrow. Here’s the css:

.tpbtns li a.sel
{
    background:none repeat scroll 0 0 #FFFFFF;
    border-color:#B8B8B8 #B8B8B8 -moz-use-text-color;
    border-style:solid solid none;
    border-width:1px 1px medium;
} 

.tpbtns li a
{
    display:block;
    font-weight:700;
    margin:5px 5px 0;
    min-width:50px;
    padding:4px;
}

The only riddle is how to get the background line to disappear for the selected tags. Like I said earlier there are 2 ways. Here’s the way I used.

Method 1: Making Tabs Using a Background Image for the bottom line.

Since the li elements are enclosed in a div, if I set a border on the div it will be over the li elements and the border will not disappear for the selected tab. So I used a simple 1px * 10 px image which was of the same color as the border.

Here’s the css

background:url("/img/gln.png") repeat-x scroll center bottom #D9E1E5;

It’s a simple image with repeat set to horizontal, and alignment to bottom. So the selected div will be able to cover the border (since it’s in background) and will give the impression of a proper tab.

One line story: Use an image set to background to create the border instead of using the css border property on the containing div.

Method 2: Set the border on the ul

Incidentally google had the same effect on the new Google font API and out of curiosity I checked how they did it. It turns out this was simpler than the method I employed.

All Google did was set the border property on the ul element itself on the enclosing div, then they padded the selected div a bit on the bottom, and set it’s bottom position to –1 after setting the position property to ‘relative’ in the CSS.

Here’s the css:

.g-tabs ul
{
    background:none repeat scroll 0 0 #EBEFF9;
    border-bottom:1px solid #6B90DA;
    display:block; list-style-type:none;
    margin:1.5em 0 2em;
    padding:8px 10px 0;
    white-space:nowrap;
}     

.g-tabs li.g-tab-selected a
{
    background:none repeat scroll 0 0 #FFFFFF !important;
    border-color:#6B90DA #6B90DA #FFFFFF;
    border-style:solid; border-width:1px 1px 2px;
    bottom:-1px; color:#000000 !important;
    cursor:default; font-weight:bold;
    padding:6px 14px 4px;
    position:relative;
    text-decoration:none;
}

The second method is slightly easier to implement and in hindsight I should have thought of that :) .

Websites are products in perpetual making

I’ve been developing packaged software for most of my working life, my transition to web was late but it seems permanent because I am enjoying web development like I’ve never enjoyed it before. Thanks in parts to ASP.Net MVC that finally allowed me to take web development seriously.

Coming from the packaged application world, one difference I am enjoying very much is that a website gives a developer much more flexibility than a packaged app does. If I find a bug I can fix it now and all my users are updated immediately. I can take a website online earlier and allow the initial users to beta test it for me, telling me what’s wrong so that I can fix it.

In packaged software once you ship you’re done. At least for a while. Testing is a tougher job, and shipping is even tougher with all those installation issues that crop up. Updating the app is also very difficult and you’ve got to make it opt-in. It’s also costlier. On the Internet users expect to upgrade and flow with the changes but not on the desktop.

As a developer I find the net a more liberating platform because it puts me in control. Of course there’s a heightened security risk to take care of but there’s no piracy!

I’ve found myself working, improving, fixing web applications that I made a year ago, and I can immediately bring the fix online. That way it seems that a website application project seems to have no end in the development cycle. Which is not only fun, but keeps people like me in demand. :)

Better CSS for everybody [SASS]

Today I ran into an innovation called SASS. This is a technology that puts CSS on steroids and takes it beyond the designers to programmers. SASS is CSS scripting to allow programmers to write CSS in a more elegant and organized manner. You’ve got goodies like variables, better hierarchy and even arguments.

Better CSS for everybody

Better CSS for everybody

Although CSS was a great idea when it was first developed you can’t get rid of the fact that it wasn’t meant for programmers. The syntax is too descriptive, cumbersome and a lot of power is missing. Take JQuery like advanced selectors for example, why can’t CSS have that as a part of the standard. That’s what the CSS standards team should be looking into instead of fancy rounded corners.

Face it, in today’s world the designer has to be programmer has to be a designer. With the advent of Javascript and Ajax the border between code and design is becoming even more blurry. As a programmer I want/demand more power out of my CSS and I think SASS is a great move forward in that direction.

Unfortunately it’s not for ASP.Net, it’s only for Linux so I won’t be running SASS on my server anytime soon.

I wonder how hard/easy it would be to implement something like SASS on a Windows server. Maybe someone could write an HTTPModule, or an ISAPI plugin that could make SASS work. It could be a translator, changing SASS like script to standard CSS. I guess that’s what SASS does on Linux because CSS is implemented by browsers on the front-end after all.

Meanwhile I am really in love with the concept of variables in CSS. That would give me the capability to define stuff like color properties at one place in the CSS file and use it all over the place. How cool is that? All I would need to change the theme of my site would be to replace that one property.

If you’re on Linux, go ahead and check SASS today.

Is Overdoing CSS good?

Whenever I look at a CSS file and see all those entries I get confused. Why have so many of them? In one CSS file we put entries that are related to so many different pages. Isn’t that an awful waste?

Yep, I do realize that CSS files are pretty small and I don’t really need to worry about the bandwidth implications most of the time… but yet.

css1

So some time ago I started the practice of putting only common CSS in the file, and all the CSS entries that would be unique to the page would be in the page itself. Maybe many people think that I am inviting trouble because there will be inconsistencies as if I make major changes in the main scheme they may not filter down to the pages, but my point is that if I make any breaking changes to the design, I will have to modify the individual CSS entries anyways.

So why not have them in the individual page anyway? That makes my job easier.

Well, this is the system that I am sticking to now. If there are problems, you’ll hear about them here.

Using SQL Views after a long time

SQL Server

SQL Server

When I first started with SQL Server I found Views and stored procedures a really cool affair and the book I read highly recommended it for everything. So I overused it a tad in the dynamic website project that I was making and later reaped the consequences which told me that views are to be hated for exactly the same reasons why they are recommended.

- They’re not maintainable (Every time I changed something in the code to fit a new database requirement, I would have to go to the server and modify the View SQL so that it would give me the correct data.) If you are on a remote machine like I was, using RDC to log into the remote machine to make changes to the database is very slow and time consuming. I would found myself waiting for the screens to refresh for a long time while I impatiently tapped the keyboard all set to type in that extra field.

So I decided that views were killing too much of my time and from now on it would be strictly SQL in my code. Later when I re-wrote the website I deleted every view and replaced it with SQL code. It really made the site easier to maintain and I didn’t find myself waiting for refreshes any longer.

The same goes for stored procedures too. I removed them all.

Now I am making a web service to process online customer data for our organization, a part of our order-process reorganization initiative. In this project we need to extract data from a virtual table that presents a collection of data drawn from several different tables (this table de-normalizes the data to make it easier to process). One possible strategy was to write the SQL code for the table in every function I am going to access it in, or use it as a global variable, but that would force me to execute two queries… Not a smart idea at all.

So views were very practical and a great solution here. Views are like a virtual table at the server end, and very easy to call. They are optimized and fast. So there’s no major performance overhead.

I guess this is ONE place where sql views are really-really helpful.