Solved

Force Null value to a DateTime property

Posted on 2004-10-20
3
1,759 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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