Solved

Force Null value to a DateTime property

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

735 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