How to resolve the SQLDateTime Overflow error in ASP.Net data class

By Joe Payne at March 01, 2009 13:06
Filed Under: AC7 Articles, Personal

I've been working alot with building data classes in ASP.Net.  While building some new tables, I ran into an odd problem.

The SQL table has 3 different datetime columns.  All are set as Nullable in SQL.  But whenever a new instance of my data class is saved, any datetime properties that were not assigned a value throw a SQL DateTime Overflow error exception.  Sure you can get around it by setting some default value, but I don't always want/need that.

Here's what I did to fix the error:

For the property data type, use Nullable(of DateTime) instead of just DateTime like this:

Private _CutoffDate As Nullable(Of DateTime)

<DataObjectField(
True, False, False)> _
Public Property CutoffDate() As Nullable(Of DateTime)

Get

Return Me._CutoffDate

End Get

Set(ByVal value As Nullable(Of DateTime))

If Me._CutoffDate <> value Then

Me._CutoffDate = value

Me.IsDirty = True

End If

End Set

End Property

Alternative to Server.MapPath in a class file

By Joe Payne at January 10, 2009 13:49
Filed Under: Personal

The Server.MapPath function a very useful, but it requires an HTTP Context or a Null exception error is thrown.

In situations where you don't have an HTTP context like a property in a class file, a slightly different approach is required.  I had to do some digging but I found this and it worked perfectly for me.  Let's say you have a String property on a class and you want a separate READONLY property to represent the full Server.MapPath equivalent.  Instead of using Server.MapPath("~/"+Me.OutFile), just use this:

System.Web.Hosting.HostingEnvironment.MapPath("~\" + Me.OutFile)

How to use the Server.MapPath in a class

By Joe Payne at December 08, 2008 08:48
Filed Under: Personal

Learning is the name of the game with ASP.Net.  Today I was trying to use Server.MapPath in my class library.  But it always throw an error and Visual Studio wouldn't suggest which Import statement to use.

 Finally figured out you have specifically add a reference to System.Web in your project.  Apparently "Imports System.Web" at the top of your class file isn't enough.

 So, once you add a reference to System.Web, you can now use System.Web.HttpContext.Current.Server.MapPath()