How to Bind DateTime from col in database to Textbox

Posted on 2008-11-16
Last Modified: 2012-05-05
This should be easy, and probably is...but

How do I bind a short format of datetime from sql server 2005 database to a textbox? I tried using tostring().substring(0,10) but if there are nulls this causes error.

Question by:ChiBella
    LVL 9

    Expert Comment

    cast the column to a DateTime then use .ToShortDateTime()


    Open in new window

    LVL 1

    Expert Comment

    textBoxMessage.Text = column != DBNull.Value ? ((DateTime) column).ToShortDateString() : null;

    VB.NET (should be close):
    textBoxMessage.Text = IIf(column <> DBNull.Value ,  (CType(column, DateTime)).ToShortDateString() , Nothing);

    Replace column with DB column var.

    Accepted Solution

    this is what worked for me:

    txtOrdDate.Text = String.Format("{0:d}", orderDetail.ORDER_DATE);

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    746 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

    15 Experts available now in Live!

    Get 1:1 Help Now