Skip to main content

Posts

Showing posts from January, 2013

C#: How to bind an Enum to a ListBox/DropDownList/ Checkboxlist control in ASP.NET?

There have been requirements where we need to bind ListControl from enum (with enum Description as Text and enum value as Value).
For example I have an enum of Countries as follows:
publicenumTestCountries {     [Description("India")] India = 1     ,     [Description("United State Of America")] UnitedStateOfAmerica = 2     ,     [Description("United Kingdom")] UnitedKingdom = 3 }
And I want to bind this enum’s description and value to any list control.
Here I am writing a static method “LoadListForListControls<T>”, which will take Enum as T, and then return list of ListItem.
// We need to use following namespaces for this method: using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Web.UI.WebControls;
publicstaticList<ListItem> LoadListForListControls<T>() { var type = typeof(T); var list = newList<ListItem>(); foreach (var value inEnum.GetValues(type))     { var e…

Difference between document.ready and window.onload/load event

$(document).ready():- Jquery $(document).ready()  function called once the DOM (Document object model) is loaded on your page. DOM means the entire html tags/script (i.e. div, table, paragraph etc.). If your web page has large images, it will not wait for loading of images completely. This method is used when we want to initialize our jQuery codes after the DOM is ready.
$(document).ready(function(){ // method called as soon as DOM is ready });
window.onload()/load() method:- The load event fires when all the content on your page loaded including the DOM (document object model) and images. This method gets called when images and all associated resources including the DOM (document object model) of the page have been fully loaded. Suppose your web page has large size images then until all the images are not fully loaded on the page, this method will not called.
$(window).load(function () { //this method called after images are loaded  });
This method is called each and every partial postback …

SQL Server: What is the difference between DELETE and TRUNCATE command?

Following are the differences between DELETE and TRUNCATE:
TRUNCATE DELETE TRUNCATE is DDL Statement DELETE is a DML command TRUNCATE cannot have WHERE Conditions DELETE can have WHERE conditions TRUNCATE does not fire trigger DELETE Fires TRIGGER TRUNCATE reset the identity to 0 (if table have any) DELETE does not RESET Identity TRUNCATE Release the table’s spaces to System DELETE removes the records but will not release the space to the system TRUNCATE cannot be used against the table that is referenced by a FOREIGN KEY constraint. DELETE can be used in tables reference by a Foreign Key and tables involved in Indexed view TRUNCATE cannot be used against the table used in Indexed view DELETE can be used against the table used in Indexed view TRUNCATE apply Fewer table locks DELETE apply more locks to the table TRUNCATE cannot be used against the tables involved in TRUNCATE transactional replication or merge replication. DELETE can b

C#: How to get list of GUID from comma separated string (in single line of code)?

If you have Guid values in a string separated by comma, and want to convert all comma separated GUIDs to list of GUID then here is a single line code that will do the trick.
privateList<Guid> GetSelectedIdList(String selectedIds) { returnArray.ConvertAll(selectedIds.Split(','), delegate(string s) { returnnewGuid(s); }).ToList(); }
Or you can rewrite same method with the help of lambda expression as:
privateList<Guid> GetSelectedIdList(String selectedIds) { returnArray.ConvertAll(selectedIds.Split(','), s => newGuid(s)).ToList(); }
Just pass the comma separated string and “GetSelectedIdList” method will return list of GUID.

SQL Server: Coding Standards and best practices

Here I am writing some of coding standard and best practices one should follow while working with database. You can also check for Query Optimization and SQL Tunning
·Use Pascal notation for SQL server Objects like Tables, Views, Stored Procedures. Also tables and views should have ending “s”. For example: UserDetails etc. Check naming convetion table given below for more details. ·If “One Table” references “Another Table” than the column name used in reference should use the following rule: Column of another Table: <FirstTableName> Id ·If you have big subset of table group than it makes sense to give prefix for this table group. Prefix should be separated by _. For example: meeting_Users, meeting_Attendee etc.
SQL Object      Pattern      Example      Stored Procedure      sp<Application Name>_[<group name   >_]<action type><table name > Where action is: Get, Delete, Update, Write, Archive, Insert… i.e. verb
      Note: Do not prefix stored procedure names …

Coding Standards and Best Practices for JavaScript/JQuery

jQuery is an extremely powerful library that provides all the tools necessary to create beautiful interactions and animations in web pages, while empowering the developer to do so in an accessible and degradable manner. Here I'll write some of best practices one should use while working with client side scripting.

·If you are registering script files through code behind, its good practice  to register all javascript/jquery files on page_init, and if you need to register on the fly scripts from code behind then register those scripts in PreRenderComplete event.

·Use the jQuery shorthand $ where possible:

$(function() { ... });
instead of
$(document).ready(function() { ... });
·Use a document ready event in place of window onload. The handler for the document ready event must be an anonymous declared in the following form:

   $(document).ready(function(){
        //all initialization    });    Instead of:    $().ready(handler);
·If you want to use jQuery’s $ variable, wrap your code like t…