Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MaskedTextBox - Date Doesn't Fit Format

Posted on 2012-03-27
9
Medium Priority
?
697 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 2000 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

670 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