?
Solved

DateTime Format using DataTable

Posted on 2011-10-02
5
Medium Priority
?
285 Views
Last Modified: 2012-05-12
Hello Experts,

I need to retrieve data from my DB to a TextBox Control and when I return it the datetimie data it is returned in this format 10/2/2011 12:00:00. I would like it to just display 10/2/2011 if possible.

The following label beow is what I need formatting help with.

txtDate.Text = data["csch_startdate"].ToString();



    protected void RetrieveCourseScheduleToUpdateValues()
    {
        int csch_id = Convert.ToInt32(Request.QueryString["csch_id"]);

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveCourseScheduleByIDForUpdate";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@csch_id", SqlDbType.Int).Value = csch_id;

            DataTable dtCourseToUpdate = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter();

            try
            {
                conn.Open();

                adp.SelectCommand = cmd;
                adp.Fill(dtCourseToUpdate);

                if (dtCourseToUpdate != null)
                {
                    DataRow data = dtCourseToUpdate.Rows[0];
                    hfv_csch_id.Value = data["csch_id"].ToString();
                    lblCourseNameResult.Text = data["ghaco_name"].ToString();
                    ddlInstructor.SelectedValue = data["hmi_id"].ToString();
                    txtDate.Text = data["csch_startdate"].ToString();
                    txtStartTime.Text = data["csch_starttime"].ToString();
                    txtEndTime.Text = data["csch_endtime"].ToString();
                }
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }
0
Comment
Question by:asp_net2
  • 2
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
jagssidurala earned 1000 total points
ID: 36900103
write code  as below

txtDate.Text = String.Format("{0:MM/dd/yyyy}", data["csch_startdate"]);
0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36900109
0
 
LVL 11

Assisted Solution

by:brutaldev
brutaldev earned 1000 total points
ID: 36900117
Parse the string to a DateTime object first, then ToString it with your desired format.
DateTime.Parse(data["csch_startdate"].ToString()).ToString("dd/MM/yyyy");

Open in new window

0
 
LVL 11

Expert Comment

by:brutaldev
ID: 36900124
jagssidurala beat me to it, using String.Format in his example is neater as well.
0
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 36900407
Thank you both!!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 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