Solved

MaskedTextBox - Date Doesn't Fit Format

Posted on 2012-03-27
9
686 Views
Last Modified: 2012-03-28
Using VB.Net 2010 (Pro)  (the database is SQL Server 2008, if it matters)

In my app, I retrieve data from the database and load textboxes.  One of the textboxes is a maskedtextbox.  When the date returned from the database is, like, 5/8/2005, the maskedtextbox will display "58/20/05__"

As a side note, this date, (5/8/2005) was put in to the database from this very same control, but it was typed in as 05/08/2005.

How do I get the proper date to display?

TIA
0
Comment
Question by:Clif
  • 4
  • 4
9 Comments
 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 500 total points
ID: 37773495
Assuming you have a date variable:

MaskedTextBox.Text = MyDate.ToString("MM/dd/YYYY")

If you don't have a date variable, convert to a date variable...
0
 
LVL 10

Author Comment

by:Clif
ID: 37773666
It gives me the error:

Conversion from string "MM/dd/YYYY" to type 'Integer' is not valid.

The variable is a date type and contains the value

#11/15/2011#
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 37773701
Show the code for this please?

Note that just because your datareader/datatable has a date type within, doesn't mean you don't have to convert to a date type variable... It's an object unless specifically cast as Date.  Which is what the error message above is telling you.  You are not calling datetime.tostring, but object.tostring.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:Clif
ID: 37776330
If IsDate(VehicleList(i).Reg_Expr) Then
    txtRegExpiry.Text = VehicleList(i).Reg_Expr.ToString("MM/dd/YYYY")
Else
    txtRegExpiry.Text = ""
End If

Open in new window





(Yes, it bothers me too that a variable for "Registration Expiration" is named "Reg_Expr", but that's the hand I was dealt.)
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37776356
Assuming that txtRegExpiry is the MaskedTextBox, set its AllowPromptAsInput property to true before setting the text property.
0
 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 500 total points
ID: 37776404
Give this a shot.  Explicitly converting the Reg_Expr to date.

If IsDate(VehicleList(i).Reg_Expr) Then
    txtRegExpiry.Text = CDate(VehicleList(i).Reg_Expr).ToString("MM/dd/yyyy")
Else
    txtRegExpiry.Text = ""
End If

Open in new window

0
 
LVL 10

Author Closing Comment

by:Clif
ID: 37776558
The combination between explicitly casting to date and formatting to a date string worked.

One small point, though, "MM/dd/YYYY" is invalid.  It should have been "MM/dd/yyyy"  (lowercase 'y').  With uppercase, the year does not show up.
0
 
LVL 10

Author Comment

by:Clif
ID: 37776567
BTW, CodeCruiser, your suggestion did not help.  The maskedtextbox's "AllowPromptAsInput" was already set to true (as default, I would guess, since I never set it in the first place).
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 37776664
The uppercase Y slipped past me on my first post...  Glad you got it sorted.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question