将 char 数据类型转换为 datetime 数据类型导致日期时间值超出范围错误

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value error

我将日期 "09/10/2014" 添加到文本框中并单击提交按钮但出现错误:-

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value error.

下面是我在调试时生成的查询:-

select * from WMS_BIN_STATUS_TRACK where 1!=1 or Current_Item_Exp_Dt = convert(datetime, '09/10/2014', 103)

下面是完整代码:-

protected void btnTrack_OnClick(Object sender, EventArgs e)
{
    string whereClause = "1!=1";

    if (ddlBin.SelectedValue != "0")
    {
        whereClause = whereClause + "or location_name='" + ddlBin.SelectedValue + "'";
    }
    if (ddlItem.SelectedValue != "0")
    {
        whereClause = whereClause + "or Current_Item_code='" + ddlItem.SelectedValue + "'";
    }
    if (txtBatch.Text != "")
    {
        whereClause = whereClause
            + " or Current_Item_Batch " + (ddlmathsign.SelectedValue == "Equal" ? (" = '" + txtBatch.Text + "'") : (" like '%" + txtBatch.Text + "%'"));
    }
    if (txtExpCal.Value != "")
    {
        whereClause = whereClause + "or Current_Item_Exp_Dt " + (ddlAssignvalue.SelectedValue == "Greater than" ? ">" : (ddlAssignvalue.SelectedValue == "Less than" ? "<" :
                  (ddlAssignvalue.SelectedValue == "Equal to" ? "=" : (ddlAssignvalue.SelectedValue == "Greater than equal to" ? ">=" : "<=")))) + "convert(datetime, '" + txtExpCal.Value + "', 103)";
    }

    if (ddlBin.SelectedValue == "0" && ddlItem.SelectedValue == "0" && txtBatch.Text == "" && txtExpCal.Value == "")
    {
        BindGrid();
    }

    else
    {
        string query = "select * from WMS_BIN_STATUS_TRACK where " + whereClause;

        SqlDataAdapter da = new SqlDataAdapter(query, strConnString);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GrdBinStockTracker.DataSource = dt;
        GrdBinStockTracker.DataBind();
    }
}

注意 由于我只是在本地条件下工作以进行测试,SQL 注射在这里不是问题 .

此外,这是与不同日期时间有关的问题吗? ?

问题出在 Current_Item_Exp_Dt 列中的数据。

这是一种重现它的方法。

create table #tt(dd char(10))
insert into #tt values('13/13/2014')

select 1 from #tt where dd = convert(datetime, '09/10/2014', 103)

您可以使用此查询来识别日期格式不正确的记录。

SELECT Current_Item_Exp_Dt FROM WMS_BIN_STATUS_TRACK WHERE ISDATE(Current_Item_Exp_Dt)=0

这是可能的解决方法。将您的 where 条件更改为..

WHERE (CASE WHEN ISDATE(Current_Item_Exp_Dt)=1 
               THEN Current_Item_Exp_Dt 
               ELSE NULL END) = CONVERT(DATETIME, '09/10/2014', 103)

您的最终查询应该是

SELECT Current_Item_Exp_Dt 
FROM WMS_BIN_STATUS_TRACK 
WHERE 1!=1 or (CASE WHEN ISDATE(Current_Item_Exp_Dt)=1 
               THEN Current_Item_Exp_Dt 
               ELSE NULL END) = CONVERT(DATETIME, '09/10/2014', 103)

因为您的列是 varchar 并且无法更改为 datetime 您还需要将该列转换为条件的日期时间。

请检查以下查询

    whereClause = whereClause + "or convert(datetime,Current_Item_Exp_Dt,103) " 
    + (ddlAssignvalue.SelectedValue == "Greater than" ? ">" : 
     (ddlAssignvalue.SelectedValue == "Less than" ? "<" : 
    (ddlAssignvalue.SelectedValue == "Equal to" ? "=" : 
    (ddlAssignvalue.SelectedValue == "Greater than equal to" ? ">=" : "<=")))) + 
"convert(datetime, '" + txtExpCal.Value + "', 103)";

If you are not sure Current_Item_Exp_Dt always contain a valid date you can use below query build as per DarkNight

   whereClause = whereClause + "or (CASE WHEN ISDATE(Current_Item_Exp_Dt)=1 
           THEN convert(datetime,Current_Item_Exp_Dt,103)  ELSE NULL END) " 
    + (ddlAssignvalue.SelectedValue == "Greater than" ? ">" : 
     (ddlAssignvalue.SelectedValue == "Less than" ? "<" : 
    (ddlAssignvalue.SelectedValue == "Equal to" ? "=" : 
    (ddlAssignvalue.SelectedValue == "Greater than equal to" ? ">=" : "<=")))) + 
"convert(datetime, '" + txtExpCal.Value + "', 103)";