从日期范围获取价值 [英] Getting value from date range

查看:63
本文介绍了从日期范围获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框

I have two textboxes

<asp:TextBox ID="Txtfromdate" runat="server" CssClass="txtBoxNormalmedium"
                                                               Height="16px" >mm/dd/yy</asp:TextBox>


   <asp:TextBox ID="Txttodate" runat="server" CssClass="txtBoxNormalmedium"
                                                               >mm/dd/yy</asp:TextBox>



名称下拉


A name drop down

<asp:DropDownList ID="ddlname" runat="server" CssClass="ddlNormalMedium"
        AutoPostBack="True" OnSelectedIndexChanged="ddlname_SelectedIndexChanged"
          >
    </asp:DropDownList>



和搜索按钮


and Search button

<asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="btnNormal" onclick="btnSearch_Click"  />





因此,如果用户选择说第6条可能因为从日期和10日可以作为到目前为止,在点击搜索按钮后,该日期范围内的所有名称都应绑定到数据库中。

以下是我试过的代码,它不是在下拉列表中显示任何名称。



So if a user selects say 6th may as the from date and 10th may as the to date,All the names within that date range should be bound into the database after the Search button is clicked.
The below is the code i tried, its not displaying any names in the drop down list.

protected void btnSearch_Click(object sender, EventArgs e)
       {
           DateTime fromDate, toDate;
           fromDate = DateTime.Parse(Txtfromdate.Text.ToString());
           toDate = DateTime.Parse(Txttodate.Text.ToString());
           SqlCommand cmd = new SqlCommand("SELECT  [emp_name], [created_date], [todate] FROM [T_TADA_temp] WHERE (([created_date] = @DateFrom) AND ([todate] = @DateTo))", conn);
           cmd.Parameters.Add("@DateFrom", SqlDbType.DateTime).Value = fromDate;
           cmd.Parameters.Add("@DateTo", SqlDbType.DateTime).Value = toDate;
           DataSet objDs = new DataSet();
           SqlDataAdapter sd = new SqlDataAdapter(cmd);
           conn.Open();
           sd.Fill(objDs);
           conn.Close();
           if (objDs.Tables[0].Rows.Count > 0)
           {
               ddlname.DataSource = objDs;

               ddlname.DataTextField = "emp_name";
               ddlname.DataValueField = "emp_name";

               ddlname.DataBind();

           }







我哪里错了? ?




where am i going wrong??

推荐答案

当您谈到日期范围时,通常表示2012年2月2日至2013年6月14日之间的所有日期或类似日期 - 但您的代码不是尝试这样做,所以我对你想要达到的目标感到有些困惑,特别是当你描述你想要的东西时如果用户选择说第6个可能作为起始日期,而第10个可能作为到目前为止,该日期范围内的所有名称都是。



您检查的代码有两个特定条件:开始日期正好是这个日期和结束日期正是那个日期。如果你想要一个范围,例如开始日期和结束日期必须在开始日期和结束日期之间,那么它非常简单:

When you talk about a date range, you normally mean "all dates between 2nd Feb 2012 and 14th Jun 2013" or similar - but your code doesn't try to do that, so I'm left a little confused as to what you are trying to achieve, especially when you describe what you want as "if a user selects say 6th may as the from date and 10th may as the to date,All the names within that date range".

The code you have checks for two specific conditions: "the start date is exactly this date", and "the end date is exactly that date". If you do want a range, such as "the start and end dates must be between the start and end dates" then it's pretty simple:
SELECT * FROM MyTable WHERE created_date BETWEEN @DateFrom AND @DateTo AND todate BETWEEN @DateFrom AND @DateTo





如果那不是你想要做的,那么我们需要更好地描述你期望发生的事情,也许只有一两个例子。



If that isn't what you are trying to do, then we need a better description of what you do expect to happen, perhaps with an example or two.


这篇关于从日期范围获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆