Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"  />


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??
Posted
Updated 11-Sep-13 6:18am
v2
Comments
ZurdoDev 11-Sep-13 12:26pm    
Put a breakpoint and walk through your code and let us know what is wrong.
Member 10066916 11-Sep-13 12:28pm    
I did that...Its taking in the from date and to date values from the textbox...but it skips the if condition...and thats y the dropdown list is empty
ZurdoDev 11-Sep-13 12:30pm    
So, there's your answer. There are not tables which means your SQL is not returning any rows. So, what's the question?
Member 10066916 11-Sep-13 13:03pm    
thank you ryan :)

1 solution

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:
SQL
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.
 
Share this answer
 
Comments
Member 10066916 11-Sep-13 12:35pm    
yes this is what i was looking for..thank you :)
OriginalGriff 11-Sep-13 12:42pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900