Amit Jangid
Amit Jangid

Reputation: 2889

How to Convert Items of Dropdownlist to datetime?

I have a dropdownlist control in asp.net which contains dates from today to next 60 days and another dropdownlist control for time. Now, when a user selects a date from the dropdownlist control I call a method of SelectedItemChanged. I want to check that date from that database to get the times from another dropdownlist which is not saved in database. The problem is in the conversion I am not able to convert the items of dropdownlist which has dates to datetime so that I can check the value or item which is selected by the user in LINQ query.

DateTime date = Convert.ToDateTime(DropDownListDate.SelectedValue);
var selectedDate = from d in datacontext.Appointment
               where d.AppointmentDate == date
               select d;

I also tried to store selected value in session but it didn't worked. I also tried the DateTime.Parse but it didn't worked as well. I want to convert the selected value from the dropdownlist control so that I can use that to check in the database and get the appropriate ans. Please Help. Any help is really appreciated.

I populated the dates in dropdownlist like this:

// List of all dates from now to 60 days
            List<string> datelist = new List<string>();
            datelist.Add("-Select Date-");
            datelist.Add(DateTime.Now.Date.ToShortDateString());

            for (int day = 1; day < 60; day++)
            {
                datelist.Add(DateTime.Now.AddDays(day).ToShortDateString());
            }
DropDownListDate.DataSource = datelist;
            DropDownListDate.DataBind();

Upvotes: 0

Views: 6700

Answers (3)

Grant Winney
Grant Winney

Reputation: 66469

You don't need to convert dates to strings just to display them. And then you won't need to convert the strings back into dates once a selection has been made.

Create your list of dates, keeping them as dates (not strings):

var datelist = new List<DateTime> {DateTime.Now.Date};
for (var day = 1; day < 60; day++)
{
    datelist.Add(DateTime.Now.AddDays(day));
}
DropDownListDate.DataSource = datelist;
DropDownListDate.DataBind();

Then specify a DataTextFormatString to control how the date is displayed to the user:

<asp:DropDownList runat="server" ID="DropDownListDate"
                  AllowSorting="True" AutoGenerateEditButton="True"
                  DataTextFormatString="{0:d}" />  // shows short date only, not time

Then you shouldn't have any problem using the SelectedValue to query the database:

var date = Convert.ToDateTime(DropDownListDate.SelectedValue);
var selectedDate = from d in datacontext.Appointment
                   where d.AppointmentDate == date
                   select d;

Upvotes: 0

SanyTiger
SanyTiger

Reputation: 666

Well once you get the value from the DropDownList, Convert it to string:

string dateString = DropDownListDate.SelectedValue.ToString();

string format = "dd/mm/yyyy";

DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);

string strNewDate = dateTime.ToString("yyyy-dd-mm");

Upvotes: 0

Manu
Manu

Reputation: 418

What error are u getting?

Try this:

DateTime date = Convert.ToDateTime(DropDownListDate.SelectedValue.ToString());

Upvotes: 1

Related Questions