formating text in a textbox control?

D

djc

Is there a property or method of the textbox web server control that I can
use to format the text in it a certian way. For example I am displaying a
date value from a database in a text box. It displays like so: mm/dd/yyyy
mm:hh:ss. I 'was' using the .ToShortDateString() function like so:
txtETA.Text = drMain.Item("EstimatedDeliveryDate").ToShortDateString() which
worked great until I came across a database record with NULL for this field.
It seems the best thing to do would be to have something attatched to the
textbox control itself that would control formating of it's own contents.
That way I would not need to add a NULL check before using the
..ToShortDateString() function.

any input would be appreciated. Thanks!
 
C

Curt_C [MVP]

Do a null check around it.

if(drMain.Item("EstimatedDeliveryDate") != null)
{
txtETA.Text = drMain.Item("EstimatedDeliveryDate").ToShortDateString() ;
}
else
{
txtETA.Text = "";
}
 
W

William F. Robertson, Jr.

I would highly recommend turning Option String On.

http://weblogs.asp.net/rchartier/archive/2004/01/13/58381.aspx

This would be a compile time error, and you would already have to handle it
differently.

Just like Curt said, you will have to do it in two steps.

if drMain.Item("EstimatedDeliveryDate") == DBNull.Value then
txtETA.Text = ""
else
txtETA.Text = CType( drMain.Item("EstimatedDeliveryDate" ),
DateTime).ToShortDateString()
end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top