从下拉列表中选择日期的问题

时间:2009-07-16 12:41:46

标签: c# sql-server date

我从下拉列表中选择日期。但我得到这个例外“SqlDateTime溢出。必须在1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间。” 这是我的代码:

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

在后端dateofjoining类型中作为datetime。请修改我的上述代码。

谢谢, 萨米特

3 个答案:

答案 0 :(得分:2)

您需要在选定索引处提取值并将它们转换为整数,然后再将它们传递给DateTime构造函数。此外,字段的顺序在构造函数中是向后的。

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 );

我认为如果他们正确排列日/月/年值,可以使用索引。请记住,指数是从零开始的。不过,我会使用所选的值。这就是它们的用途。

答案 1 :(得分:2)

我认为你应该使用.SelectedValue而不是.SelectedIndex

答案 2 :(得分:2)

尝试使用SelectedValue或SelectedText而不是SelectedIndex。 SelectedIndex只是列表中项目的位置,而不是显示给用户的值。