How to truncate date value coming out of DB

Posted on 2007-11-15
Last Modified: 2010-04-21
I have a method which fills-in the TextBoxes of my control.

The problem is, the date coming out has a TimeStamp, and I don't want to display it.

Is there someway I can format the value coming out of the datarow object so that it just returns a data?

public void FillPersonalInfoFromDB(string username, string connStringName)
        string userID = CommonFunctions.GetUserIdGivenUserName(username);
        string sqlStatement = "select Birthdate, SSN from aspnet_PersonalInfo where UserId = '" + userID + "'";
        DataSet ds = CommonFunctions.ReturnDataSet(connStringName, sqlStatement);

        foreach (DataRow dr in ds.Tables[0].Rows)
            //4/5/1974 12:00:00 AM is what this.TextBoxBirthday.Text holds after the line below runs
            //  I just want to show   4/5/1974
            this.TextBoxBirthday.Text = dr["Birthdate"].ToString();

            this.TextBoxSSN.Text = CommonFunctions.AddDashesToSSN(dr["SSN"].ToString());
Question by:knowlton
  • 2
  • 2

Author Comment

ID: 20291090
This works...but it seems kinda long and clunky.  Is there a shorter, nicer way to do it?

this.TextBoxBirthday.Text = Convert.ToDateTime(dr["Birthdate"].ToString()).ToShortDateString();
LVL 33

Accepted Solution

raterus earned 400 total points
ID: 20291316
I always use the ToShortDateString function, I don't think you are going to find anything much easier without getting into formatting strings (which you'll forget next time you need it!)

Depending on your database, you might be able to do it there too.

with sql server it would be (just as long and clunky)

select cast(convert(varchar(10), getdate(), 101) as smalldatetime) as justadate

Expert Comment

ID: 20291828
Try this for a quick clean solution with only a few changes to your code.

this.TextBoxBirthday.Text = dr["Birthdate"].ToString("d");


Open in new window


Assisted Solution

ventaur earned 100 total points
ID: 20291856
You'll actually need to cast the value first. Sorry, my fingers get away from me sometimes.

this.TextBoxBirthday.Text = ((DateTime)dr["Birthdate"]).ToString("d");


Open in new window


Author Closing Comment

ID: 31409383
Thank you both!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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