Posts Tagged ‘Solutions’

Storing Partial/Incomplete Date/DateTime Values in SQL

Wednesday, March 4th, 2009

I am currently working on a project where I am taking data from an old database.  Unfortunately, many of the date fields in the database contain incomplete date values, simply because not all the data was available when it was entered (it was taken off paper, memory and other relatively unreliable storage media).

In the old database, these fields were stored as text.  However, I wanted them to be searchable by date, so I was thinking about it and I have come up with a couple of possible ideas…

Firstly, you can store the centrepoint datetime and a float to represent a timespan in days of how accurate this is.  Whilst I think it is possible to do an SQL query to query this (I haven’t actually tried it), it is a bit messy, and I think it would probably be a nightmare to write a Linq to SQL query or use another query framework.

Secondly, you can store the min and max datetime that it could be.  This also allows you to do a little clever parsing when you retrieve the date from the database and for example, you can list a date which is in the database as 2009-01-01 to 2009-01-31 (in ISO date format)  as “January 2009″.

Of course, both of these only work if the missing data is the least significant part (in mathematical terms).  I mean, if you know the day and month, but not the year, neither of these schemes work.  For that you’d probably need to use 3 nullable integer fields, but then searching on that would be a pain.

Linq and Reflection

Wednesday, April 2nd, 2008

I’ve been using Linq to do queries on Reflection objects recently and I’ve found that it works quite well.  An example: -

From pi In Me.GetType.GetProperties Select pi Where pi.CanRead And pi.CanWrite And Not pi.Name Like “*_Display” And pi.GetGetMethod.GetParameters.Length = 0 And (pi.PropertyType Is GetType(String) Or pi.PropertyType Is GetType(Boolean) Or pi.PropertyType Is GetType(Date) Or pi.PropertyType Is GetType(Integer) Or pi.PropertyType Is GetType(Boolean?) Or pi.PropertyType Is GetType(Date?) Or pi.PropertyType Is GetType(Integer?) Or pi.PropertyType.IsEnum)

String Processing With Linq

Wednesday, March 26th, 2008

It just occurred to me that since a String can be treated as an array of characters, you should be able to do a Linq query on it, so I tried it, and it seems to work quite well…

Dim Test = “The cat sat on the mat”
Dim Test2 = (From c In Test Select c Where c <> ” “).ToArray()
Console.Write(Test2)

Personally, I much prefer string processing this way than with Regular Expressions.  Granted, it isn’t so versatile and is undoubtedly more processor intensive, but if you aren’t bothered about the clock cycles and it will do what you want, it is much easier to code and read!

On Reflection Part 2: GetType

Monday, March 24th, 2008
This entry is part of a series, On Reflection»

In order to use reflection in .NET, you need to get an object of class Type.  I will explain how to use the Type object later, but first, you need to be able to get it.

There are 3 ways to get a Type in .NET.

  1. VB.NET has a GetType keyword which takes a class as a parameter.  eg. you can writeDim T as Type = GetType(String)

    This is not really a function, because you are passing in a class, not an actual value, and not even the name of a class!  C# has a similar keyword, which I think is called typeof.

  2. All .NET objects inherit from Object.  Object has a function defined on it called GetType, so all you need to do is eg.Dim p as New Person()
    Dim T as Type = p.GetType()

    Note that if you have an Interface reference you may need to cast it using the CType operator in order to access the GetType function.  You should be able to cast any .NET object to Object.

  3. You can get a Type by name from an Assembly.  I will post details on this in a later post on Assemblies.

Structured Exception Handling in .NET Part 5: Throwing new Exceptions

Thursday, March 20th, 2008
This entry is part of a series, Structured Exception Handling in .NET»

As I mentioned earlier, throwing Exceptions can be a very useful way of notifying the appropriate code that something unexpected happened.  In order to throw an Exception, first you create an object of type Exception, or a class which inherits from Exception if there is a more appropriate one.  You should also set the Message property, which can usually be done from a constructor.  You can actually do all this in a single line.  eg.

Throw New Exception(“Invalid username”)

Some Exception classes will want additional data, depending on the type of Exception that you are notifying about.  It is worth looking around the .NET framework or any other set of code that you are using to see what Exceptions are available.  I will post up some common ones in a later post.

Affiliate Programs

Thursday, March 20th, 2008

If you want to make some money from an existing website or forum, a good way of doing it can be to sign up to affiliate programs.  These work by you linking to companies or specific products and services that may be of interest to your readers.  The pay depends on the affiliate program.  For some programs you get paid per click through, but most you need to actually get them a sale.  There is a company called TradeDoubler who run a very large number of affiliate programs with some very big names.

Google AdWords

Wednesday, March 19th, 2008

If you want to bring some (more) visitors to your website, Google have a keyword based system that they use to show adverts next to their search results and on people’s websites (see down the right hand side for example). You only have to pay for any visitors that you get – you don’t pay each time the advert is shown. Also, because it is keyword based, you can target people who are searching or viewing a website about something related to your product/service. This means you are much more likely to get a sale.


The .NET Stack Part 2: The StackTrace Class

Tuesday, March 18th, 2008
This entry is part of a series, The .NET Stack»

The StackTrace class is used to get a collection of StackFrames.  A StackFrame contains information about a single point in the Stack.  I’ll come back to that in a later post.  The StackTrace class has multiple constructors with various parameters: -

The Default Constructor (no parameters)
Gets the current point in the stack in this function that you are calling it from.

fNeedFileInfo as Boolean
Whether we need the line numbers, file names etc in the stack trace (if available).

skipFrames as Integer
The number of frames to skip.  1 starts with the function that called this function. 2 starts with the one that called that function etc.

e as System.Exception
An Exception to build the stack trace for.

frame as StackFrame
A frame that this stack trace should contain.

targetThread as Thread
The thread to get the stack trace for.

Since there are 10 different constructors in .NET 3.5 at the time I am writing this, there are plenty of combinations that these can be used in.

The StackTrace class has several members that are of interest: -

FrameCount
The number of frames that this StackTrace contains.

GetFrame(index as Integer)
Get a frame at a certain position.  See skipFrames (above) for what the position means.

GetFrames
Returns an array of the frames in the StackTrace.

Entries in this series:
  1. The .NET Stack Part 1: How The Stack Works
  2. The .NET Stack Part 2: The StackTrace Class
Powered by Hackadelic Sliding Notes 1.6.5

.NET Compilation Part 1: Profiles

Tuesday, March 18th, 2008
This entry is part of a series, .NET Compilation»

You may have noticed that in Visual Studio, most projects start off with Debug and Release profiles. This is used to control the way that the code is compiled.  By default, Debug mode creates a pdb file and compiles the code to allow it to be used.  A pdb file contains information which will help you debug the program, such as information about how the source code maps to the compiled code.  This means that eg. when you get an Exception, the StackTrace property will give you information including line numbers.

On the other hand, a Release profile is much more optimised, both in terms of file size and code speed.  However, it is entirely up to you whether you ship a Debug application.  I wouldn’t recommend it for a large number of users, but for a constantly changing application in a small office, full stack traces with line numbers can be useful to find bugs that are difficult to trace.

SQL Server Express Edition

Tuesday, March 18th, 2008

SQL Server Express Edition is the free version of Microsoft SQL Server, and is a replacement for MSDE.  It has almost all the features of the other versions of SQL Server.  There is a common misconception that it is limited in terms of the number of connections it can accept.  I am pretty sure this is something that people think because it used to be true of MSDE.  It does have some limitations, however, including : -

  • Only supports 1 CPU (although it won’t break if you put it on a multicore/multiprocessor system)
  • Won’t use more than 1GB RAM
  • Databases can’t grow to more than 4GB

This is fine for most jobs, but obviously if you have a really busy or big system then you’ll need to go for a higher edition or another alternative.