Monday 2 April 2018

how to validate date textbox using jquery format as dd MMM yy?

// condition null check
if (rowdata.EffectiveDate != null) {
           // validate format
            var date_regex = /^(([0-9])|([0-2][0-9])|([3][0-1]))\ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\ \d{2}$/;
            // if different format tha dd MMM yy it will return false
            if (!(date_regex.test(rowdata.EffectiveDate))) {
           
                alert('Please enter valid date!.');
                return;
            }
        }

Wednesday 21 February 2018

Disable cut copy paste options in textbox using angularjs

in Angularjs has predefined ng attribute, by using this we can achieve disabling Cut Copy Paste options in Textbox or text area.


<input type="text" class="form-control" ng-cut="$event.preventDefault()" ng-copy="$event.preventDefault()" ng-paste="$event.preventDefault()" ng-model="modelvalues" />

Thursday 18 June 2015

Difference between icollection vs ienumerable vs list

Difference among  IEnumerable<T> , IQueryable<T>, ICollection<T>,IList<T>, List<T>:

IENUMERABLE<T>:

Pros
1.       The most generic item of all.
2.       Still might use deferred execution.
3.       IEnumerable is best suitable for working with queries later on. It helps best when we try to add more queries with our previous one and we do not want to run our previous query in realtime but when we requested it or iterated through those or finally passing as a .ToList() object .
4.       IEnumerable does not run query until it is requested by iteration.

Cons

1.       IEnumerable doesn’t move between items, it is forward only collection as LinkedList. You can't get at "item 4" without passing items 0-3.
2.       Read-only list, you can't add to it or remove from it.
3.       Actually the model binder is not able to bind an enumerable because it is a too generic Interface, and it is not able to choose a concrete class to create that implements the IEnumerable.

IQUERYABLE<T>:

Pros
1.       Query isn't executed until to really iterate over the items, maybe by doing a .ToList()
2.       IQueryable best suits for remote data source, like a database or web service. IQueryable is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).

Cons

1.       avoid passing IQueryable to Views
2.       It is not a good place to handle errors...so better getting rid of delayed execution before passing to Views.

ICOLLECTION<T>:
Is between IEnumerable and IList. In addition, is the base of IList and IDictionary Objects.

Pros
1.       Considered the most basic type for collections.
2.       The System.Xml.Serialization.XmlSerializer class has specific requirements for types that implement ICollection and System.Collections.IEnumerable in order to be serializable.
3.      Inherited from IEnumerable<T>.
4.      Collection<T> , IList<T>, IDictionary all are inherited from this, so we have flexibility.
5.      Base of IList and IDictionary Object.
6.      Add, Delete and modify items in the collection is acceptable.
7.       Some collections that limit access to their elements, like the Queue class and the Stack class, directly implement the ICollection interface.
8.       Normally in EF table relationships we use this ICollection in virtual keyword.

Cons
1.       ICollection doesn’t have indexing like IList does.

ILIST<T>:

Pros
1.       Random access to the full list(like an ArrayList)
2.       Entirely in memory
3.      Best for model binding.
4.      Supports adding and removing but for only variable-size one.
5.       An IList implementation is a collection of values that can be sorted and whose members can be accessed by index, like the ArrayList class.
6.       In addition, IList implementations fall into three categories:
a)       Read-only: (A read-only IList cannot be modified)
b)      Fixed-size: (A fixed-size IList does not allow the addition or removal of elements, but it allows the modification of existing elements.)
c)       Variable-size: (A variable-size IList allows the addition, removal and modification of elements.)


Cons

1.       Memory Consuming.

LIST<T>:
Propertise
1.      Implements : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
Pros

1.      The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface using an array whose size is dynamically increased as required.
2.      In deciding whether to use the List<T> or ArrayList class, both of which have similar functionality, remember that the List<T> class performs better in most cases and is type safe.
3.       Methods such as BinarySearch and Sort use an ordering comparer for the list elements. The default comparer for type T is determined as follows. If type T implements the IComparable<T> generic interface, then the default comparer is the CompareTo(T) method of that interface; otherwise, if type T implements the nongeneric IComparable interface, then the default comparer is the CompareTo(Object) method of that interface. If type T implements neither interface, then there is no default comparer, and a comparer or comparison delegate must be provided explicitly.

Cons

1.      List<T> accepts null as a valid value for reference types and allows duplicate elements.
2.       The List<T> is not guaranteed to be sorted. You must sort the List<T> before performing operations (such as BinarySearch) that require the List<T> to be sorted.


In Simple words ;)

IEnumerable: Provides Enumerator for accessing collection
  • Used where you want to store a collection of objects which will be accessed only for read-only purpose.
  • You need to iterate through the collection, means to access an element at position 5 you first need to access 0-4 objects.
  • Cannot modify the list i.e. add, remove object operations not allowed.
ICollection:
  • List can be modified and iterated i.e. read, add, delete, edit operation allowed.
  • Operations like Sort are not allowed
IQueryable:
  • a special type because it allows deferred query execution i.e. if you define any query over IQueryable collection then it won’t execute till the timeGetEnumerator() is called.
  • It is particularly used for Linq queries and in Entity Framework.
  • All other collection types brings data from the database to the client side and then apply filter or do operation on that data. However, IQueryable filters data at database level i.e. filtered data is received from database.
  • Specific example, In Entity Framework based application, you use IQueryable collection for seed data and if you call collection.SaveChanges() then database update command is not sent directly in fact when you will first try to access the data (or first time you call GetEnumerator on DBContext) then database update commands will be sent by entity framework. Good example of deferred query execution.
IList:
  • Used where you need to iterate (read), modify and sort, order a collection
  • Random element access allowed i.e. you can directly access an element at index 5 instead of first iterating through 0-4 elements.

Wednesday 10 June 2015

How to get all options of a select using jQuery?

here is an example code!
$("#ID option").each(function () {
                $(this).prop('selected', true);
            });

ID=> tour item id.

Monday 8 June 2015

first and single in linq difference


refer this below table.

Single()SingleOrDefault()First()FirstOrDefault()
DescriptionReturns a single, specific element of a sequenceReturns a single, specific element of a sequence, or a default value if that element is not foundReturns the first element of a sequenceReturns the first element of a sequence, or a default value if no element is found
Exception thrown whenThere are 0 or more than 1 elements in the resultThere is more than one element in the resultThere are no elements in the resultOnly if the source is null (they all do this)
When to useIf exactly 1 element is expected; not 0 or more than 1When 0 or 1 elements are expectedWhen more than 1 element is expected and you want only the firstWhen more than 1 element is expected and you want only the first. Also it is ok for the result to be empty
for more details,

Monday 1 June 2015

difference between viewbag and viewdata and tempdata in mvc3

the main difference below,
ViewData:
  • ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys
  • It’s required type casting for complex data type and check for null values to avoid error.

ViewBag: 
  • ViewBag doesn’t require typecasting for complex data type.
  • ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0. 

Check the following example:
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var emp = new Employee
        {
            EmpID=101,
            Name = "Deepak",
            Salary = 35000,
            Address = "Delhi"
        };

        ViewData["emp"] = emp;
        ViewBag.Employee = emp;

        return View(); 
    }
}
And the code for View is as follows:
@model MyProject.Models.EmpModel;
@{ 
 Layout = "~/Views/Shared/_Layout.cshtml"; 
 ViewBag.Title = "Welcome to Home Page";
 var viewDataEmployee = ViewData["emp"] as Employee; //need type casting
}
<h2>Welcome to Home Page</h2>
This Year Best Employee is!
<h4>@ViewBag.emp.Name</h4>
<h3>@ViewData.Employee.Name</h3>