【发布时间】:2016-03-14 06:41:00
【问题描述】:
我将日期作为"09/10/2014" 添加到文本框中并单击提交按钮,但出现错误:-
将 char 数据类型转换为 datetime 数据类型导致了超出范围的 datetime 值错误。
以下是我在调试时生成的查询:-
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 注入在这里不是问题。
另外,这是与不同日期时间有关的问题吗? ?
【问题讨论】:
-
sqlserver?或任何其他数据库?
-
尝试将查询中的
Current_Item_Exp_Dt也转换为103格式。 -
确保
Current_Item_Exp_Dt列中有正确的数据。 -
@Sachu:是的,它是
sql-server-2005。对不起,我忘了提这个 -
试试这个
convert(date,Current_Item_Exp_Dt,103) = convert(date, '09/10/2014', 103)
标签: c# sql asp.net datetime sql-server-2005