Results 1 to 9 of 9

Thread: UpDownNumber or DataTime save as Null

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    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.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    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 -

  3. #3
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    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!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    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.

  5. #5
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    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!

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: UpDownNumber or DataTime save as Null

    Quote Originally Posted by warrenayen View Post
    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).

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    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 -

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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
  •  



Click Here to Expand Forum to Full Width