Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 25, 2012 06:16 AM by Allen Li - MSFT
May 23, 2012 03:46 PM|LINK
I am trying to convert the data from a calender control from Ajax toolkit (for Date of Birth) using the code
create.DOB = System.DateTime.Parse(txtDOB.Text); but it throws an exception saying conversion to smalldatetime failed.
I also tried
create.DOB = System.DateTime.Parse(txtDOB_CalendarExtender.ToString()); .. still the exception is the same.
here create is the object of the class where I have defined the properties for passing the data into stored procedures for inserting the data. The Database works on smalldatetime everywhere.
the DOB property defined in the main class is of type DateTime..
Where am I wrong ? Kindly help urgently
Thanks in advance
May 23, 2012 03:59 PM|LINK
Parse method the expects in specific format and value in txtDOB.Text is not in same format you may split the values example
if value in txtDOB.Text is 05/28/2012 then
string dt = txtDOB.Text.Split('/');
create.DOB = new DateTime(dt,dt,dt); //new DateTime(year,month,day)
May 23, 2012 04:03 PM|LINK
It gives an error saying that
the best overloaded method match for System.DateTime.DateTime(int, int, int) has some invalid arguments.
any further suggestions ?
May 23, 2012 04:05 PM|LINK
If you are using SqlCommand parameters;
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters["@DateTime"].Value = System.DateTime.ParseExact(txtDOB.txt);
May 23, 2012 04:06 PM|LINK
oh sorry forgot to mention int parse
May 23, 2012 04:10 PM|LINK
it says string was not recognized as valid datetime
May 23, 2012 04:13 PM|LINK
it throws a different exception
Index was outside the bounds of the array.
If you are going to beyond smalldatetime range (1900-01-01 through 2079-06-06) than you will get this error.
One thing you can do in your database to turn smalldatetime into datetime will solve your problem
May 23, 2012 04:18 PM|LINK
whats date in textbox just want to know the format
Well Naseer bro .. I hope you understand that changing the database would create alot of problems... I need to do it do it in smalldatetime at all the places...as per the requirement