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;
}
saurabh mittalPosted Apr 9, 2014, 5:44 AM
you can chaeck these link for this
http://www.w3schools.com/sql/func_convert.asp
http://stackoverflow.com/questions/5222075/sql-datetime-format-to-date-only
http://sqlhints.com/2013/07/14/how-to-get-date-part-only-from-datetime-in-sql-server/
Abhay ShankerPosted Apr 9, 2014, 3:26 AM
1. Change data type of Summary_Date as Datetime
2. Properly Convert string to datetime in Where condition