DatePicker and DB

base836ball

Active member
Joined
May 4, 2005
Messages
31
Programming Experience
1-3
Hey everyone,

I have a datepicker and I want it to write the current date to the database unless I click on it and pick another date. So by default I want the current date to write to the DB. Since today is the 18th I will use that as an example.

If I want the datepicker to use 05/18/2005 then I have to manually click inside the datepicker myself and do it. Even though the picker says the date on the form it wont write to the DB unless I click it. How can automatically make it write the current date to the DB. I tried this

If datepicker = Nothing Then
datepicker = Date.Today()
End If

It works well on the form but in the DB it shows an empty space.
Any help will work

Thanks,
Drew
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
try this
datepicker = SelectedDate.ToShortDateString

Or if you want to get todays date you can use

System.DateTime.Today.ToShortDateString

 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
If datepicker = Nothing Then
datepicker = Date.Today()
End If

operator = is not defined for dateTimePicker control ... and you should use 'Is' to compare two reference types like this below:

VB.NET:
If datePicker Is Nothing then[/color]
[color=blue]DatePicker.text = Date.Today[/color]
[color=blue]Else[/color]
[color=blue]{...}

btw, datepicker = SelectedDate.ToShortDateString 'is wrong as Date cannot be converted to datatimePicker control ... ;) and you need to use text property for the purpose.

but the biggest point is how can be DTP empty/blank if we know that the DateTimePicker control does not allow you to enter a blank date. There is a way to work around this inability but it asks to derive your own control from the DateTimePicker and handle the edit box yourself so you would allow the delete or backspace key to delete the text ...

Cheers :)
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
About solution,

I think you should use textBox for the purpose ... hiden or not it really doesn't matter but you should pass DTP's value to the textBox control as it's easier to manage out the date/s needed for DB ...
VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] DateTimePicker1_ValueChanged([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] DateTimePicker1.ValueChanged
 
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].txtTextBox1.Text = [/size][size=2][color=#0000ff]Me[/color][/size][size=2].DateTimePicker1.Value.ToShortDateString
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size]


[size=2][color=#0000ff][color=#000000]
[/color][/color][/size]

VB.NET:
[/color][/color][/size]
 
[/color][/color][/size][size=2][color=#0000ff][color=#000000]later you could check the textbox only and proceed textBox text to the DB:[/color][/color][/size]
[size=2][color=#0000ff][color=#000000][/color][/color][/size]
[size=2][color=#0000ff][color=#000000][size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].txtTextBox1.Text = [/size][size=2][color=#0000ff]String[/color][/size][size=2].Empty [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].txtTextObject.Text = [/size][size=2][color=#0000ff]Date[/color][/size][size=2].Today[/size]
 
[size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If[/color][/size]

[size=2][color=#0000ff][size=2][color=#0000ff][color=#000000]

Kind regards ;)
[/color][/size]
 

base836ball

Active member
Joined
May 4, 2005
Messages
31
Programming Experience
1-3
Makes a lot of sense. I had to do the from listbox to another when making an audio player. Just never thought I would need it here. I will give it a try and if anything goes wrong I will write back.

Thanks for the fast respose as well

Drew
 

angitapalli

Member
Joined
Mar 29, 2005
Messages
7
Programming Experience
1-3
You coulc use :

MonthCalendar1.TodayDate.GetDateTimeFormats().GetValue(7)

This will give you the current date in "MM/dd/YYYY" format. You can store this in a date variable or You can assign this to your column in the database. Hope this works out.


 
Top Bottom