Solved

Force Null value to a DateTime property

Posted on 2004-10-20
3
1,745 Views
Last Modified: 2008-01-09
I have a class into my asp.net project with two properties defined as datetime.
I need to force null value or blank into these two properties (not 01/01/1900):

beginDate
endDate

I use System.DBNull but i can't use it because return an error, for example:

newActivation.beginDate =  System.DBNull.Value;

the value of the property then is write into a datetime field in a SQL Server Table.

Can you help me?

Enrico Collenzini
0
Comment
Question by:ecolle
3 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12356135
Hi ecolle,

in your stored procedure, create a param beginDate as datetime like this

@beginDate [datetime] = Null

this will set the parameter to Null if you don't pass a value and it will insert Null in you sql table


Regards!
B..M
0
 
LVL 8

Expert Comment

by:razo
ID: 12356156
try begindate=nothing
0
 
LVL 22

Accepted Solution

by:
CJ_S earned 500 total points
ID: 12356161
There are multiple options.
1) You can extend the existing DateTime object so it can also accept the Null values
2) Change the sql statement (check whether the current datetime value is within an accepted range, and pass either the datetime value or a NULL value)
3) Change the logic where you show the date (if the date is within an accepted tange you show the date, otherwise you show 'unavailable')

Allowing NULL values in the database has a performance hit. The execution plan takes more time to generate the results when columsn allow null values. Therefore I do store a datetime value in the database, but when using the data (after I read the data or in a sql statement) I determine whether I show the date or show a text like 'Not available'.

Here's an extended textbox which allows for a date to show either unavailable or the date itself, depending on the value.

      Public Class EditorDatebox
            Inherits System.Web.UI.WebControls.TextBox
            
            Public Property IsEditState as Boolean
                  Get
                        If(ViewState("editstate") Is Nothing) Then
                              Return False
                        Else
                              Return CType(ViewState("editstate"), Boolean)
                        End If
                  End Get
                  Set(ByVal Value as Boolean)
                        ViewState("editstate")=Value
                  End Set
            End Property
            
            Protected Overrides Sub OnInit(ByVal e as System.EventArgs)
                  MaxLength=10
                  CssClass = "formDate"
            End Sub
            
            Public Overrides Property Text as String
                  Get
                        Return MyBase.Text
                  End get
                  Set(ByVal Value as String)
                        '' * make readable datevalue
                        If(Value<>"" AND HVDH.General.isDate(Value)) Then
                              If(System.DateTime.Parse(Value)>System.DateTime.Parse("1-1-1900")) Then
                                    Dim d as System.DateTime = System.DateTime.Parse(Value)
                                    MyBase.Text = d.ToShortDateString()
                              Else
                                    MyBase.Text = "1-1-1800"
                              End If
                        Else
                              MyBase.Text = "1-1-1800"
                        End If
                        
                  End Set
            End Property
            
            Protected Overrides Sub Render(writer As HtmlTextWriter)
                  '' ** afhankelijk van die editstate wordt de textbox of gewone text getood
                  Dim sCurrentText as String
                  sCurrentText = Me.Text
                  If(HVDH.General.isDate(Me.Text)) Then
                        Dim d as System.DateTime = System.DateTime.Parse(Me.text)      
                        If(d<System.DateTime.Parse("1-1-1900")) Then
                              '' ** set onbekend
                              MyBase.Text = Microsoft.VisualBasic.Iif(IsEditState, "", "Unavailable")
                        End If
                  Else
                        MyBase.Text = Microsoft.VisualBasic.Iif(IsEditState, "", "Unavailable")
                  End If
                  
                  If(IsEditState) Then
                        MyBase.Render(writer)
                  Else
                        writer.write(MyBase.Text)                  
                  End If
                  
                  MyBase.Text = sCurrentText
            End Sub
      End Class

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now