Jul 15, 2014

Our great nation

I finally published a post I'd written long back.. I'd completely forgotten about it.. Lot's of exciting stuff happening in my life and hence.. once again.. this hiatus from my blog...

Saw a funny debate on times now tonight about juvenile offenders.. The panel tends to get very loud and defensive focussing on what the other panelist said loudly rather than the issue.. It's like one of those college group discussions gone wrong or MTV roadies GD (based on what i've seen on television) where it turns into a bad debate where you must belittle the other's ideas rather than make realistic sense..

Why must we reinvent the wheel? The defending spokesperson made an age old proven point (via many documentaries and bollywood/hollywood movies) that undeserved hard penalisation cultivates hardened criminals.. The age old vicious circle you can call it.. The other panelists made the point that just a number (Age) can't be the only deciding factor.. They put it as "the heinous nature of the crime and the awareness and understanding of the juvenile must be taken into account"...

I just don't get how this is even a debate.. Neither of the sides are saying something ridiculous but that show absolutely was ridiculous..

Why not resort to degrees? A first degree crime if proven will have a particular set of laws regardless of age.. Whereas a second degree crime won't attract the same penalty.. It's an old concept..

A court judgement doesn't reach one citizen at a time.. it reaches potentially more than a billion people in our great nation..

A person, able of committing an intentional crime knowing it's consequences and still doing it willingly is "probably" more likely to be a repeat offender no matter what the age and what the punishment.. I don't understand how 17 yrs, 11 months, and 29 days is different from 18 years when it comes to such a case..

Everybody knows what the problems are in our nation today.. Experts don't need to highlight that sensationally.. Any one of us can do that.. We look up to you for realistic and fair solutions.. As a leader or manager i would never hire a person who highlights problems 24/7 and albeit is very active and passionate about the problems, he/she is unknowingly just adding to the problem.. I'll go for a person who offers a solution (it may be an effective solution or may be a disaster) and doesn't just whine about the state of affairs. This very approach needs to become our culture.. Our culture shouldn't be based on history or a religion.. A culture should be what it means.. What all of us partake in today.. How we make decisions and how we govern our present day actions.. It should be an optimistic and problem solving culture rather than an indifferent or complaining culture

If you have views (which i'm sure you do) on the matter, please write away in the comments :)

Where did we go wrong

It's been so long since I bothered this blog with my ramblings. It's been busy times followed by empty times but the mind was too distracted and busy for any form of catharsis. My brother posted something about writing an app to prevent blogging when inebriated about a week ago but where will we go if we stifle this too.
Growing up fast is a disease. An incurable one at that. My mom called me the oldest sixteen year old in the world. Never felt anything back then but I guess it's something that sticks with you. I feel like the oldest twenty five year old today. I love and respect all that's gone into giving me the liberty and luxury today in my life, but I can't help but wonder, who decided this is how it should be?
There's always the next milestone and the next assignment waiting for us. Going with the flow saves us from the momentary worries but comes back to haunt twice as bad in my opinion. My life took me from wanting to be an author to a painter to a pilot to a musician to a doctor to a shippy. And here I am listening to people say I was born to be this IT geek that I am. I love my job and this life, but at some point I would want it to be about what I think is necessary and what I think is important. It should not be about the next deliverable or the next promotion or the next assignment no matter what field you work in.
You can keep planning and delivering all life long. When you realize that you can't live up to the promises you made to your loved one though, or wake up more than thrice a week wondering what you're doing.. It's on you.. only you 

Aug 23, 2011

Passing jQuery MultiSelect values to an SQL Server Parameter

The Problem

There are various jQuery plugins out there that allow you to convert a simple dropdown list into a multi-select one. They all return either an array of the selected choices, or a comma-separated string like 'opt1,opt3'. We generally take this value and put it into a hidden field we can access on the server side and try and pass it to our store procedure or query as a parameter. All goes well till you find that the query/procedure doesn't give you the expected results anymore if it has the keyword "IN", and you're trying to filter your results based on your dropdown's selection.
When you pass a string 'Miami,Berlin,London' as a parameter, say @Cities, to a query like

SELECT Population FROM Cities WHERE Name IN @Cities

It will only produce the correct results if you select only one checkbox from the multiselect. The reason is SQL server needs a list to come after the “IN” keyword, and 'Miami,Berlin,London' is not a list, but a single string literal. The query will try and look for a single record where the city name is 'Miami,Berlin,London' as it currently looks like this:

SELECT Population FROM Cities WHERE Name IN ('Miami,Berlin,London')

What we need to send the query is this:

SELECT Population FROM Cities WHERE Name IN ('Miami','Berlin','London')

The Solution

We can create a simple table valued function that takes our comma-separated string and returns a list that the “IN” keyword can work with. Here’s a sample:

CREATE FUNCTION [dbo].[SplitListWithDelimiter](
@InputString VARCHAR(8000) 
, @Delimiter VARCHAR(2) = ',' 
) RETURNS @List TABLE (Item VARCHAR(1000))
WHILE CHARINDEX(@Delimiter ,@InputString ,0) <> 0
@Item=RTRIM(LTRIM(SUBSTRING(@InputString ,1,
CHARINDEX(@Delimiter ,@InputString ,0)-1))),
@InputString =RTRIM(LTRIM(SUBSTRING(@InputString ,
CHARINDEX(@Delimiter,@InputString ,0)+
LEN(@Delimiter ),LEN(@InputString ))))
IF LEN(@Item) > 0
IF LEN(@InputString ) > 0
INSERT INTO @List SELECT @InputString 

Now in our procedure or query, all we need to do is call this function and pass it our string:

SELECT Population FROM Cities WHERE Name IN
(SELECT Item FROM dbo.SplitListWithDelimiter(@Cities,','))

This will give us a result like this:

Jul 26, 2011

Creating filter links for an ASP.Net MVC 3 List View

I’ve started dabbling with ASP.NET MVC 3 recently and have been following Jon Galloway’s “ASP.NET MVC Music Store Tutorial”. It’s a great place to start if you have little or no experience with ASP.NET MVC (like me).
The tutorial shows you how to create a music store application and has an administrative section that lets us add albums and classify them by genre. The code samples come with a substantial amount of seed data as well so I was a littler overwhelmed when I first created the landing page of the administration and saw a sprawling table and a huge vertical scroll.

I wanted to be able to filter down the list based on Genre because that’s what I base my collection of music on. So as my first experiment with MVC, I decided to put filter links on top and I may even put in paging and search features later and write posts about it to keep you updated Dear Reader. As of now, this is what is was aiming for:

To give you a little background details on the current implementation, I have an Album, Genre, and Artist class in my Model. The Genre class has a GenreID which I plan to use in my URL to filter the results that are displayed.
E.g.: If Classical has GenreID 1, the URL will look something like: /StoreManager/Index/1 and initially when no filter is applied, it will look like: /StoreManager
MVC follows a lot of “Convention over Configuration” so when you create an app with default route settings in Global.asax, the controller takes Index as the default action. My Index action presently looks like:

Now to filter this list based on GenreID, I’m going to do the following things:
  • Send a list of Genre to the View (To display the filter links)
  • Add Filter links to the View
  • Pass a parameter ID to the Index method which I’ll keep as nullable
  • Filter the list based on supplied ID (if present)
There are multiple ways you can send data from the controller to the view, I’m gonna using the ViewBag.
Then in my view index.cshtml (I’m using the razor view engine), I loop through the list to display my filter links:

I’ve used the ActionLink Html helper to display the links which show the Genre Name as the link text and redirect the user to the Index view with GenreID being passed to the Action as a parameter named ID. This would render a link like
Now in my Index method, I add a nullable integer parameter and filter the list based on the supplied value. My Index method now looks like:

Note the parameter declaration, the ? specifies that id is a nullable. As we may view the list without any filters, we must first check if id has a value and only then filter the list. Now when I browse to the URL StoreManager/Index/1, I see only classical albums and when I browse to /StoreManager I see all Genres.