sumit
sumit

Reputation:

problem with selecting date from dropdownlist

I am selecting the date from dropdownlist. but i am getting this exception "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM." Here is my code:

DateTime dateofjoining = new DateTime(DropDownListDay.SelectedIndex,
                                      DropDownListMonth.SelectedIndex,
                                      DropDownListYear.SelectedIndex);

In backend dateofjoining type as datetime. Pls modify my above code.

Thanks, Sumit

Upvotes: 0

Views: 783

Answers (3)

tbreffni
tbreffni

Reputation: 5130

Try using SelectedValue or SelectedText rather than SelectedIndex. SelectedIndex is just the position of the item in the list, not the value being displayed to the user.

Upvotes: 2

Matthew Groves
Matthew Groves

Reputation: 26149

I think you should use .SelectedValue instead of .SelectedIndex

Upvotes: 2

tvanfosson
tvanfosson

Reputation: 532565

You need to extract the value at the selected indices and convert them to integers before you pass them to the DateTime constructor. Also, the order of your fields is backwards in the constructor.

int day = int.Parse( DropDownListDay.SelectedValue );
int month = int.Parse( DropDownListMonth.SelectedValue );
int year = int.Parse( DropDownListYear.SelectedValue );

DateTime dateofjoining = new DateTime( year, month, day );

I suppose that using the indices could be made to work if they lined up with the day/month/year values correctly. Remember though that the indices are zero-based. I would use the selected values, though. That's what they are for.

Upvotes: 2

Related Questions