Solved

MaskedTextBox - Date Doesn't Fit Format

Posted on 2012-03-27
9
685 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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