I have a DateTimePicker and Masked Textbox ,Masked Textbox almost covering all of DateTimePicker ,just down arrow key to select the date into the Masked Textbox.

Question No1. What event should I put the current code?I want the selected date to be inside the Masked textbox after it is being selected.

 DateTimePicker4.Format = DateTimePickerFormat.Custom
        DateTimePicker4.CustomFormat = "dd/MM/yyyy" 
        MT.Text = DateTimePicker4.Text

Question No2.
How can the Null value from Masked Textbox save into the database and retrives the same from database?

Dani AI

Generated

Good news: you do not need a MaskedTextBox to support a blank/NULL date. In WinForms, set your DateTimePicker to show a checkbox and treat an unchecked state as “no date.” Always read/write the Value (a DateTime), not Text (a formatted string), to avoid culture issues. Example workflow: if the checkbox is unchecked, save NULL; if checked, save the selected DateTime. This keeps parsing out of your code and avoids dd/MM vs. MM/dd pitfalls. See the ShowCheckBox and Value docs. (learn.microsoft.com)

Saving and loading with NULLs (ADO.NET):

' Read the optional date from the picker
Dim picked As Date? = If(DateTimePicker4.Checked, DateTimePicker4.Value.Date, CType(Nothing, Date?))

' Save
Using cmd As New SqlCommand("INSERT INTO MyTable(MyDate) VALUES (@d)", conn)
    Dim p = cmd.Parameters.Add("@d", SqlDbType.Date)
    p.Value = If(picked.HasValue, DirectCast(picked.Value, Object), DBNull.Value) ' must use DBNull.Value for SQL NULL
    cmd.ExecuteNonQuery()
End Using

' Load (ordinal 0 for example)
Using rd = cmd.ExecuteReader()
    If rd.Read() Then
        If rd.IsDBNull(0) Then
            DateTimePicker4.Checked = False
        Else
            DateTimePicker4.Checked = True
            DateTimePicker4.Value = rd.GetDateTime(0)
        End If
    End If
End Using

When sending NULLs, use DBNull.Value, not an empty string or Nothing; and when reading, use IsDBNull before accessing the value. (learn.microsoft.com)

If you still prefer the overlay pattern asked about, validate the MaskedTextBox by parsing the user entry with TryParseExact for the intended culture and treat an empty or incomplete mask as NULL. This ensures only valid dates make it to your DB. (learn.microsoft.com)

Last, that “Insufficient security permissions to set the system date” hit likely happened because the identifier DateString resolved to VB’s DateAndTime.DateString property (which sets the OS date) rather than your local variable; declaring your variable at form scope, as suggested, prevents that collision. (oreilly.com)

Recommended Answers

All 9 Replies

My question is - Is using a custom format on the DateTimePicker out of the question?

Why are you using a MTB over the DTP?

You can set custom DTP formats like so:

DateTimePicker1.CustomFormat = "dd/MM/yyyy"

Or you can set it by using the GUI.

Something like this should work.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set the format and initialize masked textbox
        DateTimePicker4.Format = DateTimePickerFormat.Custom
        DateTimePicker4.CustomFormat = "dd/MM/yyyy"
        mt.Text = DateTimePicker4.Text
    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker4.ValueChanged
        'change masked textbox every time datepicker changes
        mt.Text = DateTimePicker4.Text
    End Sub 

To set the masked textbox to a null value you can use MT.Text = ""

Begginnerdev I want to use MTB because I want an option to enter Null vlaue into the database and It will not be happening in DTP case ,but in MTB case it is possible.

Thank you Tinstaafl :)

You're welcome.

On a side note. Using a string variable will acheive what you want without using a control that is basically redundant.

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set the format and initialize the variable
        DateTimePicker4.Format = DateTimePickerFormat.Custom
        DateTimePicker4.CustomFormat = "dd/MM/yyyy"
        Dim DateString as String = DateTimePicker4.Text
    End Sub
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker4.ValueChanged
        'change the variable every time datepicker changes
        DateString = DateTimePicker4.Text
    End Sub 

Error is

Insufficient security permissions to set the system date

On this line

DateString = DateTimePicker4.Text

oops put the declaration in the wrong place.
try this:

    Dim DateString as String     
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set the format and initialize the variable
        DateTimePicker4.Format = DateTimePickerFormat.Custom
        DateTimePicker4.CustomFormat = "dd/MM/yyyy"
        DateString = DateTimePicker4.Text
    End Sub
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker4.ValueChanged
        'change the variable every time datepicker changes
        DateString = DateTimePicker4.Text
    End Sub 

Now I don't need MaskedTextbox for the Blank date to be entered into the database?
I can use DateString = "" for empty string to be Entered into the database ?

Yes that should work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.