Code Snippets

What would NHibernate ICriteria look like in .net 3.5?

If NHibernate decided to ditch compatibility with plain old .net 2.0 and focus on 3.5 how would the ICriteria interface change? Previously I was throwing around an idea of using a simple lambda expression to resolve the property name. Well, I couldn't help but build on this a little more. The following idea is not supposed to be LINQ, that would be far more complicated and LINQ is essentially its own interface, which is not the point. The point is, if ICriteria was written today in .net 3.5, what could it look like? How could it change? So just to recap,...

posted @ Sunday, August 17, 2008 3:12 PM | Feedback (0)

NHibernate Type Safety using Lambda Expressions

I can't remember if this has been around before, I do vaguely remember seeing something like it. However, I just wanted to apply a snippet of code I found on Paul's blog the other day to NHibernate. Of course we will definitely have type safety in queries when Linq-to-NHibernate is completed. But surely linq-to-nhibernate is not going to be the _only_ way of writing queries. Using the original code snippet 'as is' would look something like this: ICriteria c = session.CreateCriteria(typeof(Person));c.Add(Restrictions.Eq(Property.GetFor(() => new Person().FirstName), "John")); This is ok, but a little long winded, so I implemented another class called RestrictBy which can break...

posted @ Tuesday, July 22, 2008 7:38 AM | Feedback (3)

IDataErrorInfo for ASP.NET

As far as I'm aware ASP.NET doesn't support IDataErrorInfo, I've asked about this in many places, including Tech.Ed '07 with no success. The closest things I've seen in terms of Business Object level validation is from Enterprise Library validation block (which is attribute based and can render out with custom EntLib web controls) and another example in the Futures Dynamic Data Controls using Linq (I have no idea how this magic validation magically appears). It all just seems overly complicated, so, have a look at the IDataErrorInfo interface the methods are: string this [ string columnName ] { get; } string Error { get; } Simple and...

posted @ Thursday, November 08, 2007 11:42 PM | Feedback (2)

Stopping ASP.NET web.config inheritance

If you are only ever running one ASP.NET application on a website this is not an issue. However, if you are running a site which may have an application at the root and other separate applications running in sub or virtual directories, then Settings inheritance could be a problem. You can read more about how config files get inherited on msdn but here's a tip for stopping settings in the root app from getting inherited. The <location> tag is the only tag I've come across which has the inheritInChildApplications attribute. So to target the main <system.web> just wrap it in the location tag as seen below.  <location path="."...

posted @ Wednesday, May 16, 2007 1:11 PM | Feedback (9)

ERROR: 42601: a column definition list is only allowed for functions returning record

Since the postgres forums appear to be a little quite, I'll post my error here as well.  This error is in regards to the postgres .net npgsql driver and seemed to only occur when using stored procs. The Error The behavior I experienced happens when using stored procedures started out with me getting the following error: ERROR: 42601: a column definition list is only allowed for functions returning "record" After checking and double checking the stored proc and the parameters I was sending in I turned Npgsql's debugging on. The error appears to be coming from a statement that looks like the following: select * from...

posted @ Thursday, March 15, 2007 9:30 PM | Feedback (0)

The C# @ String Literal

C# is a pretty sweet language, and there are many, many, many little things that just make the code that much nicer. Have you ever been in a situation where for some reason you NEEDED to have a string/code fragment/js fragment/sql statement inline in your code because you don't believe in resource files or stored procedures? Aside from whatever great debate about "if you should or shouldn't". If you are going to, please, please, learn to take advantage of what C# has to offer, yes I'm talking about the '@' string literal. Now I'm not going to target anyone in particular,...

posted @ Tuesday, March 06, 2007 1:10 AM | Feedback (4)

Search Engine Optimization for .NET

Search Engine Bot Detection So after I first noticed a large build up of strange session Urls in Google searches for my domain I've then done a little bit of research into the issue and discovered it was .NET not detecting that the search engine spider was Mozilla/5.0 compliant and inserting some rubbish session id into the url. It's been nearly a week since my changes to correct this and there's already a small indication of the 'healing' process. The pages are not out of the index yet, (and yes I do know that I 'could' remove them manually) but there...

posted @ Sunday, December 17, 2006 1:42 AM | Feedback (2)

ASP.NET 2.0 Mozilla Browser Detection Hole

It has recently come to my attention that there is something drastically wrong with the way search engines have been indexing my ASP.NET 2.0 blog. As I've started to explain previously, this is because of the way the browser detection is set up. To give a brief rundown ASP.NET 2.0 has a default browser definition which seems to assume that the default browser is fairly capable and supports common things such as javascript and cookies. A browser definition can get inherited into other definitions which can then override specific properties to update it for that specific browser or browser version. Apparently in...

posted @ Monday, December 11, 2006 12:58 PM | Feedback (11)

Cleaning Up ASP.NET Sessions in Google

ASP.NET and Dirty Urls There are two things that have been bothering me about pages that are getting indexed in Google from an ASP.NET application. The first is somehow there are ASP.NET Session Urls ending up in the Google index. This is bad because searchers that actually do click these links are likely to get a 500 error (internal server error) because they will be trying to access a page of an expired session. How is Google finding all these 'bad' urls? Well apparently there is no browser definition in ASP.NET 2.0 for the Googlebot's useragent string, so when the spider...

posted @ Wednesday, December 06, 2006 11:49 PM | Feedback (3)

Brendan's C# Modifier Keyword Guide

After a little bit of thought from my last post I decided to write a small sample class and use tooltips to document what some of the most common modifiers do. So run your mouse over the boxes below to see the documentation about the keyword. Edit: In an effort to unbreak my RSS I've moved it to it's own page http://www.kowitz.net/CSharp_Modifier_Keyword_Guide.aspx

posted @ Sunday, November 05, 2006 11:54 AM | Feedback (0)

The public new overridden int ID

As with most days I learn things about programming and C# that I didn't know before, until I've already past the point where I know I'll never ever come close knowing everything. Now, I though I knew a bit about method/property modifiers, well apparently not, found one today that I haven't seen before. Unexpected places to find 'new':Maybe I've been living under a rock for quiet some time, but I'm talking about the keyword 'new'. And I bet the first this that's come to mind is 'what kind of developer are you? everyone knows about 'new'! Well it...

posted @ Tuesday, October 31, 2006 12:45 PM | Feedback (0)

Accessing controls on a masterpage

Here are a couple of neat things in ASP.NET 2.0 that I've used recently. Access the controls on a master page. You'd think it must be possible, and it is, it's easy to do.Label Label1 = (Label)Master.FindControl("Label1"); Label1.Text = "Set by child page"; ObjectDataSource...Before I used it I was suspecting that it may have been like one of the adapters in 1.1 which I try to keep away from on a the page itself. I find it cleaner to put all the database access logic in a datalayer under the business objects. Well it seems Microsoft may have been thinking...

posted @ Wednesday, August 02, 2006 9:31 PM | Feedback (1)

Removing ASP.NET Calendar Control's Default Style

This problem has been bugging me ever since we hit it the other day at work. It occurred when Al was trying to style a .NET Calendar using an external CSS file. In the properties of the Calendar you can specify the css class using the CssClass fields, these render correctly. However, there are a few properties such as DayStyle which have a default colour that renders in the ‘Style’ tag of the link. If the ForeColor is specified it does render that colour, if you don’t specify, it renders ‘black’. This effectively makes the CssClass field useless for setting...

posted @ Saturday, May 13, 2006 2:03 AM | Feedback (6)

CSharp and Datalayers

One thing that has become very important in my recent work is datalayers. I’ve seen a few difference approaches to filling objects after the data has been retrieved. All methods appear very similar in theory, but the implementations and restrictions are slightly different. I’ll start with the simplest example which is right here is “Single User Blog”. I like the theory behind this, basically there is a root object in the application that all other objects inherit from, in this case “ApplicationDataObject”. This root object contains a Fill() function which can in simplest terms, populate itself from an input datasource....

posted @ Tuesday, March 21, 2006 12:05 AM | Feedback (1)

'TextBox' cannot have children of type 'DataBoundLiteralControl'.

From what I've seen this particular error is already pretty well documented, however it is one of the things that Visual Studio is a little misleading about. Seems this is a lesson why you can't always rely on intellisense to tell you what to type. In a datagrid, the following code will error: Error: <EDITITEMTEMPLATE> <asp:TextBox id=TextBox1 runat="server"> <%# TimeZoneInformation.ToLocalShortDateString( (DateTime)DataBinder.Eval(Container.DataItem, "FromDate")) %></asp:TextBox> </EDITITEMTEMPLATE> This will land you with a "'TextBox' cannot have children of type 'DataBoundLiteralControl'." error. Solution.<EDITITEMTEMPLATE><asp:TextBox id="TextBox1" runat="server" value='<%# TimeZoneInformation.ToLocalShortDateString( (DateTime)DataBinder.Eval(Container.DataItem, "FromDate")) %>'> </asp:TextBox> </EditItemTemplate>In this case the "value" tag is valid, even though intellisense doesn't think...

posted @ Monday, March 13, 2006 5:14 AM | Feedback (12)

IHTTPModule vs IHTTPHandler

The proof of concept IHTTPModule friendly url rewriter for SUB using regular expressions. For my previous post regarding friendly urls see here.Edit: To read more on this topic see my next post here. There are very important differences between IHttpHandler and IHttpModule. Last week I was also playing around with IHTTPHandler as a way of implementing a stop-gap friendly url handler. Now the important difference is, once the request is sent to IHTTPHandler, if it doesn’t send any content back, the user will basically see a blank page. Now that’s not to say that one handler can’t pass the request only another handler...

posted @ Wednesday, March 08, 2006 6:43 AM | Feedback (3)

SQLite data provider for SingleUserBlog

Please be aware that it may still have bugs. I have been using it now for about two weeks, making small refinements as I’ve found them. To Install. Copy the “sub.db” file in the /Data directory. Ensure the “sub.db” file is writable and not read-only. Copy the "kowitz.SQLiteBlogDataProvider.dll" file into the /Bin directory. Copy the "System.Data.SQLite.dll" file to the /Bin directory. Change the config file so SUB now uses the SQLite provider: <DataAccessType> kowitz.SingleUserBlog.Services.DataReaderBlogDataProvider, kowitz.SQLiteBlogDataProvider </DataAccessType> If you experience any problems when using the provider, just post comments here. Download Get it here [Edit 10-Mar-06: Updated Link]View Source Files OnlineI've...

posted @ Saturday, March 04, 2006 9:54 PM | Feedback (12)

ASP.NET Google Sitemap

Probably the best thing about SUB is its simplicity. There are two things I’d like to try to document in this post, firstly is the code for adding a simple google sitemap, the other is actually a question. I’m still curios to why Darren has used hard coded URL formatting in most of the repeaters when displaying a collection of posts. Under the APP_Code/ directory there is a UrlFormatter class which can generate a URL for any ApplicationDataObject (post or feedback item). Taking this UrlFormatter class into consideration I have modifiied the GSiteMap.ashx file include a method that will write...

posted @ Wednesday, March 01, 2006 11:45 PM | Feedback (2)