locked
Display only Date Part RRS feed

  • Question

  • User-797751191 posted

    Hi

      In below code i want to display Data as only DatePart

    txtDate.Text = sdr["PDate"].ToString();

    Thanks

    Tuesday, July 2, 2019 3:34 PM

Answers

  • User288213138 posted

    Hi jsshivalik,

    I mean I'd like to know the details of "PDate" in your database.Does it only save DateTime type of data and you want show this data in page. I suggest that you could convert its type to string.

    Here's a demo you can use as a reference. In my database, I have a DateTime type of data.

    The code:

    <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </div>
    public string str = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString;
            string query = "select * from Date";
            protected void Page_Load(object sender, EventArgs e)
            {
                using (SqlConnection con = new SqlConnection(str))
                {
                    using (SqlCommand cmd = new SqlCommand(query,con))
                    {
                        con.Open();              
                        SqlDataReader sdr = cmd.ExecuteReader();
    
                        while (sdr.Read())
                        {
                            string s1 = Convert.ToString(sdr["PDate"]);
    
                            DateTime value;
                            DateTime.TryParse(s1, out value);
                            TextBox1.Text = value.ToString(("dd MMM yyyy"));
                        }
                        
                    }
                }
            }
    

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 6:47 AM

All replies

  • User288213138 posted

    Hi jsshivalik,

    According to your description, I'm not quite clear about your requirements.

    jsshivalik

    sdr["PDate"]

    What's in SDR ["PDate"]?I need to know the specific contents of SDR ["PDate"].

    Best regards,

    Sam

    Wednesday, July 3, 2019 5:40 AM
  • User-797751191 posted

    Hi samwu

    using (SqlDataReader sdr = cmd.ExecuteReader())

    Thanks

    Wednesday, July 3, 2019 6:06 AM
  • User288213138 posted

    Hi jsshivalik,

    I mean I'd like to know the details of "PDate" in your database.Does it only save DateTime type of data and you want show this data in page. I suggest that you could convert its type to string.

    Here's a demo you can use as a reference. In my database, I have a DateTime type of data.

    The code:

    <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </div>
    public string str = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString;
            string query = "select * from Date";
            protected void Page_Load(object sender, EventArgs e)
            {
                using (SqlConnection con = new SqlConnection(str))
                {
                    using (SqlCommand cmd = new SqlCommand(query,con))
                    {
                        con.Open();              
                        SqlDataReader sdr = cmd.ExecuteReader();
    
                        while (sdr.Read())
                        {
                            string s1 = Convert.ToString(sdr["PDate"]);
    
                            DateTime value;
                            DateTime.TryParse(s1, out value);
                            TextBox1.Text = value.ToString(("dd MMM yyyy"));
                        }
                        
                    }
                }
            }
    

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 6:47 AM