简体   繁体   English

错误:字符串未被识别为有效的DateTime。 我正在使用带有asp.net和C#Web窗体和SQL数据库的jQuery Datepicker

[英]Error: String was not recognized as a valid DateTime. I'm using jquery datepicker with asp.net and c# web forms and sql databse

I have a text box in which i select the date from the datepicker. 我有一个文本框,其中我从日期选择器中选择日期。 I then need to validate: 1. A date is selected 2. date selected is not a future date. 然后,我需要验证:1.选择了一个日期2.选择的日期不是将来的日期。 only todays date or any previous date. 仅今天或以前的日期。 3. Regular hrs add over time hours on that date do not exceed 20-24 hrs. 3.当天的固定小时数不超过20-24小时。

I've an error "Error: String was not recognized as a valid DateTime." 我有一个错误“错误:字符串未被识别为有效的DateTime。” Can I please get some help over this as I have spent days over this. 我已经花了很多天了,对此可以帮忙吗?

C# code: C#代码:

protected void btnCreate_click(object sender, EventArgs e)
    {
        try
        {
            DateTime dt = DateTime.ParseExact(DateTxt.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces);

            if (DateTxt.Text != "" && PName_dd.Text != "" && CCG_dd.Text != "" && CCL_dd.Text != "")
            {

                cmd.CommandText = "INSERT INTO TSHistory(EmpId_Int, ProjectCode_Int, TSDate_dt, CCode_Int, TSRhours_nu, TSOhours_nu, JobDescp_vc) VALUES(@GEId, @Pcode, @date, @CCode, @Rhr, @Ohr, @job)";
                cmd.Parameters.AddWithValue("@GEId", EmpID.Text);

                cmd.Parameters.AddWithValue("@PCode", PCode_txt.Text);
                cmd.Parameters.AddWithValue("@date", dt);
                cmd.Parameters.AddWithValue("@CCode", CCL_dd.SelectedValue);
                cmd.Parameters.AddWithValue("@Rhr", R_txt.Text);
                cmd.Parameters.AddWithValue("@Ohr", O_txt.Text);
                cmd.Parameters.AddWithValue("@job", JobDesc_txt.Text);
                cmd.Parameters.AddWithValue("@Emp", EmpID.Text);
                cmd.Connection = Cnn;
                Cnn.Open();
                cmd.ExecuteNonQuery();
                Cnn.Close();
                updateUserDetails();
                // displayTS();
                RefreshSrc();
                RefreshDatabase();


            }

            else
            {


                //  DateTime dt = Convert.ToDateTime();
                //   var total_hrs = 24;
                //   DateTime dt = DateTime.ParseExact(DateTxt.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces);
                //   global.Text = "Done";

                if (DateTxt.Text == "")

                {
                    // Response.Write("<script>alert('PLease select date');</script>");
                    //  dateError.Text = "PLease select date";
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Select date')", true);



                }


                else if (DateTxt.Text != "" && dt > DateTime.Today)

                {
                    // Response.Write("<script>alert('Invalid Date');</script>");
                    //  dateError.Text = "Invalid date";
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Invalid date')", true);


                }



            }




        }

       catch (Exception ex)
        {
            Response.Write("Error: " + ex.Message.ToString());
        }
    }

Aspx code: Aspx代码:

 <div class="form-group col-sm-1">
   <asp:TextBox ID="DateTxt" placeholder="Date" CssClass="form-control" runat="server" 
                ReadOnly="True">
   </asp:TextBox>

Make ReadOnly= "False" 使ReadOnly =“ False”

The contents of the TextBox control cannot be changed if ReadOnly="True".And that's why DateTxt text values remains empty("") and it is throwing an Error: String was not recognized as a valid DateTime. 如果ReadOnly =“ True”,则不能更改TextBox控件的内容。这就是为什么DateTxt文本值保持为空(“”)并引发错误:字符串未被识别为有效的DateTime的原因。

由于您的文本仅包含日期部分,因此请按如下所示更改代码,

DateTime dt = DateTime.ParseExact(DateTxt.Text, "MM/dd/yyyy",CultureInfo.InvariantCulture);

I've changed the code a little bit. 我对代码做了一些更改。 Please check. 请检查。

DateTime dt = DateTime.ParseExact(DateTxt.Text.Date, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误“字符串未被识别为有效的日期时间。” 在 c# - error “String was not recognized as a valid DateTime.” in c# 字符串未被识别为有效日期时间asp.net C# - string was not recognized as a valid datetime asp.net c# 给定System.FormatException:字符串未被识别为有效的DateTime。 在C#中使用datetime.ParseExact - Giving System.FormatException: String was not recognized as a valid DateTime. using datetime.ParseExact in C# 错误消息为“字符串未被识别为有效的DateTime。” - Error Message as “String was not recognized as a valid DateTime.” 错误:字符串未被识别为有效的DateTime。 - Error: String was not recognized as a valid DateTime. 获取错误“字符串未被识别为有效的DateTime”。 - Getting error “String was not recognized as a valid DateTime.” 将字符串解析为DateType ASP.NET C#异常-无法将该字符串识别为有效的DateTime - Parse string to DateType ASP.NET C# exception - The string was not recognized as a valid DateTime 发布后出错:字符串未被识别为有效的日期时间 asp.net - Error after publishing : String was not recognized as a valid DateTime asp.net “在Asp.net中,字符串未被识别为有效的DateTime - "String was not recognized as a valid DateTime in Asp.net 字符串未被识别为有效的日期时间,asp.net - String was not recognized as a valid DateTime, asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM