【发布时间】: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();
}
【问题讨论】:
-
调试并查看实际值。
-
运行时选择除日期以外的其他选项时出现错误字符串无法识别为有效日期时间
-
是的,结果是这样,但您应该调试并查看错误的原因和位置,没有人可以帮助您调试。
-
我将为 To 和 From 创建 2 个 DateTime 值,然后用这些文本框填充...应该有助于调试
-
@MadMychewhere 你所说的两个日期时间值是什么意思?在数据库中创建?
标签: c# asp.net datetime gridview