我有一个页面,用户可以在其中输入“从日期”和“到日期”并根据日期间隔获取一组值。唯一的问题是它不适用于“迄今为止”。当我输入这样的内容时:from: 2012-01-01 | to: 2013-07-26
它工作正常。但是当我尝试这样的事情时:from: 2010-07-04 | to: 2012-01-01
我仍然会在那个日期之后得到所有结果(比如从 2013 年开始)。
我已经使用上面的输入值(2010/07/04-2012/01/01)运行了调试。这是我的代码以及调试中的相关值:
ASPX
<asp:TextBox ID="VolumeSearchFromDate" runat="server" CssClass="dateTextBox" />
<br />
<asp:TextBox ID="VolumeSearchToDate" runat="server" CssClass="dateTextBox" />
<br />
<asp:Button ID="Button2" runat="server" Text="Search" onclick="btnVolumeSearch_Click" ValidationGroup="validate" />
<br />
<asp:GridView ID="myGv" runat="server"
ShowFooter="True"
AutoGenerateColumns="False" AllowSorting="True" DataKeyNames="id" DataSourceID="myObjectDataSource" >
<Columns>
//my columns here
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="myObjectDataSource" runat="server"
DeleteMethod="myDeleteMethod" SelectMethod="mySelectMethod"
TypeName="whereItsAt.sqlDataLayer" UpdateMethod="myUpdateMethod">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:Parameter Name="fromDate" Type="DateTime"/>
<asp:Parameter Name="toDate" Type="DateTime"/>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="volume" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
背后的代码 (c#)
//dont know why this is here. The person who wrote the code
//in the first place wrote this and I haven't removed it.
protected void btnVolumeSearch_Click(object sender, EventArgs e)
{
gvVolumeListBindData();
}
private void gvVolumeListBindData()
{
myObjectDataSource.SelectParameters.Remove(myObjectDataSource.SelectParameters["fromDate"]);
string debugString /* "" */= VolumeSearchFromDate.Text.ToString(); /* 2010-04-07 */
string debugString2 /* 2012-01-01 */ = debugString /* "" */;
myObjectDataSource.SelectParameters.Add("fromDate", VolumeSearchFromDate.Text.ToString());
myObjectDataSource.SelectParameters.Remove(myObjectDataSource.SelectParameters["toDate"]);
myObjectDataSource.SelectParameters.Add("toDate", VolumeSearchToDate.Text.ToString());
debugString /* "" */ = VolumeSearchToDate.Text.ToString(); /* 2012-01-01
debugString2 /* 2012-01-01 */ = debugString; /* "" */
gvVolumeList.DataBind();
}
检索数据的方法(也是 C#)
public static DataTable mySelectMethod(DateTime fromDate /* 2010-07-04 00:00:00 */, DateTime toDate /* 2013-07-26 00:00:00 */)
{
DateTime minDate = new DateTime(1900,01,01,00,00,00);
DateTime maxDate = DateTime.Today;
int result = DateTime.Compare(minDate, fromDate);
if (result >= 0)
{
fromDate = minDate;
}
result = DateTime.Compare(maxDate, toDate);
if (result >= 0)
{
toDate = maxDate;
}
//set up connection, call stored procedure etc etc etc.
return table;
}
那么有人明白这里发生了什么吗?在我看来,我的变量只是不时地随机改变值。
编辑 问题是我只检查 fromDate 是否太小以及 toDate 是否太大,但我需要同时检查两者。感谢 Md. Parvez Alam 和 Can Canbek 帮助我得出这个结论 :)