C# 字符串在c中未被识别为有效的日期时间#
每当我将下拉列表留空时,就会出现此错误: “字符串未被识别为有效的日期时间” My.aspx如下所示:C# 字符串在c中未被识别为有效的日期时间#,c#,asp.net,C#,Asp.net,每当我将下拉列表留空时,就会出现此错误: “字符串未被识别为有效的日期时间” My.aspx如下所示: <asp:DropDownList ID="cboDay" runat="server" Width="55px" Height="32px" AppendDataBoundItems="true"> <asp:ListItem></asp:ListItem> </asp:DropDownList> <asp:DropDownLis
<asp:DropDownList ID="cboDay" runat="server" Width="55px" Height="32px" AppendDataBoundItems="true">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="cboMonth" runat="server" Width="80px" Height="30px" AppendDataBoundItems="true">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="cboYear" runat="server" Width="65px" Height="30px" AppendDataBoundItems="true">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
}
任何帮助都将不胜感激。提前谢谢
每当我将下拉列表留空时,就会出现此错误
然后不要将其留空,或更改代码以检测空输入
有几种方法可以做到这一点,但例如,您可以这样做:
protected void btnSave_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(cboDay.SelectedItem.Text)
|| String.IsNullOrEmpty(cboMonth.SelectedItem.Text)
|| String.IsNullOrEmpty(cboYear.SelectedItem.Text))
{
// TOOD: notify your UI that the values must be supplied
return;
}
String str = cboDay.SelectedItem.Text + "/" + cboMonth.SelectedItem.Text + "/" + cboYear.SelectedItem.Text;
DateTime dt = Convert.ToDateTime(str);
// ...
}
每当我将下拉列表留空时,就会出现此错误
然后不要将其留空,或更改代码以检测空输入
有几种方法可以做到这一点,但例如,您可以这样做:
protected void btnSave_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(cboDay.SelectedItem.Text)
|| String.IsNullOrEmpty(cboMonth.SelectedItem.Text)
|| String.IsNullOrEmpty(cboYear.SelectedItem.Text))
{
// TOOD: notify your UI that the values must be supplied
return;
}
String str = cboDay.SelectedItem.Text + "/" + cboMonth.SelectedItem.Text + "/" + cboYear.SelectedItem.Text;
DateTime dt = Convert.ToDateTime(str);
// ...
}
当字段留空时,是否应使用可为空的日期时间
DateTime? dt = null;
DateTime tmp;
if (DateTime.TryParse(
String.Format("{0}/{1}/{2}",
cboDay.SelectedItem.Text
, cboMonth.SelectedItem.Text
, cboYear.SelectedItem.Text
)
, out tmp
))
{
dt = tmp;
}
当字段留空时,是否应使用可为空的日期时间
DateTime? dt = null;
DateTime tmp;
if (DateTime.TryParse(
String.Format("{0}/{1}/{2}",
cboDay.SelectedItem.Text
, cboMonth.SelectedItem.Text
, cboYear.SelectedItem.Text
)
, out tmp
))
{
dt = tmp;
}
当你在使用不同的下拉列表时,试试这个
string date=cboDay.selectedtext+"/"+cbomonth.selectedtext+"/"+cboYear.selectedtext;
convert.todatetime(date);
但在尝试此方法之前,请确保您的日格式为01,02,…29,30,月格式为01,02,…11,12,所有格式均为两位数。否则,您将出错。因为您在日、月和年使用单独的下拉列表,请尝试此方法
string date=cboDay.selectedtext+"/"+cbomonth.selectedtext+"/"+cboYear.selectedtext;
convert.todatetime(date);
但在尝试此操作之前,请确保您的日格式为01,02,…29,30,月格式为01,02,…11,12,所有格式均为两位数。否则您将出错。问题字符串是什么?代码太多,且缺乏格式设置。你的问题不可读。
GetAge
方法也让我感到紧张-一旦你解决了最初的问题,关于这一点,值得再问一个问题。你在str
中的价值是什么?你目前的文化支持这一点吗format@Photon这完全取决于线程的当前区域性。有问题的字符串是什么?太多的代码和缺乏格式。你的问题不可读。GetAge
方法也让我感到紧张-一旦你解决了最初的问题,关于这一点,值得再问一个问题。你在str
中的价值是什么?你目前的文化支持这一点吗format@Photon这完全取决于线程的当前区域性。感谢您的回复。我需要在我的数据库中保存值,即使用户没有填写出生日期,所以我在出生日期上允许空值,这就是为什么我没有在我的ddl上设置不允许空白的验证。@user2388316然后看看if
的魔力。如果没有输入日期,请不要尝试解析它,也不要在查询中使用它。感谢您的回复。我需要在我的数据库中保存值,即使用户没有填写出生日期,所以我在出生日期上允许空值,这就是为什么我没有在我的ddl上设置不允许空白的验证。@user2388316然后看看if
的魔力。如果没有输入日期,不要尝试解析它,也不要在查询中使用它。