ejmtv
ejmtv

Reputation: 198

Insert birthdate to databse from day, month, year dropdown lists

In my form I have three separate dropdown lists for birthday: Day, Month, and Year. In my database, I have a column "birthday" with type date.

How will I convert those values from the dropdownlists in a specific date format to be accepted in the database?

Dropdown list values:

Day       Month         Year

1          Jan          1990
2          Feb          1991
3          Mar          1992

...and so on.

I tried this. It works but I know there's a better way:

DateTime bday = DateTime.Parse(String.Format("{0}/{1}/{2}", dropDay.SelectedValue, dropMonth.SelectedValue, dropYear.SelectedValue));

Upvotes: 1

Views: 996

Answers (2)

Krzyserious
Krzyserious

Reputation: 374

First you have to parse your Month string to int and than you should use DateTime and initialize it like this (as AitorFDK wrote) :

int month = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture ).Month
DateTime birthday = new DateTime(int year, int month, int day);

Upvotes: 0

AitorFDK
AitorFDK

Reputation: 101

You should use DateTime and initilize it like this:

DateTime birthday = new DateTime(int year, int month, int day);

Upvotes: 2

Related Questions