Posted by Joe | Posted in C# | Posted on 25-06-2010
Here’s a quick an easy way to show the time difference between two DateTime objects using C#.
The DateTime structure has an overridden subtract operator which return a TimeSpan object when subtracting two DateTimes:
public static TimeSpan operator -(DateTime d1, DateTime d2);
You can then use this TimeSpan to get the amount of time between each DateTime:
DateTime fromDate = DateTime.Now.AddDays(-1).AddMinutes(-10).AddSeconds(-20);
DateTime toDate = DateTime.Now;
TimeSpan timeSpan = toDate - fromDate;
string timeString =
string.Format(
"{0:00}:{1:00}:{2:00}",
(timeSpan.Days * 24) + timeSpan.Hours,
timeSpan.Minutes,
timeSpan.Seconds
);
In the above example I’m comparing a date in the past to the current date. This could be useful for working out the total amount of time that has passed since something has happened.
I’m then using the Hours, Minutes and Seconds properties of the TimeSpan to format a string displaying the amount of time. If the hours goes over 24 then the Days property is used, so for my hours I’m also adding on the number of days multiplied by 24.

Posted by Joe | Posted in C#, Linq | Posted on 10-06-2010
Posted by Joe | Posted in ASP.NET, C# | Posted on 16-04-2010
Download source
When creating web applications with ASP.NET developers will usually create User Controls or Custom Server Controls. User Controls have the ASCX extension and allow developers to group together ASP.NET controls and functionality into a reusable control. Custom Controls are a bit more complex and require the developer to define the html that the control generates as well as all it’s functionality. It is also possible to add child ASP.NET controls to a Server Control and have the control render those child control and maintain their viewstate with little work.
In .NET 2.0 the abstract CompositeControl class was introduced, which is derived from WebControl but also uses the interfaces INamingContainer, which makes sure all child controls have a unique ID, and ICompositeControlDesignerAccessor which is used to allow the control to render the control automatically at design time based on the child controls. The CompositeControl also ensures that child controls are always created which saves using the EnsureChildControls method that many Custom Control developers will be familiar with.
In this post I’ll explain how to create a simple Composite Custom Control and how to customise the HTML that it renders.
Read the rest of this entry »

Posted by Joe | Posted in ASP.NET, C#, Linq, XML | Posted on 08-01-2010
Download source
This is an introduction to Linq to XML showing how to read, insert, update and delete from an XML file.
Read the rest of this entry »

Download source
Making ajax calls to an ASP.NET web service using jQuery is too easy. In this post I’ll explain how to do it!
Read the rest of this entry »

Posted by Joe | Posted in ASP.NET, C#, JavaScript | Posted on 29-12-2009
Download source code
Previously I’d done JSON serialization using the JavaScriptSerializer which is part of AJAX Extensions 1.0, but this is now obsolete.
.NET 3.5 introduced the DataContractJsonSerializer class. The class sits in the System.Runtime.Serialization.Json namespace which is curiously hidden away in the System.ServiceModel.Web assembly.
The DataContractJsonSerializer can serialize a class that contains the Serializable attribute or any class defined as a DataContract.
Read the rest of this entry »

Posted by Joe | Posted in C#, JavaScript | Posted on 29-12-2009
Posted by Joe | Posted in ASP.NET, C#, Linq | Posted on 28-12-2009
Download source code
In a previous post; Base Repository/Business Logic wrapper, I talked about a basic Linq to SQL Framework I created. I then extended it in my ObjectDataSource binding with paging and sorting post to show how to use it with the ObjectDataSource.
The problem was that although it worked quite nicely for getting all records and getting by ID, if I wanted to perform any specific filtering I had to create a derived repository class with a specific method for each query.
In this post I’ll explain how I have extended this framework using reflection to allow filtering on multiple columns without having to created separate repository objects.
Read the rest of this entry »

Posted by Joe | Posted in ASP.NET, C#, Linq | Posted on 23-11-2009
Download source
With Linq to SQL lazy loading is used by default. That means that if one object contains another object, the child object will only be loaded when first accessing it. Using Load Options it is possible to tell Linq to SQL to also load the child object at the same time as loading it’s parent.
Read the rest of this entry »

Posted by Joe | Posted in ASP.NET, C# | Posted on 20-11-2009
I have FormView and on changing to Edit mode I wanted to set focus to the first textbox. I thought I’d do this hooking into the ModeChanged event but this doesn’t work as the textbox hasn’t been rendered.
I then tried overriding the Render method and setting the focus after rendering; at this point the control was created but I get an exception to say focus can only be set in PreRender, but in PreRender the control won’t be available.
One way I found to get this to work was to set the focus in a handler for the DataBound event of the FormView.
Read the rest of this entry »
