Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

possible to insert NULL into datetime field via textbox 1

Status
Not open for further replies.

johnc83

Technical User
Jan 29, 2008
154
GB
Hi,

My title basically says it all. My datetime field is bound to a textbox. Entering dates is no problem, the update works but I need to be able to enter NULL if the user has accidentally saved a date in there and needs to remove it.

Im starting to think this isn't possible and I hope I am wrong.

Could someone advise please?

Thanks

John

.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 
The only way I can think to do this is to save the values of the existing record in variables, delete the existing record and then insert a new record with the saved values, excluding the date value in question.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
A null date is not a valid date so an error is thrown, I like you don't agree.

If you are using a TextBox and not a DateTimePicker, I have come up with a workaround. I haven't seen it documented anywhere so I'm not sure if there are any inherent problems with it but it works fine for me.

Select the Date TextBox and go to properties -> DataBindings -> Advanced. Now we want to set the Null Value to "", the problem is if we do that VS thinks we don't want to set a null value. So we need to set the null value to something. In the "Null Value:" Box enter something odd like "Rover" an odd string is just easy to search for later. Now in the Solution Explorer select the "Show All Files" Button and expand the form you are using and select the Form.Designer.vb file. Now do a search for "Rover" and you will come up with something like:

Me.ShipDateTextBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.OrdersBindingSource, "ShipDate", True, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "Rover"))

Next simply remove the word Rover and leave the double quotes ""

Me.ShipDateTextBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.OrdersBindingSource, "ShipDate", True, System.Windows.Forms.DataSourceUpdateMode.OnValidation, ""))


Now if the date in the TextBox is removed the value goes back to Null.

Hope this helps
Perrin
 
Hi Perrin, suprisingly that works perfect!! I have read so many pages hovering around the answer (giving workarounds etc..) but never anything that worked as it should but thanks to you I have found it!

Thank you so much. Really appreciate it.

Jebenson - thanks for your reply, I was doing something similar to your answer but appreciate the reply nonetheless.

thanks both of you.

John



.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 
Most Excellent......Thank you.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
John,

I'm glad it worked for you, I struggled with this problem for a while until I stummbled on this solution. I was also surprised how well it worked.

The only problem I have found and it's a minor one is if you later change the advanced databindings the null value changes will be overwritten and you will have to go through this process again.

Perrin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top