Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have the follwing problem in my ASP project :
i use LinQ to Retrive data including date however it throw an exception
the Code:
C#
DateTime? castvar = DateTime.Parse(TextBox1.Text);
       DateTime neededDay;
        //and then check it to give the value of the neededadte for linq query
       if (castvar != null)
       {

           neededDay = castvar.Value;


       }
       else
       {
           neededDay = DateTime.Now;
       }

            var DayReservations = (from r in db.reservations
                                  join p in db.Clints on r.clintId equals p.ClintID
                                  join d in db.Emplyees on r.doctorID equals d.EmpID
                                  where r.apointDateStart.Date == neededDay.Date
                                 && r.apointDateStart.Month == neededDay.Month
                                  && r.apointDateStart.Day == neededDay.Day
                                  && r.doctorID == Convert.ToInt32(DoctorList.SelectedValue)
                                  select new { p.Name, d.EmpName, r.apointDateStart, r.apointEndTime, r.suggestedProcedure, r.comment }).ToList();



the Exception :

Input string was not in a correct format.When converting a string to DateTime ,parse the string to take the date before putting each variable into the DateTime object.Make sure your method arguments are in right format.
this exception was thrown in the LinQ query



In the SQL the Reservation table is:
SQL
[clintId] = <clintId, int,>
     ,[doctorID] = <doctorID, int,>
     ,[apointDateStart] = <apointDateStart, datetime,>
     ,[apointDateEnd] = <apointDateEnd, datetime,>
     ,[comment] = <comment, nvarchar(max),>
     ,[suggestedProcedure] = <suggestedProcedure, nvarchar(max),>



where is the problem?????!!!!
Posted

1 solution

DateTime.Parse does not return a null on failure; it throws an exception. Use DateTime.TryParse.
DateTime neededDay = DateTime.Today;
DateTime.TryParse(textBox1.Text, out neededDay);


If "r.apointDateStart" is nullable, your LINQ expression should test for null values first.

Quote:
where r.apointDateStart.Date == neededDay.Date
&& r.apointDateStart.Month == neededDay.Month
&& r.apointDateStart.Day == neededDay.Day


If the "Date" values are equal, there is no to test if the "Month" and "Day" properties are also equal.
 
Share this answer
 
Comments
DentalDeveloper 11-Sep-13 11:08am    
thanx:) TnTinMn

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