2013-07-26 47 views
0

我有一個頁面,用戶可以輸入「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幫助我得出這個結論:)

回答

1

改變這一行

result = DateTime.Compare(maxDate, toDate); 

result = DateTime.Compare(toDate, maxDate); 

通過這個鏈接

http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx

+0

我也這麼認爲,但是當我這樣做時,if語句沒有進入,我得到了'SqlDateTime溢出。必須介於1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM.' –

+0

你在第二次比較時得到這個錯誤 –

+0

請嘗試像這樣result = DateTime.Compare(toDate, DateTime.Now); –

1

好吧,讓我們一步一步走;

你有你今天的minDate值和maxDate值。

result = DateTime.Compare(maxDate, toDate); 

    if (result >= 0) 
    { 
     toDate = maxDate; 
    } 

當你進入這個部分,你比較今天的日期與todate變量,因爲今天的日期是你TODATE值後,返回「1」,分配今天的日期TODATE值從而回到今天的日期。

我認爲,如果你與此開關的代碼,它應該工作

result = DateTime.Compare(maxDate, toDate); 

    if (result < 0) 
    { 
     toDate = maxDate; 
    } 

因此,如果輸入實際上是今天的日期後,你把輸入到今天的日期。

+0

正如我回答馬里蘭州佩爾韋茲×阿拉姆,我在第一次嘗試這樣做,因爲這是它在MSDN文檔上的外觀如何。但是當我這樣做時,我得到這個錯誤:'SqlDateTime溢出。必須介於1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM.'之間,因爲If-語句永遠不會輸入 –

+0

那麼您應該添加一個else語句並返回原始toDate的值 –