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

I have tried many thinks as below:

C#
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

C#
DateTime date = DateTime.Parse(DropDownlistDate.SelectedValue);


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.
Posted
Updated 7-Nov-14 0:56am
v2
Comments
KaushalJB 7-Nov-14 6:55am    
What kind of value would you get from DropDownListDate.SelectedValue ?
Amit Jangid 7-Nov-14 7:05am    
"String was not recognized as a valid DateTime."

This exception is displayed. If you can please provide me the codes it will be a great help to me. I just want to know how to convert the item which will be selected by the user to datetime so that I can use that in LINQ query to solve my problem.

SQL
var selectedDate = from d in datacontext.Appointment
                   where d.AppointmentDate.Contains( date)
                   select d;
 
Share this answer
 
The error that you've already mentioned in the comments,

Quote:
String was not recognized as a valid DateTime.


means that the String that you're passing to the Convert.ToDateTime(argument); is not a valid dateTime object. There are actually many things you need to consider while writing a string that can be converted to a DateTime object.

The syntax of the string, the month and date sequence, usage of - or / might also raise this sort of error. It doesn't mean that your dateTime is not a human understandable dateTime but it means that the .NET framework doesn't understand the format of your DateTime.

For example if you use,

C#
// you wanted to write 12th november 2014, seems fair?
DateTime dateTime = new DateTime(12, 11, 2014);
// throws error


To minimize this error, you need to be passing the parameters as

C#
// 2014 year, 11 month and 12th day.
DateTime dateTime = new DateTime(2014, 11, 12);


Same stuff for the strings. While converting from String to a DateTime, you need to take a good care of the DateTime seperator (/ is used in .NET), the Month year day sequence etc. This document[^] of MSDN explains this concept a little bit accurately.

A sample code for you to pass a string would be,

C#
DateTime dateTime = Convert.ToDateTime("05/01/1996");
// converts to 5/1/1996 12:00:00 AM


Read this[^] article of mine to understand the DateTime in (a little bit) depth.
 
Share this answer
 
This way I have converted the date from DropDownList and is working fine.

C#
DateTime date = Convert.ToDateTime(DropDownListDate.SelectedValue.ToString());
 
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