Solved

How to truncate date value coming out of DB

Posted on 2007-11-15
5
1,710 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

11 Experts available now in Live!

Get 1:1 Help Now