Home > Software design >  How to display date from database table to a date textfield
How to display date from database table to a date textfield

Time:09-14

I have a database table with a column Birthdate. I want to display the date from the db table to a 'date' textfield on my webform.

For example, the data in my database table Birthdate column is 'Sep 18, 2000' and I want to display it on the 'date' textfield with a format of dd/mm/yyyy.

This is what I have did but it does not display anything on the 'date' textfield

Code in Webform1.aspx.cs

string date = DataAccess.BrgyCitizenBirthdate;
DateTime getBDate = Convert.ToDateTime(DateTime.ParseExact(date, "MMM dd, yyyy", null));
BCBirthdate = getBDate.ToString("dd/mm/yyyy");

Note that the value of DataAccess.BrgyCitizenBirthdate; is Sep 18, 2000.

Code in Webform2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
   txtBirthdate.Text = Brgy_Citizens.BCBirthdate;
}

My output

enter image description here

My DESIRED output

enter image description here

CodePudding user response:

Use this:

txtBirthdate.Text = Brgy_Citizens.BCBirthdate.ToShortDateString();

You don't really want to use fixed formatting, since that may well change.

But, as a FYI?

this would work (it is MM, and not mm)

getBDate.ToString("dd/MM/yyyy");

Well, you kind have to make a choice here.

that date setting is going to be the result of regional settings on the client side.

And you can well see the default settings - just look at the default date setting when you drop in that text box textmode = date.

So, for the simple answer?

Shove into that text box a date formatted to what the text box is expecting.

I thus suggest using ToShortDateString, and it should work if the date settings are different or changed on the web server.

  • Related