为什么我的代码会抛出错误
Cannot perform '=' operation on System.DateTime and System.Int32.
,
此行引发错误:
DataRow[] dr = ds.Tables[0].Select("HolidayDate=" + SelectedDate);
但是当我传递静态日期如“#2014-10-12#”然后它可以工作时,我还附加#with selectedDate但程序没有给出响应。
完整代码:
protected void Calndar_SelectionChanged(object sender, EventArgs e)
{
string SelectedDate = Calndar.SelectedDate.ToString(DateTime.Now.ToString("yyyy") + "-" + "MM-dd");
DataSet ds = (DataSet)Session["Calendar"];
DataRow[] dr = ds.Tables[0].Select("HolidayDate=" + SelectedDate);
foreach (DataRow drr in dr)
{
Response.Write(SelectedDate);
}
string SelectedDate存储值,并在response.write中显示
更新的代码也不起作用
protected void Calndar_SelectionChanged(object sender, EventArgs e)
{
DateTime SelectedDate =new DateTime(DateTime.Now.Year, Calndar.SelectedDate.Month, Calndar.SelectedDate.Day);
DataSet ds = (DataSet)Session["Calendar"];
IEnumerable<DataRow> selectedRows = ds.Tables[0].AsEnumerable().Where(row => (row.Field<DateTime>("HolidayDate") == SelectedDate));
foreach (DataRow drr in selectedRows)
{
Response.Write(drr[4]);
}
}
答案 0 :(得分:0)
如果您想避免此类日期序列化问题,可以使用Linq to DataSet:
DateTime selectedDate =
new DateTime(DateTime.Now.Year, Calndar.SelectedDate.Month, Calndar.SelectedDate.Day);
DataSet ds = (DataSet)Session["Calendar"];
IEnumerable<DataRow> selectedRows = ds.Tables[0].AsEnumerable().
Where(row => (row.Field<DateTime>("HolidayDate") == selectedDate));
foreach (DataRow selectedRow in selectedRows)
...