Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have one textbow with one dropdownlist as shown below,
ASP.NET
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<table>
<tr>
<td>
<asp:TextBox ID="fromTxt" runat="server" AutoPostBack="true" 
        ontextchanged="fromTxt_TextChanged"></asp:TextBox>
        <asp:CalendarExtender ID="calExt1" runat="server" Format="dd/MM/yyyy" TargetControlID="fromTxt" PopupButtonID="fromTxt"></asp:CalendarExtender>
</td>
<td>
<asp:DropDownList ID="ddTodate" runat="server"></asp:DropDownList>
</td>
</tr>
</table>

I want to bind next 30 days date in dropdownlist(ddTodate) from textbox(fromtxt) textchanged event.
C#
protected void fromTxt_TextChanged(object sender, EventArgs e)
{
//I have to bind Dropdownlist with next 30 days date from the date selected in textbox.
}

for example,if i select date like (15/11/2014) in textbow(fromtxt) then the dropdownlist should bind like this,
16/11/2014
17/11/2014
18/11/2014
....
....
....
....
....
....
....
15/12/2014


Thanks in Advance,
R@JE$#
Posted
Updated 15-Nov-14 1:35am
v4

Hi R@jes#,
Better you can use CTE in Sql
Here is the Query and Modify it ,



SQL
DECLARE @Date Datetime = getdate()
Declare @endDate Datetime=Dateadd(month,1,@date);
;WITH CTE AS
(
    SELECT @Date as dates UNION ALL
    SELECT DATEADD(day,1,dates) FROM CTE where dates<@endDate
)
SELECT dates AS Date FROM CTE



Edited:

SQL
DECLARE @Date Datetime = getdate()
Declare @endDate Datetime=Dateadd(month,1,@date);
;WITH CTE AS
(
    SELECT @Date as dates,1 as no UNION ALL
    SELECT DATEADD(day,1,dates),no+1 FROM CTE where dates<@endDate
)
SELECT dates AS Date ,no FROM CTE


You can Bind The Query Result to your Dropdown List
 
Share this answer
 
v4
Comments
DamithSL 15-Nov-14 7:22am    
I don't see any database related issue ask by OP.
King Fisher 15-Nov-14 7:23am    
Sorry ,I just Suggest to do in Sql
DamithSL 15-Nov-14 7:26am    
you mean taking next n days and binding to drop down functionality by using database? I think it is overkill, simple for loop or linq query can be used without going to db level
King Fisher 15-Nov-14 7:38am    
i think ,instead of using For Loop we can use a simple Query for this,In case OP need to show Date for 3 months or more then what to do ? :)
DamithSL 15-Nov-14 7:45am    
Query may be simple but it is adding another dependency just for generate list of dates. there is nothing you take from database, it is only do the date generation using SQL, you think C# is weak or inefficient for such task?
Try This ... You Will Get Your Solution..

C#
DateTime fromDate = [Your calender Date (TextBox date)];

   for(int i=0; i< 30;i++)
   {
       fromDate = fromDate.AddDays(1);
           ddTodate.Items.Insert(i,fromDate.ToString("dd/MM/yyyy"));

   }



Thanks
AARIF SHAIKH
 
Share this answer
 
Comments
Rajesh waran 15-Nov-14 8:08am    
Thank you. Good one got solution.
aarif moh shaikh 18-Nov-14 5:29am    
Thanks... Rajesh
Rajesh waran 18-Nov-14 5:38am    
you're Welcome.
I think best option is load the data on Date Selection Changed event of calander control, but you can't directly handle it by server side, check this solutionhttp://forums.asp.net/post/4119866.aspx[^]
it will postback using javascript and you can load the dropdownlist data on button click event based on calendar selected date.

C#
DateTime fromDate = // selected calendar date..
List<string> dates = new List<string>();
for(int i=0; i< 15;i++)
{
    fromDate = fromDate.AddDays(1);
    dates.Add(fromDate.ToString("dd/MM/yyyy"));
}

set abow date list as datasource of your dropdown list
 
Share this answer
 
v2
Comments
Rajesh waran 15-Nov-14 7:37am    
sorry sir, i forgot to tag sql-server-R2-2008.Now i have updated my question.
and thank you for the link that you have provided here.

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