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
a9e73a31-c4de-4564-964d-cbcc8242c2e0|0|.0
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)
af90b654-eae5-4c35-b263-14c7ac866fac|2|5.0
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()
9b28e808-46a6-4b75-b145-07052196c5b0|0|.0