Solved

How to truncate date value coming out of DB

Posted on 2007-11-15
5
1,716 Views
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());
        }
    }
0
Comment
Question by:knowlton
  • 2
  • 2
5 Comments
 
LVL 5

Author Comment

by:knowlton
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();
0
 
LVL 33

Accepted Solution

by:
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
0
 
LVL 6

Expert Comment

by:ventaur
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

0
 
LVL 6

Assisted Solution

by:ventaur
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

0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 31409383
Thank you both!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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