mardi 8 décembre 2015

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

I am adding a date as "09/10/2014" into the textbox and clicking the submit button but getting error as:-

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

Below is my query generated while debugging:-

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

and below is the full code:-

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();
    }
}

NOTE As I am working on local condition only to test, SQL Injection is not a matter of concern here.

Also, Is this a issue related to different datetime. ?

Aucun commentaire:

Enregistrer un commentaire