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
41 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

786 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