Solved

How do I default to NULL in Date Time Picker, when there is no applicable date in vb.net

Posted on 2016-10-25
9
49 Views
Last Modified: 2016-10-25
I'm using a Date Time Picker. There will not always be a need to select a Date. I would like that entry to default to NULL in the Sql database.
so in other words I would like to give the user an option to select or not select.
can anyone share sample vb.net code that would allow me to default to NULL in Date Time Picker, when there is no applicable date, thank you
0
Comment
Question by:fadiel ras
  • 4
  • 3
  • 2
9 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41858275
hi Fadiel,

so in other words I would like to give the user an option to select or not select
if you're calling a stored procedure, try to make sure that parameter is optional with default value as NULL.

then from your coding, when the date time picker was not being used, just do not pass that parameter to your stored procedure. let the stored procedure to detect whether a value is passed into it.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41858278
You can SET the parameter to NULL in stored procedure like

CREATE Proc ProcName
(
   a DateTime NULL...
)
AS..

In the above case the you dont pass the value to datetime, SP will automatically handle it.
0
 

Author Comment

by:fadiel ras
ID: 41858284
Thanks Gents!
yes I am using a Stored Proc and the Sql Column does allow NULL.

So the record saves back to the database, however is saves as "1900-01-01" instead of NULL
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41858286
So the record saves back to the database, however is saves as "1900-01-01" instead of NULL
how's your vb.net codes look like?
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41858289
Yes default it will save 1900-01-01 as the default date in the column. Try...below..

INSERT INTO [pawan] ([c],[date])
VALUES ('111',NULL)
0
 

Author Comment

by:fadiel ras
ID: 41858291
something like:

                        If IsDBNull(dr.Cells("EffectiveToDate").Value)  Then
                            AddParm("@EffectiveToDate", DBNull.Value.ToString, DbType.String)
                        Else
                            AddParm("@EffectiveToDate", Trim(Replace(dr.Cells("EffectiveToDate").Value.ToString.ToUpper, "'", "")), DbType.String)
                        End If

Open in new window

0
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41858292
try this instead:

                        If IsDBNull(dr.Cells("EffectiveToDate").Value)  = false Then
                           AddParm("@EffectiveToDate", Trim(Replace(dr.Cells("EffectiveToDate").Value.ToString.ToUpper, "'", "")), DbType.String)
                        End If

Open in new window

0
 

Author Closing Comment

by:fadiel ras
ID: 41858297
Solved. Thank you Mr Chong (you genius)
0
 

Author Comment

by:fadiel ras
ID: 41858298
Thanks Pawan, Thanks Ryan. appreciate that...saved me a few hours, I'm sure
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

792 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