Solved

MaskedTextBox - Date Doesn't Fit Format

Posted on 2012-03-27
9
692 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 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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 When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

636 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