我有一個頁面,用戶可以輸入「from date」和「to date」並根據date-intervall得到一組值。唯一的問題是它不適用於「迄今爲止」。 當我輸入這樣的東西: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;
}
所以有人明白這裏發生了什麼?在我看來,我的變量只是隨機更改值。
編輯 問題是,我只是檢查是否從日期太小,如果toDate是大,但我需要檢查兩者。感謝Md Parvez Alam和Can Canbek幫助我得出這個結論:)
我也這麼認爲,但是當我這樣做時,if語句沒有進入,我得到了'SqlDateTime溢出。必須介於1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM.' –
你在第二次比較時得到這個錯誤 –
請嘗試像這樣result = DateTime.Compare(toDate, DateTime.Now); –