Jump to content

How to get date from database and display in textbox as Date/Month/Year format?(ASP/C#)

if anyone could tell me a way to get date from DB and convert it to date/month/year format in ASP.NET it would be very helpful. The data type of the date column in the db is date, so when i retrieve it and display it in a textbox i get the time as well.i found a code to convert in the internet but i get a error saying: String was not recognized as a valid DateTime.

 

This is my code

 

 cn.Open();
            string supdate = "SELECT Date FROM Payroll WHERE EmpID='" + empid + "'";
            SqlCommand query2 = new SqlCommand(subdate, cn);
            SqlDataReader reader2 = query2.ExecuteReader();

            reader2.Read();
            String sdate = reader2["Date"].ToString();
            reader2.Close();
            cn.Close();


            String conv = sdate; // get value from text field
            DateTime suDateTime = new DateTime();
            suDateTime = DateTime.ParseExact(conv, "MM-dd-yyyy", null);
            String converted_date = suDateTime.ToString("dd-MM-yyyy");
            TextBox4.Text = converted_date;

  

Link to comment
Share on other sites

Link to post
Share on other sites

I think you should just be able to cast from the reader and not have to go through a string conversion at all.

// Cast instead of using ToString
DateTime sdate = (DateTime) reader2["Date"];

// Then format it as desired. Example:
string formattedDate = sdate.ToString("dd-MM-yyyy");

 

Link to comment
Share on other sites

Link to post
Share on other sites

ToShortDateString() is probably what you're looking for unless I completely misunderstood what you're doing with it w/o reading the code.

 

I currently use it to print a date time as MM/dd/yyy on a generated pdf. 

 

Example:

vendor.dateEntered.ToShortDateString()

or if it's nullable:

vendor.approvedTimeStampSVP.Value.ToShortDateString()

 

Link to comment
Share on other sites

Link to post
Share on other sites

47 minutes ago, jslowik said:

ToShortDateString() is probably what you're looking for unless I completely misunderstood what you're doing with it w/o reading the code.

 

I currently use it to print a date time as MM/dd/yyy on a generated pdf. 

 

Example:


vendor.dateEntered.ToShortDateString()

or if it's nullable:


vendor.approvedTimeStampSVP.Value.ToShortDateString()

 

 
 

Note that ToShortDateString is culture specific so it won't format the same for everyone. For example, ToShortDateString uses a "yyyy-MM-dd" format for me.

DateTime.Now.ToString("yyyy-MM-dd") == DateTime.Now.ToShortDateString() // true for me

 

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, madknight3 said:

Note that ToShortDateString is culture specific

Excellent point. It didn't matter for my purposes, but I could see this being an issue in certain situations and definitely worth a mention.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×