C# 字符串未被识别为有效的日期时间

C# 字符串未被识别为有效的日期时间,c#,asp.net,ajax,datetime,asp.net-ajax,C#,Asp.net,Ajax,Datetime,Asp.net Ajax,在我的asp.net c#应用程序中,我使用ajax calender extender收集日期时间信息。当我尝试提交此页面时,它显示字符串未被识别为有效的日期时间。我尝试了不同的转换方法,但都不适用于我 string date =Request.Form[ txtBookDate.UniqueID]; DateTime yeer = Convert.ToDateTime(date); 字符串未被识别为有效的日期时间 描述:在执行过程中发生未处理的异常 当前的web请

在我的asp.net c#应用程序中,我使用ajax calender extender收集日期时间信息。当我尝试提交此页面时,它显示字符串未被识别为有效的日期时间。我尝试了不同的转换方法,但都不适用于我

string date =Request.Form[ txtBookDate.UniqueID];

            DateTime yeer = Convert.ToDateTime(date);
字符串未被识别为有效的日期时间

描述:在执行过程中发生未处理的异常 当前的web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息及其在代码中的来源

异常详细信息:System.FormatException:无法识别字符串 作为有效的日期时间

源错误:

第78行:字符串日期=Request.Form[txtBookDate.UniqueID]; 第79行:第80行:日期时间yeer= 转换.ToDateTime(日期);第81行:日期时间Bookdate= Convert.ToDateTime(date.ToString());第82行:国际年 =记账日期。年份

源文件:D:\Sample Project\AuditoriumNew\AuditoriumNew\Registration.aspx.cs行:80

堆栈跟踪:

[格式异常:字符串未被识别为有效的日期时间。]
System.DateTimeParse.Parse(字符串s、DateTimeFormatInfo dtfi、, DateTimeStyles样式)+10404058 System.Convert.ToDateTime(字符串 值)+83 AuditoriumNew.Index.txtRent\u TextChanged(对象发送方, D:\Sample中的事件参数e) Project\AuditoriumNew\AuditoriumNew\Registration.aspx.cs:80
System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e)+116
System.Web.UI.WebControls.TextBox.RaisePostDataChangedEvent()+50
System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +10 System.Web.UI.Page.RaiseChangedEvents()+132 System.Web.UI.Page.ProcessRequestMain(布尔值 IncludeStages前同步点,布尔值IncludeStages后同步点) +1644年

版本信息:Microsoft.NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.17929

ajax Extender的源代码

<asp:Label ID="bok" Text="Booking Date" runat="server"></asp:Label>
                    <asp:TextBox ID="txtBookDate" runat="server" ReadOnly="true"></asp:TextBox>
                    <asp:Image ID="calender" runat="server" ImageUrl="img/cals/calender.png" />
                    <asp:CalendarExtender ID="BookingDate" TargetControlID="txtBookDate" PopupButtonID="calender"
                        runat="server" PopupPosition="TopRight" DaysModeTitleFormat="dd/MM/yyyy" 
                        TodaysDateFormat="dd/MM/yyyy">
                    </asp:CalendarExtender>

实际代码

 protected void txtRent_TextChanged(object sender, EventArgs e)
        {
            float CentralAmnt;
            float renter;

            double rent = Convert.ToDouble(txtRent.Text);
            renter = float.Parse(rent.ToString());


            ///Calculate CTValue
            double CTValue = Convert.ToDouble(12.36 / 100);
            double CTax = CTValue;
            float CentralTax = float.Parse(CTax.ToString());

            CentralAmnt = renter * CentralTax;


            ///Calculate LT Value

            double LTax = 0.15;
            float LuxTax = float.Parse(LTax.ToString());
            float LuxAmnt = renter * LuxTax;
            string s = txtBookDate.Text;
            DateTime yeer;

             yeer = Convert.ToDateTime(s);

            //DateTime yeer = Convert.ToDateTime(s);
            DateTime Bookdate = Convert.ToDateTime(yeer.ToString());
            int WedYear = Bookdate.Year;
            if (WedYear < 2014)
            {
                float TotAmnt = renter + LuxAmnt;

                txtTotal.Text = TotAmnt.ToString();
                LuxuaryTaxAmnt = LuxAmnt;


            }
            else
            {
                float TotAmnt = renter + CentralAmnt + LuxAmnt;

                txtTotal.Text = TotAmnt.ToString();
                LuxuaryTaxAmnt = LuxAmnt;
                CentralTaxAmnt = CentralAmnt;
            }
        }
受保护的void txtRent\u text已更改(对象发送方,事件参数e)
{
浮动中心;
浮动租金;
double rent=Convert.ToDouble(txtRent.Text);
renter=float.Parse(rent.ToString());
///计算CTV值
双CTValue=转换为双CTValue(12.36/100);
双CTax=CTX值;
float-CentralTax=float.Parse(CTax.ToString());
CentralAmnt=承租人*CentralTax;
///计算LT值
双LTax=0.15;
float-LuxTax=float.Parse(LTax.ToString());
浮动LuxAmnt=承租人*Luxax税;
字符串s=txtBookDate.Text;
DateTime yeer;
yeer=转换到当前时间(s);
//DateTime yeer=Convert.ToDateTime(s);
DateTime Bookdate=Convert.ToDateTime(yeer.ToString());
int WedYear=记账日期。年份;
如果(2014年之后的星期三)
{
浮动总金额=承租人+卢森堡总金额;
txtTotal.Text=TotAmnt.ToString();
LuxuaryTaxAmnt=LuxAmnt;
}
其他的
{
浮动总金额=承租人+中央银行+卢森堡银行;
txtTotal.Text=TotAmnt.ToString();
LuxuaryTaxAmnt=LuxAmnt;
CentralTaxAmnt=CentralAmnt;
}
}

如果txtBookDate为空,并且您决定在txtRent中输入一个值,那么您将继续收到“字符串未被识别为有效的日期时间”错误,因为txtBookDate为空=NULL。。。或者,如果确实插入了日期,请确保其格式合理。

所以问题是:在为txtRent输入值之前,是否在txtBookDate中输入值

您的代码在txtRent\u textchange期间运行有什么原因吗?这是唯一的办法吗

请尝试以下操作,至少在这里我确保yeer具有“默认”值,这样您就不会收到“字符串未被识别为有效的日期时间”。在这种情况下:

DateTime yeer;

if (!string.IsNullOrEmpty(txtBookDate.Text))
    yeer = Convert.ToDateTime(txtBookDate.Text);
else
    yeer = new DateTime(9999, 12, 31);

放一个断点,看看你在
date
字符串中得到了什么值。不@Habib我想我们应该自己预测这个值:-)你能提供你的ASPX页面代码吗?通过20个问题进行调试。为什么人们很难帮助你?在问题中包括所有相关信息。就目前而言,你的问题甚至不是一个问题。这只是一个悲伤故事的叙述。确保1:提供再现问题的完整示例(即
DateTime.Parse(“YerExactTextHere”)
),2:在发布您的问题之前阅读一些类似的问题。