[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to truncate date value coming out of DB

Posted on 2007-11-15
5
Medium Priority
?
1,724 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Author Comment

by:Tom 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 1600 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 400 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:Tom Knowlton
ID: 31409383
Thank you both!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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