Solved

How to truncate date value coming out of DB

Posted on 2007-11-15
5
1,722 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 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:Tom Knowlton
ID: 31409383
Thank you both!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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