-
Jun 24th, 2009, 10:32 AM
#1
Thread Starter
Addicted Member
UpDownNumber or DataTime save as Null
Hi All.
In my form I have 3 tools: combobox1, DateTimePicker1, and UpDownNumber1.
I'm using ComboBox_SelectedValueChanged to display UpDownNumber1 if combobox1.value = 3 in rest conditions form will display DateTimePicker1 tool. But when I save a record I have problem. How to save value from tools DateTimePicker1 and UpDownNumber1 as Null if they not display on the form?
Thanks.
-
Jun 24th, 2009, 11:35 AM
#2
Re: UpDownNumber or DataTime save as Null
Does you database allow null on these fields? If it does, you can check the visibility of the controls. If they are visible, you read the values from them. If they are not visible, you update the database with null values for these fields.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jun 24th, 2009, 03:04 PM
#3
Fanatic Member
Re: UpDownNumber or DataTime save as Null
Use DBNull.Value if you're passing parameters to a query. Use the keyword nothing if you're passing to a TableAdapter.
Keep in mind that if the field does NOT allow null values, you'll throw an exception.
Warren Ayen
Senior C# Developer
DLS Software Studios ( http://www.dlssoftwarestudios.com/)
I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
Hey! If you like my post, or I solve your issue, please Rate Me!
-
Jun 26th, 2009, 11:34 AM
#4
Thread Starter
Addicted Member
Re: UpDownNumber or DataTime save as Null
My problem is how to save value from tools DateTimePicker1 and UpDownNumber1 as Null because by defauly UpDownNumber1 has value 0 and DateTimePicker1 has value current date. When click Save I got error message, for example, cannot conver DateTime format to Null.
Thanks.
-
Jun 26th, 2009, 11:40 AM
#5
Fanatic Member
Re: UpDownNumber or DataTime save as Null
You can check for the default value - so if the value of UpDownNumber1 = 0, then pass null to your update.
A dateTimePicker's default value should be "1/1/0001", not the current date, unless you set it to that explicitly. I'd check for it. You can also keep a private field that changes to true if the user makes a change, which means the default value no longer applies.
Warren Ayen
Senior C# Developer
DLS Software Studios ( http://www.dlssoftwarestudios.com/)
I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
Hey! If you like my post, or I solve your issue, please Rate Me!
-
Jun 26th, 2009, 11:41 AM
#6
Re: UpDownNumber or DataTime save as Null
The datetimepicker has a checkbox property, so you can use that to disable/enabled the data selection and that is one way to check for null in this control. It is not very user friendly though, since the user has to click the checkbox to enable date entry, then select a date. Obviously if you could simply clear out the value in the control and consider that null, it would be more desirable. That doesn't exist though, so you would have to write your own, find an already written one (they do exist), or deal with the checkbox.
The numeric up down issue can be resolved using a textbox instead and limiting input to only numeric values. You can then accept blank values and consider this null.
-
Jun 26th, 2009, 11:46 AM
#7
Re: UpDownNumber or DataTime save as Null
Originally Posted by warrenayen
A dateTimePicker's default value should be "1/1/0001", not the current date
From MSDN:
DateTimePicker.Value Property
If the Value property has not been changed in code or by the user, it is set to the current date and time (DateTime.Now).
-
Jun 26th, 2009, 11:57 AM
#8
Re: UpDownNumber or DataTime save as Null
I don't get it... If you want to save null to the database, why can't you just use DBNull.Value? Why bother to read the value from DTP and then try to convert it to DBNull, which is not possible as you already see?
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jun 26th, 2009, 12:02 PM
#9
Re: UpDownNumber or DataTime save as Null
I agree with Stanav, if you are doing something like setting the controls visible property to false when they are not needed, then simply check if they are not visible, and when they are not, use null as your value to save, when they are visible, then use their actual values.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|