Solved

Force Null value to a DateTime property

Posted on 2004-10-20
3
1,773 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 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