Pony
Pony

Reputation: 401

My date field is displaying the time too. How to remove the time?

My Date datatype is date. And it shows the time too in the form which is 12:00:00AM. But in my database It only have 6/6/2014 and 12/12/2016 and don't have this 12:00:00AM. How to remove that? Below are my appointment form codes. Please tell me how to delete the time?

enter image description here

2 dates now

enter image description here

enter image description here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class member_viewappointment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            // call BindGridView
            bindGridView();

        }
    }

    private void bindGridView()
    {
        int ID = Convert.ToInt32(Session["ID"].ToString());
        //get connection string from web.config
        string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT pat.pFirstName AS FirstName, aStatus AS Status,  aDate AS Date, aTime AS Time, aContact AS Contact, aHeight AS Height, aWeight AS Weight, med.mcCentre AS MedicalCentre from appointment AS app ";
        strCommandText += " LEFT OUTER JOIN MEDICALCENTRE as med on app.mcid = med.mcid";
        strCommandText += " LEFT OUTER JOIN PATIENT as pat on app.patientid = pat.patientid ";
        strCommandText += " WHERE app.patientid = " + ID.ToString();

        try
        {
            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

            myConnect.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(reader);
            grdViewAppointment.DataSource = dt;
            grdViewAppointment.DataBind();
            lblResult.Text = "";

            reader.Close();
        }
        catch (SqlException ex)
        {
            lblResult.Text = "Error:" + ex.Message.ToString();
        }
        finally
        {
            myConnect.Close();
        }

    }
}

Upvotes: 0

Views: 84

Answers (3)

Jake Rote
Jake Rote

Reputation: 2267

When using a DateTime you can do .ToString("d/M/yyyy")

you can do this in the sql convert(varchar, aDate, 103) AS Date

Upvotes: 2

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28413

Just Run this query

private void bindGridView()
    {
        int ID = Convert.ToInt32(Session["ID"].ToString());
        //get connection string from web.config
        string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

 string strCommandText = "SELECT pat.pFirstName AS FirstName, aStatus AS Status,  COnvert(VARCHAR(15),aDate,103) AS Date, aTime AS Time, aContact AS Contact, aHeight AS Height, aWeight AS Weight, med.mcCentre AS MedicalCentre from appointment AS app ";
    strCommandText += " LEFT OUTER JOIN MEDICALCENTRE as med on app.mcid = med.mcid";
    strCommandText += " LEFT OUTER JOIN PATIENT as pat on app.patientid = pat.patientid ";
    strCommandText += " WHERE app.patientid = " + ID.ToString();

        try
        {
            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

            myConnect.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(reader);
            grdViewAppointment.DataSource = dt;
            grdViewAppointment.DataBind();
            lblResult.Text = "";

            reader.Close();
        }
        catch (SqlException ex)
        {
            lblResult.Text = "Error:" + ex.Message.ToString();
        }
        finally
        {
            myConnect.Close();
        }

    }
}

Upvotes: 1

Sachin
Sachin

Reputation: 40990

You can try DataFormatString to set the format of datetime in the bound column

<asp:BoundField DataField="MyDataField" HeaderText="Date" DataFormatString="{0:MM/dd/yyyy}"  />

Upvotes: 1

Related Questions