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
53 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 51

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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 51

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 51

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

762 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