Reputation: 11423
Why the following datatable contains datetime although the expected result from my SQL query is Date ?
public static DataTable CheckCalcDateToSend(int month, int year, int camp)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ss"].ToString()))
{
StringBuilder Query = new StringBuilder();
Query.Append(" SELECT CONVERT(date, to_date) AS to_date FROM CalcAttend ");
Query.Append(" WHERE month = @month AND year = @year");
Query.Append(" AND camp = @camp AND emp_num = 0 ORDER BY calc_date");
using (SqlCommand cmd = new SqlCommand(Query.ToString(), con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@month", SqlDbType.Int).Value = month;
cmd.Parameters.AddWithValue("@year", SqlDbType.Int).Value = year;
cmd.Parameters.AddWithValue("@camp", SqlDbType.Int).Value = camp;
con.Open();
using (var dataReader = cmd.ExecuteReader())
{
dt.Load(dataReader);
}
}
}
return dt;
}
Upvotes: 0
Views: 79
Reputation: 26
In .Net, "Date" type does not exist. Default is "DateTime" type. "Date" property in "DateTime" variable will give date component. Alternatively, You can try to use DataTextFormatString for dropdownlist binding.
Refer this post Date in dropdownlist
Upvotes: 1