Friday, 29 July 2011

Generating xml using linq query

The sample code for generating xml using linq query as follows
List<Contact> contacts = Contact.SampleData();
List<CallLog> callLog = CallLog.SampleData();
var q = from call in callLog
where call.Incoming == true
group call by call.Number into g
join contact in contacts on
g.Key equals contact.Phone
orderby contact.LastName, contact.FirstName
select new XElement(”contact”,
new XElement(”lastName”,
contact.LastName),
new XElement(”firstName”,
contact.FirstName),
new XElement(”count”,
g.Count()),
new XElement(”totalDuration”,
g.Sum(c => c.Duration)),
new XElement(”averageDuration”,
g.Average(c => c.Duration))
);
// create the XML document and add the items in query q
XDocument doc = new XDocument(
new XComment(”Summarized Incoming Call Stats”),
new XElement(”contacts”, q)
);
Console.WriteLine(doc.ToString());

output for the above query is as follows
<!—Summarized Incoming Call Stats—> 
<contacts>
<contact> 
<lastName>Gottshall</lastName> 
<firstName>Barney</firstName> 
<count>4</count> 
<totalDuration>31</totalDuration> 
<averageDuration>7.75</averageDuration>
</contact> 
... (records cut for brevity) 
<contact>
<lastName>Valdes</lastName> 
<firstName>Armando</firstName> 
<count>2</count> 
<totalDuration>20</totalDuration> 
<averageDuration>10</averageDuration>
</contact> 
</contacts>

Thursday, 28 July 2011

Grouping list of objects

There are two methods for grouping list of  objects in c sharp. one is using  Dictionary and other one is using linq to objects

Using StoredDictionary
// group by state (using a sorted dictionary)
SortedDictionary<string, List<Contact>> groups =
new SortedDictionary<string, List<Contact>>();
foreach (Contact c in contacts)
{
if (groups.ContainsKey(c.State))
{
groups[c.State].Add(c);
}
else
{
List<Contact> list = new List<Contact>();
list.Add(c);
groups.Add(c.State, list);
}
}
// write out the results
foreach (KeyValuePair<string, List<Contact>>
group in groups)
{
Console.WriteLine(”State: “ + group.Key);
foreach (Contact c in group.Value)
Console.WriteLine(” {0} {1}”,
c.FirstName, c.LastName);
}

Using Linq to objects:

Linq to objects works from 3.0 .net frame work

var query = from c in contacts group c by c.State;
// write out the results
foreach (var group in query)
{
Console.WriteLine(”State: “ + group.Key);
foreach (Contact c in group)
Console.WriteLine(” {0} {1}”,
c.FirstName, c.LastName);
}

Sorting a list of object in c sharp

There are three methods to sort list of  objects in c sharp. one is using Deletages, second Implementing IComparer interface, and other one is using linq to objects

Implementing IComparer(C#1.0)
class ContactNameComparer : IComparer
{
public int Compare(Contact x, Contact y)
{
return x.Name.CompareTo(y.Name);
}
}
...
List<Contact> contacts=Contact.SampleData();
contacts.Sort(new ContactNameComparer());
foreach (Contact contact in contacts)
{
Console.WriteLine(contact);
}


Using  inline delegates(from C#2.0)

List<Contact> contacts = Contact.SampleData();
// sort by last name
contacts.Sort(
delegate(Contact c1, Contact c2)
{
if (c1 != null && c2 != null)
return string.Compare(
c1.LastName, c2.LastName);
return 0;
}
);

Using Linq to Objects(From C#3.0)

List<Contact> contacts = Contact.SampleData();
// perform the LINQ query
var query = from c in contacts
orderby c.State, c.LastName;

Friday, 22 July 2011

Regular Expression for password strength validation

Shown below is the regular expression for password strength with n number of digits, upper case, special characters and at least 6 characters in length.
(?=^.{6,25}$)(?=(?:.*?\d){2})(?=.*[a-z])(?=(?:.*?[A-Z]){2})(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$
Explanation:
  • (?=^.{6,25}$) -- password length range from 6 to 25, the numbers are adjustable
  • (?=(?:.*?[!@#$%*()_+^&}{:;?.]){1}) -- at least 1 special characters (!@#$%*()_+^&}{:;?.}) , the number is adjustable
  • (?=(?:.*?\d){2}) -- at least 2 digits, the number is adjustable
  • (?=.*[a-z]) -- characters a-z
  • (?=(?:.*?[A-Z]){2}) -- at least 2 upper case characters, the number is adjustable

Wednesday, 13 July 2011

HTML 5 support for all non supported IE browsers or HTML5 in IE

Many of time you have seen that few HTML 5  tags are not supported by Internet Explorer. The solution for this is only <script> tag.

The <script> tag is very useful to define client side script but in this code I used it for only to make my Internet Explorer compatible to represent HTML5  tags.
To work with html5 tags in IE write the following code in <script> tag in header section.
<script type="text/javascript">  
document.createElement('nav');
document.createElement('menu');
.........................
.........................
</script>

Like the above what are the tags you want use just write single statement for each tag. or write as below
<!--[if IE]>
<script type="text/javascript">
var html5elements =
"menu,nav,output,progress,section,time,video,...........etc".split(',');
for (var i = 0; i < html5elements.length; i++)
document.createElement(html5elements[i]);
</script>
<![endif]-->

Monday, 6 June 2011

How to identify is image is loaded or not using jquery. If image notloaded loading the default image

When we are working with websites some times images may failed to load. To Identify is the image is loaded or not the follwing is the solution.
Solution 1
function IsImageLoaded() {
if($('#img_id').attr('complete')){
alert('Image is loaded!');
return true;
} else {
return false;
}
}
Solution 2
$("#photo").error(function() {
alert("Image failed to load");
});

Thursday, 5 May 2011

IIS7.5 deployment - duplicate 'system.web.extensions/scripting/scriptResourceHandler' section

On attempting to deploy a .net 3.5 website on the default app pool in IIS7 having the framework section set to 4.0, I get the following error. There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined.

To resolve this issue

Solution 1:


You will need to cleanup the web.config that includes all the section Definitions that point to .net 3.5. The reason this fails is because these section definitions are already included in the root web.config in .NET 4.0 (see %windir%\microsoft.net\framework\v4.0.30319\config\machine.config) that include all the system.web.extensions declared already.

Solution 2:

Another quick fix is to have the application pool set to 2.0 just as your development machine appears to have,.