Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
Dear Friends,
 
Good Day to every one of you............
 
I have a registration page to create the users..In this it has Issue date and Expiry date of user accessing the application.
 
I have set, Issue date will be the system date and for Expiry date administrator will choose it for the user.
 
For Expiry date I have a textbox with Ajax Calendar Extender....when i click on submit the expiry date is storing in database as 11/30/2012
 
But my requirement is.....
 
I want to display date as dd/MM/yyyy Format using label...I have used LABEL.
 
This is my code....Please help.
 
protected void Page_Load(object sender, EventArgs e)
    {
 
        SqlConnection con = new SqlConnection(_connString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "select datediff(dd,getdate(),ExpiryDate) as Edays, CONVERT(varchar, expirydate, 101) as ExpiryDates from dbo.UserAccounts where UserName='" + Session["UserName"].ToString() + "'";
 

        using (con)
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
 
            
 

               // lblExpiryDisplay.Text = DateTime.Parse(reader["ExpiryDates"].ToString()).ToString("dd/MM/yyyy");

                lblExpiryDisplay.Text = "Your Password will Expire on " + reader["ExpiryDates"].ToString() + " , and the Left Over days are " + reader["Edays"].ToString();
 

            }
 
           
        }
        con.Close();
        }
Posted 18-Nov-12 20:57pm
Edited 18-Nov-12 21:01pm
__TR__28.9K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Quote:
lblExpiryDisplay.Text = "Your Password will Expire on " + reader["ExpiryDates"].ToString() + " , and the Left Over days are " + reader["Edays"].ToString();

 
Try this:
lblExpiryDisplay.Text = "Your Password will Expire on " + Convert.ToDateTime(reader["ExpiryDates"]).ToString("dd/MM/yyyy") + " , and the Left Over days are " + reader["Edays"].ToString();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

To get date in dd/MM/yyyy format use
CONVERT(VARCHAR,expirydate,103)
in your query
 
Refer
How to format datetime & date in Sql Server[^]
  Permalink  
Comments
Software Engineer 892 at 19-Nov-12 2:24am
   
Thanx, 103 is working
__TR__ at 19-Nov-12 2:25am
   
You are welcome :)
member60 at 19-Nov-12 22:23pm
   
my 5!
__TR__ at 20-Nov-12 2:53am
   
Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try the below one
 
CONVERT(VARCHAR ,expiry date ,103)
 
syntax: convert(datatype ,column-name,style number)
 
ex:
 
in the above syntax you can specify your style number.
 
style numbers are valid from 1 to 9 , 100 to 110, 120 to 126
 
based on your expected output you can pass the stylish number.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 285
1 DamithSL 265
2 CPallini 195
3 Maciej Los 175
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100