【问题标题】:String was not recognized as valid datetime after add BETWEEN statement in choosing date asp.net c#在选择日期asp.net c#中添加BETWEEN语句后,字符串未被识别为有效日期时间
【发布时间】:2017-12-06 04:57:31
【问题描述】:

我在添加语句 BETWEEN 以在两个日期之间进行选择并显示数据后出现此问题。在此之前,当我只选择一个日期时它会起作用。当我想选择日期以外的其他值时会出现此问题。似乎字符串未被识别为有效的日期时间。但如果我只选择日期或日期的值,它不会出现任何错误。为什么会这样?当我只有一个日期可供选择但当我在日期中的语句之间添加错误时,它可以完美运行。到目前为止,这是我的代码。

 if (!string.IsNullOrEmpty(DropDownListSearchJO.SelectedValue) || (!string.IsNullOrEmpty(DropDownListSearchLine.SelectedValue)) || (!string.IsNullOrEmpty(TextBoxSearchRec.Text)) || (!string.IsNullOrEmpty(TextBoxSearchRec2.Text)) || (!string.IsNullOrEmpty(DropDownListProcess.SelectedValue)))
       {

           try
           {
               con.Open();

               //select data from CutPanelCard
               query = "select a.req_id, a.prod_line, a.jo_no, a.buyer, a.request_date,CONVERT(VARCHAR(10),a.need_by_date ,101) as need_by_date, a.qty, a.username, b.process_id from CutPanelCard a LEFT JOIN CutPanelConfirmation b on b.req_id = a.req_id ";

               //check if dropdownJO not empty so query the data
               if (!string.IsNullOrEmpty(DropDownListSearchJO.SelectedValue))
               {
                   query += "where jo_no=@jo_no and status='F' ";
                   cmd = new SqlCommand(query, con);
                   cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue);
                   checking = true;

               }

               //check if dropdownLine not empty so query the data
               if (!string.IsNullOrEmpty(DropDownListSearchLine.SelectedValue))
               {
                   if (checking == true)
                   {
                       query += "and prod_line=@prod_line and status='F' ";
                       cmd = new SqlCommand(query, con);
                       cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue);
                       cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue);
                       cmd.Parameters.AddWithValue("@from", Convert.ToDateTime(TextBoxSearchRec.Text));
                       cmd.Parameters.AddWithValue("@to", Convert.ToDateTime(TextBoxSearchRec2.Text));
                   }
                   else
                   {
                       query += "where prod_line=@prod_line and status='F' ";
                       cmd = new SqlCommand(query, con);
                       cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue);
                       cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue);
                       checking = true;
                   }
               }

              //check if dropdownProcess not empty so query the data
               if (!string.IsNullOrEmpty(DropDownListProcess.SelectedValue))
               {
                       if (checking == true)
                       {
                           query += "and a.req_id not in (select req_id from cutpanelconfirmation where process_id = @process_id) and status='F' ";
                           cmd = new SqlCommand(query, con);
                           cmd.Parameters.AddWithValue("@processs_id", DropDownListProcess.SelectedValue);
                           cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue);
                           cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue);
                       }
                       else
                       {
                           query += "where a.req_id not in (select req_id from cutpanelconfirmation where process_id = @process_id) and status='F' ";
                           cmd = new SqlCommand(query, con);
                           cmd.Parameters.AddWithValue("@process_id", DropDownListProcess.SelectedValue);
                           cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue);
                           cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue);
                           checking = true;
                       }
               }

               //check if textboxRec not empty so query the data
               if ((!string.IsNullOrEmpty(TextBoxSearchRec.Text)) || (!string.IsNullOrEmpty(TextBoxSearchRec2.Text)))
               {
                   if (checking == true)
                   {
                       query += "and need_by_date BETWEEN @from and @to and status='F' ";
                       cmd = new SqlCommand(query, con);
                       cmd.Parameters.AddWithValue("@from", Convert.ToDateTime(TextBoxSearchRec.Text));
                       cmd.Parameters.AddWithValue("@to", Convert.ToDateTime(TextBoxSearchRec2.Text));
                       cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue);
                       cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue);
                       cmd.Parameters.AddWithValue("@process_id", DropDownListProcess.SelectedValue);
                   }
                   else
                   {
                       query += "where need_by_date BETWEEN @from and @to and status='F' ";
                       cmd = new SqlCommand(query, con);
                       cmd.Parameters.AddWithValue("@need_by_date", Convert.ToDateTime(TextBoxSearchRec.Text));
                       cmd.Parameters.AddWithValue("@to", Convert.ToDateTime(TextBoxSearchRec2.Text));
                       cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue);
                       cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue);
                       cmd.Parameters.AddWithValue("@process_id", DropDownListProcess.SelectedValue);
                       checking = true;
                   }
               }


               //show the query based on ascending req_id and last_update
               query += "order by a.req_id, b.last_update DESC ";
               cmd = new SqlCommand(query, con);
               cmd.Parameters.AddWithValue("@from", Convert.ToDateTime(TextBoxSearchRec.Text));
               cmd.Parameters.AddWithValue("@to", Convert.ToDateTime(TextBoxSearchRec2.Text));
               cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue);
               cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue);
               cmd.Parameters.AddWithValue("@process_id", DropDownListProcess.SelectedValue);

               SqlDataAdapter da = new SqlDataAdapter(cmd);

               //remove duplicated req_id before bind into gridview
               DataTable dtRemoveDuplicate = new DataTable();
               da.Fill(dtRemoveDuplicate);
               dtRemoveDuplicate = DeleteDuplicateFromDataTable(dtRemoveDuplicate, "req_id");
               GridView1.DataSource = dtRemoveDuplicate;
               GridView1.DataBind();

               con.Close();
           }

【问题讨论】:

  • 调试并查看实际值。
  • 运行时选择除日期以外的其他选项时出现错误字符串无法识别为有效日期时间
  • 是的,结果是这样,但您应该调试并查看错误的原因和位置,没有人可以帮助您调试。
  • 我将为 ToFrom 创建 2 个 DateTime 值,然后用这些文本框填充...应该有助于调试
  • @MadMychewhere 你所说的两个日期时间值是什么意思?在数据库中创建?

标签: c# asp.net datetime gridview


【解决方案1】:

试试这个。

query += "and a.need_by_date BETWEEN @from and @to and status='F' ";
query += "where a.need_by_date BETWEEN @from and @to and status='F' ";

【讨论】:

  • 使用 datePicker 控件代替文本框
  • 例如?对不起,因为我直到这个 c# 和 Visual Studio 都是新的
  • 试试这个。"Select data from tablename where date>=startdate and date
  • 仍然无法正常工作。当我之前只有一个文本框日期时,这项工作。但是当我在查询日期之间添加它不起作用:(我只是添加另一个文本框和语句之间:(
  • 如果您使用文本框获取日期,可能格式错误。字符串日期 = @“20/07/2017”; DateTime 日期 = Convert.ToDateTime(dateString);以下代码抛出 FormatException: String was not Recognized as a valid DateTime 在此代码中,dateS 以日/月/年格式表示日期。默认情况下,.NET 使用 en-US 文化。因此,当使用 toDateTime 方法时,它会抛出错误,因为 20 超出了月份范围。字符串 dateString = @"20/05/2012";尝试这个。 Convert.ToDateTime(dateString, System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
猜你喜欢
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多