Wednesday, August 14, 2013

Overview of ScriptCS

What I found is that, ScriptCS (to be pronounced as Script-C-S) is a light weight open source development environment for using C# as scripting language.

ScriptCS removes the programmer’s dependency from Visual Studio. So one can write and execute the code without using Visual Studio at all. This means one can create a single ScriptCS file and execute it. There is no need to install .Net framework, IIS, etc... Also no cs-project is required to be created. It can be as simple as a single file (no additional DLLs). - This is cool.

So below are the components which enables programmers to write C# code without any project/Visual Studio:
1. Use Chocolatey (NuGet-based application/component installer) in powershell to install ScriptCS.
2. Install Nancy using NuGet Package manager. Nancy serves the web server role to run the apps built in ScriptCS.
3. ScriptCS uses "Roslyn" which is Microsoft's "Compiler as a Service" for compiling C# code. - This is really a revolutionary thought.

So these are all what is needed to run a C# application. There are some additional plug-ins available for syntax highlighting, debugging, etc. but these are non-mandate things.

My feeling is that it is like reinventing the wheel just to avoid Visual Studio. Although there are scenarios where tech evangelists are making use of ScriptCS and promoting it.

Tuesday, July 16, 2013

Generating data script for table using conditions in SQL Server

Problem Statement

Generate data script from table where records are in millions but we need data script for few records only.


Create a temporary table with the same structure as of the main table and insert data in the temporary table using the condition for filtering the records for which data script is required.
Or this can be done by:
select * into tmpMyTable from MyTable where <your condition(s)>

Now you can use the normal generate script method for this temp table and chose the option "Data Only" for getting the data script.
In the end drop the temp table.

Simple solution, works fine :)

Thursday, May 2, 2013

Simple checkbox list using Knockout JS


Often we need to display list of checkboxes in HTML and there is no native HTML control for checkbox list.
If you are from ASP.Net background then you will know that there exists an asp control for checkbox list. However here we are not going to discuss that.
Here we will see how to generate a customizable checkbox list using Knockout.JS.

Setting up Knockout JS

First step is to setup Knockout.JS framework in your web based application. In respect of time, for that purpose let me redirect you to KnockoutJS. Here you will get steps required to wire knockout with your web application.

Knockout ViewModel

Let us say, you are having a list of fruits (Apple, Mango, Grapes, etc.) which you want to display in a checkbox list. Then you should have an KO observable array for that. You can achieve it simply by following similar code in you JS file:
Assumption : You are having your ViewModelReady with data coming from server.

var self = this;
self.fruitsList = ko.observable([]);
self.selectedFruits = ko.observable([]);

if (data.fruitsList != null) {
   self.fruitsList ($.map(data.fruitsList, function (item) { 
         return new SelectedListItemViewModel(item) }));

Separate helper function
Assumption : Object coming from server side is having "Value", "Text" and "IsSelected" properties.

function SelectedListItemViewModel(data) {
    this.Key = ko.observable(data.Value);
    this.Name = ko.observable(data.Text);
    this.IsSelected = ko.observable(data.IsSelected);

HTML code

In your HTML supported page you can keep the following HTML code to render the checkbox list:

We created a container
to hold all the checkboxes with some specified width and height so that it should get a look of list of controls.
For each item in the fruitList, one checkbox will get created and its value property will get binded with item's Key value.
To display text, we added a span and binded its text property with item's Name value.
Depending upon your object's properties, these two will vary from my example.

The important part to be noted here is another KO observable array "selectedFruits". On checking the checkbox, this observable will get filled with one item and will hold the "value" property of the checkbox. Using this observable array we can find out that in UI how many and what all checkboxes are selected.
Also if this list "selectedFruits" is already having some values in it which are same as of the checkbox's value property then the checkbox will appear as checked. Isn't it good for us!!!

Rest all depends on you that how you want to post this data to server back again.
Happy coding.