Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string D1 = Convert.ToDateTime(TextBox5.Text).ToString("0:MM/dd/yyyy");
Emp_Course EC = new Emp_Course();
EC.Course_ID = M.Courses.Single(c => c.Course_Name == DropDownList2.Text).Course_ID;
EC.Emp_ID = M.Employees.Single(c => c.Emp_Name == DropDownList1.Text).Emp_ID;
EC.Course_Start_Date = Convert.ToDateTime(D1);
EC.Course_End_Date =Convert.ToDateTime( TextBox6.Text.Trim().ToString());

if (EC.Course_End_Date > EC.Course_Start_Date)
{
  Response.Write("<script>alert('the end date cannot be bigger than start date')</script>");
  return;
}

M.Emp_Courses.InsertOnSubmit(EC);
M.SubmitChanges();
Response.Write("<script>alert('Data Saved')</script>");
Posted
Updated 13-Jan-12 21:20pm
v2

C#
//Why you require it to convert it to string and back to DateTime ?
//string D1 = Convert.ToDateTime(TextBox5.Text).ToString("0:MM/dd/yyyy");

Emp_Course EC = new Emp_Course();
EC.Course_ID = M.Courses.Single(c => c.Course_Name == DropDownList2.Text).Course_ID;
EC.Emp_ID = M.Employees.Single(c => c.Emp_Name == DropDownList1.Text).Emp_ID;

//Try this 
EC.Course_Start_Date = Convert.ToDateTime(TextBox5.Text.Trim());
EC.Course_End_Date =Convert.ToDateTime( TextBox6.Text.Trim());

if (EC.Course_End_Date > EC.Course_Start_Date)
{
Response.Write("<script>alert('the end date cannot be bigger than start date')</script>");
return;
}
M.Emp_Courses.InsertOnSubmit(EC);
M.SubmitChanges();
Response.Write("<script>alert('Data Saved')</script>"); 



You Converted Textbox text to DateTime and then to String again and then back to DateTime. For first one you have used string format and for the end date you did not. Keep your code consistent.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jan-12 20:00pm    
You point if good, a 5, but... why using a text box at all? Please see my answer.
--SA
You don't know what the user can type in your message box. You can deal with it but… Why, why messing with text box at all. Wouldn't be much easier to use a DateTimePicker which would not leave a room for a mistake?

Please see:
DateTimePicker Web Control[^] (a CodeProject article),
http://www.asp.net/community/control-gallery/Item.aspx?i=3221[^],
http://weblogs.asp.net/hajan/archive/2010/07/15/integration-of-jquery-datetimepicker-in-asp-net-website-part-1.aspx[^].

—SA
 
Share this answer
 
Comments
amr_amrovitsh 13-Jan-12 20:05pm    
thank you for replying
it's more easy to deal with datetimepicker but i also need to deal with textbox
the here why cant accept my string format as datetime i did it before in desktop applications but here with ajax extenders !!!!
Sergey Alexandrovich Kryukov 14-Jan-12 0:42am    
Well, I cannot see a reason why would you need to deal with it... I think virang_21 explained you the problem. Also, you don't show what exactly input string is not recognized.
--SA
NandaKumer 14-Jan-12 0:23am    
good links
Sergey Alexandrovich Kryukov 14-Jan-12 0:40am    
Thank you, Nanda.
--SA
Thanks for help
i figured where was the problem if u got the same error
First :-
U have to change The format of datetimepicker u r using to the datetime culture of your windows
my problem was iam using datetimepicker with format MM/dd/yyyy while the built in datetime using dd/MM/yyyy
so all i have done i changed the format and worked well.
 
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