Vignesh Kumar

Vignesh Kumar

  • NA
  • 1k
  • 416.4k

Conversion failed when converting date and/or time from char

Apr 9 2014 2:56 AM

Hi,
 
I am using this query to convert datetime column to date field and I get an error. When I try to select --Select-- in my dropdown I get this error.

     **Error:Conversion failed when converting date and/or time from character string**

**Code Behind:**

    protected void ddlDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDate.SelectedItem.Text != "--Select--")
        {
            strDate = dddlDate.SelectedValue;
            DataTable dtDate = DataRepository.GetDate(strDate);
            gvDetails.DataSource = dtDate;
            gvDetails.DataBind();                       
        }
        else
        {
            Response.Redirect("Details.aspx");
        }
    }

**I am using this in my DAL Layer**

     public static DataTable GetDate(string strDate)
        {
            DataTable dt = new DataTable();
            string strcon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(strcon))
            {
                conn.Open();
                string strQuery = "select ID,Application_Name,Group,CONVERT(Date, Summary_Date) as Summary_Date,Comments from Application where                Summary_Date <= '" + strDate + "'";
                SqlCommand cmd = new SqlCommand(strQuery, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
            }
            return dt;
        }


Answers (2)