views:

91

answers:

3

Hi all, 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

+2  A: 

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.

tvanfosson
I have written this code this is also not working:DateTime dateofjoining = new DateTime(int.Parse(DropDownListDay.SelectedItem.ToString()), int.Parse(DropDownListMonth.SelectedItem.ToString()), int.Parse(DropDownListYear.SelectedItem.ToString()))
I have no idea what the ToString method on SelectedItem does, but I doubt it if does what you want. If you can't get directly at SelectedValue (and I'm not sure why you wouldn't be able to), you could use SelectedItem.Value.
tvanfosson
Don't neglect the fact that you have the order of the values reversed in the constructor. It needs to be "year, month, day", not "day, month year".
tvanfosson
Hi Mr. tvanfosson your code is working fine. But incase of Month dropdown list i have given January to December not 1 to 12. Then how can i do this?? Its working only with int but not with string like January to December.
Each ListItem in the DropDownList has two properties: Text and Value. Set the Text as the month name, but the value as the integer value (as a string). You don't show the code where you create the dropdown list, but if you use the Item.Add() method that takes a ListItem you should be able to do ddl.Items.Add( new ListItem( "January", 1 ) )
tvanfosson
+2  A: 

I think you should use .SelectedValue instead of .SelectedIndex

mgroves
+2  A: 

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.

tbreffni
In SQLServer 2000 Selected text property is not available.
Try modifying the above code to read: DateTime dateofjoining = new DateTime(DropDownListDay.SelectedValue, DropDownListMonth.SelectedValue, DropDownListYear.SelectedValue); . You could also try: DateTime dateofjoining = new DateTime(DropDownListDay.SelectedText, DropDownListMonth.SelectedText, DropDownListYear.SelectedText);
tbreffni