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.
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.
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.
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.
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.
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.
Re: UpDownNumber or DataTime save as Null
Quote:
Originally Posted by
warrenayen
A dateTimePicker's default value should be "1/1/0001", not the current date
From MSDN:
Quote:
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).
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?
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.