Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Force Null value to a DateTime property

Posted on 2004-10-20
3
Medium Priority
?
1,793 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 1500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

972 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