Download source
This is an introduction to Linq to XML showing how to read, insert, update and delete from an XML file.
Download source
This is an introduction to Linq to XML showing how to read, insert, update and delete from an XML file.
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!
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.
I was using the StringBuilder to create a JSON string which included an array and therefore curly braces, but I got an exception as curly braces are used to define the parameters for the format.
sb.AppendFormat("{\"Forename\":\"{0}\",\"Surname\":\"{1}\"}", person.Forename, person.Surname);
At first I tried adding a backslash which is used to escape the quotation mark but this didn’t work, so I tried the double character trick but adding two curly braces which seems to do the job.
sb.AppendFormat("{{\"Forename\":\"{0}\",\"Surname\":\"{1}\"}}", person.Forename, person.Surname);
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.
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.
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.
Automatic Properties were a nice addition to C# 3 and I use them all the time. A lot of people may already know this but somehow I only found out this week; you can create a read only automatic property simply by adding the ‘private’ keyword before ’set’:
public class UserEventArgs : EventArgs
{
public UserEventArgs(int userID)
{
UserID = userID;
}
public int UserID { get; private set; }
}
I feel like I haven’t posted in ages. Have been busy changing job and moving house but I have a list of things I want to blog about soon.
Recently I needed to convert a number of bytes to a readable string that represents the number of kilobytes, megabytes or gigabytes.
I created an extension method for Int32 which does this for me Int64 as per Alex’s comment about the FileInfo.Length property being a long. I also changed my method for Int32 to call the other method so that it can be used for both int and long.
Consider the following:

Using Linq I want to select all the roles for a particular user. I could create a query like this:
public List<Role> GetByUserID(int userID)
{
List<Role> roles = (from r in Context.Roles
join ur in Context.UserRoles on r.ID equals ur.RoleID
where ur.UserID.Equals(userID)
select r).ToList();
return roles;
}
This simply creates the following SQL:
SELECT [t0].[RoleID] AS [ID], [t0].[Name] FROM [dbo].[Role] AS [t0] INNER JOIN [dbo].[UserRole] AS [t1] ON [t0].[RoleID] = [t1].[RoleID] WHERE [t1].[UserID] = @p0
Alternatively I could use the Any extension method.
public List<Role> GetByUserID(int userID)
{
return Context.Roles.Where(r => r.UserRoles.Any<UserRole>(ur => ur.UserID.Equals(userID))).ToList();
}
The Any method returns a bool which indicates if any of the UserRoles meet the lambda expression; in this case checking the UserID property. The Where method evaluates the Roles against the Roles the User belongs to so we only get those Roles.
The resulting SQL looks like this:
SELECT [t0].[RoleID] AS [ID], [t0].[Name]
FROM [dbo].[Role] AS [t0]
WHERE EXISTS(
SELECT NULL AS [EMPTY]
FROM [dbo].[UserRole] AS [t1]
WHERE ([t1].[UserID] = @p0) AND ([t1].[RoleID] = [t0].[RoleID])
)