我在添加语句BETWEEN以在两个日期之间进行选择并显示数据后发生此问题。在此之前,它只适用于我只选择一个日期。当我想选择日期以外的其他值时会发生此问题。它显示字符串不被识别为有效的日期时间。但如果我只选择日期或日期的值,它不会出现任何错误。为什么会发生?它只适用于我有一个日期选择,但错误,当我在日期之间添加声明。这是我的代码到目前为止。在选择日期asp.net c中添加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 [email protected]_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 [email protected]_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 p[email protected]_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();
}
调试并查看实际值。 –
当运行时选择除日期之外的其他选项时,它显示错误字符串未被识别为有效的日期时间 – NFH
yes是结果,但您应该调试并查看错误的原因和位置,并且没有人可以帮助您进行调试。 –