Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Here is my code to get 3 dates(12.5.2011,12.4.2011,12.3.2011) and display tat in dropdwonlist.I want to show the date which is having data in database.otherwise i want to show the previous 3 dates(12.4.2011,12.3.2011 and 12.2.2011) which is having data in database.how can i achieve this...

can any one tell idea to solve..?
C#
string[] date = new string[8];
        DateTime dt = DateTime.UtcNow.AddHours(5.5);
        DropDownList1.Items.Insert(0, "--SELECT DATE--");
        DropDownList1.Items.Insert(1, new ListItem(dt.ToString("MM/dd/yyyy")));
        for (int i = 1; i < 3; i++)
        {
            string s = dt.AddDays(-i).ToString("MM/dd/yyyy");
            DropDownList1.Items.Insert(1 + i, new ListItem(s, s));
        }
Posted
Updated 4-Dec-11 23:07pm
v2
Comments
[no name] 5-Dec-11 5:09am    
EDIT: added "pre" tag
Richard MacCutchan 5-Dec-11 5:12am    
Please explain what is not working with the above code.
Thendral.N 5-Dec-11 5:15am    
it is giving dates(12.5.2011,12.4.2011,12.3.2011) which is having values..if the today date is not having value means need to display date like 12.4.2011,12.3.2011 and 12.2.2011 in drop downlist..then after pushing data for today date then the date will be display in dropdownlist..
RavinderaG 5-Dec-11 8:15am    
Can u explain it more what exactly u want here ?
Thendral.N 5-Dec-11 8:23am    
i am loading records from excel to sql table.after pusing data to table only the 3 dates which is having records in db need to listed in dropdown...otherwise before today's dates r listed in dropdown...i mentioned the code already..in that i got 3 dates but suppose if there is no record in today date means i need to remove today date and display 3 days like december 4,3,2 records in gridview...how can i remove the date and getting before today's date..

1 solution

In business logic
DataTable dt = GetDataFromDataBase();
if(dt.Rows.Count == 0)
{
  dt.Row.Add(new object[]{"12.5.2011"});
  ...
}

In code-behind
DropDownList1.DataSource = dt;


When adding items to the DropDownList Items collection in sequential order it is not necessary to use Insert. Using the Add method will be faster.
 
Share this answer
 

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