Showing posts from February, 2008

ICacheItemExpiration implementation for a set of Rows

Here is a little code that will create a class that you can use with Enterprise Library caching.  Basically it allows you to define a WHERE clause where any object in that WHERE clause changes (across a single table) then the cache will expire the object in the cache.  Think of it as a way to store a List<> of object that represent rows in a table in the cache and be able to detect if any row changes so that you can drop the cached list.  LastUpdate is a timestamp column that needs to be on every row.  This works since timestamp column type is always incremented by one, which means that SELECT MAX(LastUpdate) will tell you the maximum change across all rows. using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data; using System.Configuration; using Microsoft.Practices.EnterpriseLibrary.Caching; using Microsoft.Practices.EnterpriseLibrary.Caching.Properties; namespace ERS.Base { /// <summary> ///

Movin' along with .net 2.0 web project in c#

I'm getting farther along in my new pet project. I had many issues with converting to a web application project. Then I balked at which style of data access/data layer to use. Then I fidgetted with notation and styles. After wasting bunches of time, I'm finally moving along. Data is going in, data is going out. It looks aweful and the backend is a SQL client. But the user functionality is moving along. The rest can be fixed after I feel some momentum. So I'm using 2.0 c# with sql backend. Enterprise Framework from MS patterns and practices. MS Membership provider with a SQL provider. Data layer is business objects with provider model. I have nunit plugged in but haven't written in unit tests yet. Argh! Since I'm not a graphic designer, it looks BAD. A bad ripoff of .   Programming to .  

My Lastest Tech Article on 15Seconds

Image  - Create a Slide Show Using the AJAX SlideShow and TreeView Controls

Skining Custom Controls

For the longest time I couldn't skin my custom control, background: a custom control looks like this: public class MyGridView : GridView { ... } This custom controls was in a separate class library.  Bill researched the problem and it appears that you can't skin a custom control that is in a nested namespace.  I.e. the custom control can't have this: namespace MyNamespace.SubNameSpace {    public class MyGridView : GridView    {       ...    } } It has to be in a single namespace to work: namespace MyNamespace {    public class MyGridView : GridView    {      ...    } } Visual Studio 2005 - .NET 2.0 - ASP.NET 2.0 {6230289B-5BEE-409e-932A-2F01FA407A92}  

Compare Byte Arrays

Slightly more efficient by starting off with a reference compare. If the same two arrays are passed in, no need to compare the elements. I don't think there is a better way unless you use unsafe code / pointers. The use of generics makes this more general purpose. bool Compare<T>(T[] left, T[] right) {     // handles the same array     // handles both null     if (left == right)         return true ;       // fails when either are null     if (left == null || right == null )         return false ;       if (left.Length != right.Length)         return false ;       for ( int i = 0; i < left.Length; i++)     {         if (!left[i].Equals(right[i]))             return false ;     }       return true ; }  

Compare IEnumerable (a still better option)

My office mate Chad came up with yet an even better option. You need to be using 3.5 and include the System.Linq extension method namespace. It didn't perform quite as well but pretty close. Even more general purpose. Note that the default SequenceEqual method throws an exception when either operator is null so I handled those cases first. bool Compare<T>( IEnumerable <T> left, IEnumerable <T> right) {     // handles the same array     // handles both null     if (left == right)         return true ;       // fails when either are null     if (left == null || right == null )         return false ;       return left.SequenceEqual(right); }  

When a Codebase Sucks There's Always JavaScript

In working on some inherited code in a very large and complex website I was a little bit skeptical of touching the code.  Let's just say the codebase was dubious at best.  This presented a problem as I had 3 days to make a lot of changes in the admin section of the site.  When inheriting code there is always a learning curve.  Unfortunately I didn't have this luxury as I spent 1.5 days getting the code running locally (still haven't COMPLETELY finished this).  This left me with about another half-day to learn the handful of tables I was to be using.  One day to actually write the code.  No problem.  So the client relayed to the PM what needed to be done which was ultimately relayed to me.  "When a manager selects a value from the whatever dropdownlist the values for the FileUpload control disappears and the user has to re-enter this value (which is by design as this would be a huuuuge security risk. Think if you could programmatically set the postedfile for the FileU

Here Is A Little Code That I Simplified

Here is a little code that I simplified: public Boolean Compare(Byte[] a, Byte[] b) { return (a=b); } Seems like it should work right? Compare each Byte in a with the same Byte position in b and return if the two arrays have equal values in each position? It doesn't do that -- it compares the two objects to see if they are equal. My solution: public Boolean Compare(Byte[] a, Byte[] b) { if ((a == null ) && (b ! = null )) return ( false ); if ((a ! = null ) && (b == null )) return ( false ); if ((a == null ) && (b == null )) return ( true ); if (a.Length ! = b.Length) return ( false ); for ( int i = 0; i < a.Length; i++) if (a[i] ! = b[i]) return ( false ); return ( true ); } Is there a better solution? {6230289B-5BEE-409e-932A-2F01FA407A92}

Build Issues with Web Projects vs Web Applications in Visual Studio 2005 (Windows 2003)

My virtual dev server was running out of hard drive space. Wayne ’s preferred solution was a new and different dev server. I called him a few days ago about a build problem but I don’t remember what that was. He dinked with my solution. One of things he turned on was signing. I didn’t notice and at the time everything kept building. But I needed to move to the new server. The major apps were installed. I just needed to move all my VS projects, and whatever else I had on the old server. This part is the drudgery. Move everything and then see if it builds.   Well it built but I didn’t notice that my business object dll wasn’t getting moved up to the web site. So the ObjectDataSource wasn’t finding my new class methods for the business object. Of course, I didn’t figure that out immediately. I thought the docs for the ODS were just missing something. Wayne looked at the project and couldn’t figure out how to solve the problem immediately because of the type of project I had for the web

Back To Anonymous Types

C# .NET 3.0 has anonymous types, since I am working on some legacy projects I have the experience of using anonymous types already in VBScript.  Remember classic ASP? Not having to Dim your variables, and being able to assign a variable to an int, then a string.  Has it been so long ago that you forgotten all the fun you have had with anonymous types in classic ASP? I have a project that is classic ASP and SQL Server, if we change the SQL Server column (i.e. rename it or change the type) we have to scan the classic ASP (Find In Files) then make the changes to all inline SELECTS, UPDATES, and DELETES. So then we got smart and changed to stored procedures, now if the type changes the stored procedures doesn't compile, easier to find places that need to be changed, however we had to adjust the classic ASP to handle the new type as an output -- really not a problem becuase all VBScript is anonymous types.  So then we got really smart and used ASP.NET CLR 2.0, a class library to talk

Comma Delimited String From An Array -- The Final Word

In response to the previous posting .  James Arendt has comment the final word: static internal String AredntWay(String[] list) {    return (String.Join(',',list)); } Thanks James Arendt! {6230289B-5BEE-409e-932A-2F01FA407A92}

Where do you put your code?

I have a friend who is constantly saying he can write code much faster by configuring the various data controls with the SQL insert, select, update, and delete statements. I tried this. It is true the page was much faster built. A few clicks, a few typed attributes. However, and this is a big however , that style of coding makes several assumptions. Under each assumptions, I'll also list why I think a middle layer needs to exist between the GUI and the database. For those of you who are laughing that I even need to make this argument, beware what project you take over. Look at the code first. you configured the controls correctly the first time    In order for the process to truely be fast, you can only touch it once. With a code library, you can easily grow the middle layer to suit all your needs without having to retouch that page using a data control. you don't have any need to use that code again in that web app, ever    Whatever you write is what the user, admin, etc s

ObjectDataSource and GridView

I'm working on the infrastructure for a new web site. Lots of data layer so I can test. But I don't want to spend loads of time with the UI for insert/update/delete from an admin standpoint. Sure the UI for customers will get plenty of time but my own interface can be functional but not pretty. I already have robust classes for the data layer. Do I build my own pages with calls to the data layer in the code behind? I want to remove as much testable code from the UI (aspx and aspx.cs) as possible. So I'm going to try to use the GridView with a DataSourceObject. Make the GridView and ObjectDataSource do most of the work since they are far more tested than any UI code I could write. That's my logic anyway.  

Email Newsletter

The one newsletter that arrives in my inbox that is bound to perk me up anytime is from . It is sort of an older style newsletter but the contents are great. The editorials (yes, there are editorials) are also great. The main articles might not be right for me at the time but then there is the question and the community-submitted help code as a featured script.

Comma Delimited String From An Array

Creating a comma delimited string from an array came up yesterday, here is a couple of methods, one from our junior developer and one that I did.  I entitled mine old school, because of the C coding that shaped my programming. static internal String Junior(String[] list) { String output = String.Empty; foreach (String item in list) { output += item + "," ; } output.TrimEnd(','); return (output); } static internal String OldSchool(String[] list) { StringBuilder output = new StringBuilder(); foreach (String item in list) { if (String.IsNullOrEmpty(item)) continue ; if (output.Length > 0) output.Append( "," ); output.Append(item); } return (output.ToString()); } Which one do you like? {6230289B-5BEE-409e-932A-2F01FA407A92}    

Comma Delimited String From An Array using LINQ

In response to the previous posting . A few weeks ago, I would certainly have solved this the same way that you did in "OldSchool" with the exception of using IEnumerable < string >. Why limit your code to just string arrays? Now that we have LINQ, we can get pretty fancy! Technically one line within the method but I split it up to aid readability.   static void Main( string [] args) {     string [] names = new string [] { "Andy" , "Marcy" , "Cindy" , "Jennifer" , "Aaron" };       Console .WriteLine(NewSchool(names)); }   static string NewSchool( IEnumerable < string > list) {     return list.Aggregate( new StringBuilder (),         (sb, n) => sb.Length == 0 ? sb.Append(n) :          sb.Append( "," ).Append(n),         sb => sb.ToString()); }   And for even more fun, I built a simple test harness for each of these three methods. The test: 10000 itterations where I concatenat

Seattle Code Camp:

This presentation was given by Brad Wilson and James Newkirk , Microsoft. I sat in on Brad's Pair Programming Agile presentation at the first Seattle Code camp which I enjoyed alot. The first code camp had more Agile presentations. Apparently Agile is no longer the shinny new toy because there was only one presentation on saturday in that area. James has been with xUnit, since some part of it's JUnit days, through NUnit and now the new version of XUnit. He went through the history which explained some of the wierdness the process has had. He also went through a discussion of what they were concerned about when they decided to build a new TDD framework. It was nice to hear real world examples of bad logic or implementation. My favorite they mentioned was using the framework for code coverage. Code coverage is the process make making sure every line of code is executed. The idea being that the more executed code that goes through testing, the fewer bugs the release product will

Reason #1 Not To Use Open Source

I have several reasons not to use open source and I will be posting all in short order.  By open source, I mean a free assembly I can get on the web that includes the source, usually a development tool that is build by a group of people. For some time I have been debating weather to use open source projects for my C# programming, or to buy professional components.  By debating, I mean thinking about, trying, and experimenting with both -- however now it is time to put my ideas to paper and share.  Disclaimer: in a year I might change my mind, or a posted comment might change my mind.  However here is one tought to chew on: We reference assemblies so that we can gain functionality without having to code the funtionality ourselves.  Microsoft provides some great functionality in the CLR and it allows us to make significant gains in productivitiy by not making up write the stuff in the CLR.  However, if Microsoft is missing the functionality and it isn't specific to your vertical or