Solved

MaskedTextBox - Date Doesn't Fit Format

Posted on 2012-03-27
9
683 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now