代码如下:
protected void btnApplyOnClick(object sender, EventArgs e)
{
try
{
string addr = Configurations.GetServerAddrByID(ddlServer.SelectedValue.Trim());
SendServerAnnouncementTrans trans = new SendServerAnnouncementTrans();
trans.DtoIn.AnnouncementText = this.txtAnnouncementText.Value;
trans.DtoIn.LinkAddress = this.txtLinkAddress.Text;
if (trans.DtoIn.BeginTime != null)
{
trans.DtoIn.BeginTime = Convert.ToDateTime(this.txtBeginTime.Text);
} //这行走完就直接报错了
trans.DtoIn.DurationTime = Convert.ToInt32(this.txtDurationTime.Text);
trans.DtoIn.UserID = Convert.ToInt64(HttpContext.Current.Request.Cookies["HKUserId"].Value);
trans.Execute(addr);
}
catch (Exception ex)
{
throw ex;
问题补充:
string addr = Configurations.GetServerAddrByID(ddlServer.SelectedValue.Trim()); SendServerAnnouncementTrans trans = new SendServerAnnouncementTrans(); trans.DtoIn.AnnouncementText = this.txtAnnouncementText.Value;
trans.DtoIn.LinkAddress = this.txtLinkAddress.Text;
if (!string.IsNullOrEmpty(this.txtBeginTime.Text))
{
trans.DtoIn.BeginTime = Convert.ToDateTime(this.txtBeginTime.Text);
}
else
{
trans.DtoIn.BeginTime = DateTime.Now;
}
if (!string.IsNullOrEmpty(this.txtDurationTime.Text))
{
trans.DtoIn.DurationTime = Convert.ToInt32(this.txtDurationTime.Text);
}
else
{
trans.DtoIn.DurationTime = 0;
}
trans.DtoIn.UserID = Convert.ToInt64(HttpContext.Current.Request.Cookies"HKUserId"].Value);
trans.Execute(addr);
我又把代码重新修改了一下,这下没问题了,留着大家以后参考.也谢谢各位的回答了!
你排查下,几个类型转换是不是存在空。
当这几个类型为空时,页面会出现这样的错误提示
Server Error in '/' Application.
String was not recognized as a valid DateTime.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: String was not recognized as a valid DateTime.
Source Error:
Line 48: catch (Exception ex)
Line 49: {
Line 50: throw ex;
Line 51: }
Line 52: }
|
楼主的图挂了,
是不是你的字符串不符合时间格式的要求。
我在.aspx页面上的代码
<asp:TextBox ID="txtBeginTime" CssClass="calendar hasCalendar" onfocus="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" Width="150px" Text="" runat="server"></asp:TextBox>
使用My97DatePicker控件,时间都符合时间格式要求
@X*: 主要你还是要调试一下,看看后台调用日期转换时,看看传入的参数是什么?
用DateTime.Parse()试一下
我用DateTime.Parse试了,还是不行
@冷X: 那你就上一下图吧,我们也好知道是咋回事。。。用博客园的相册功能
图片传不上来了,我把错误内容提示贴出来吧
捕捉到FormatException
string was not recognized as a valid DateTime.
疑难解答提示:
确保方法参数的格式正确。
将一个字符串转换为DateTime时,先分析该字符串以获取日期,然后再将每个变量放置到DateTime对象中
获取此异常的常规帮助。
搜索更多联机帮助…
操作:
查看详细信息…
将异常详细信息复制到剪贴板
@冷X: 也就是说你这里不是时间你加上toString().trim()试试
@王凌志: 可是我使用的是My97DatePicker控件,时间符合格式要求啊
@X*: 输出一下,看看你这个时间是啥样的
@王凌志: 2012-08-07 11:47:25