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..
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
Dev1411
Member
7 Points
184 Posts
Conversion to DateTime
May 23, 2012 03:46 PM|LINK
hi all,
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
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Conversion to DateTime
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[2],dt[0],dt[1]); //new DateTime(year,month,day)
Dev1411
Member
7 Points
184 Posts
Re: Conversion to DateTime
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 ?
teh munk
Participant
1466 Points
297 Posts
Re: Conversion to DateTime
May 23, 2012 04:05 PM|LINK
If you are using SqlCommand parameters;
SqlCommand command = new SqlCommand(commandText, connection); command.Parameters.Add("@DateTime", SqlDbType.SmallDateTime); command.Parameters["@DateTime"].Value = System.DateTime.ParseExact(txtDOB.txt);Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Conversion to DateTime
May 23, 2012 04:06 PM|LINK
oh sorry forgot to mention int parse
new DateTime(int.Parse(dt[2]),int.Parse(dt[0]),int.Parse(dt[1]))
Dev1411
Member
7 Points
184 Posts
Re: Conversion to DateTime
May 23, 2012 04:10 PM|LINK
it says string was not recognized as valid datetime
Dev1411
Member
7 Points
184 Posts
Re: Conversion to DateTime
May 23, 2012 04:13 PM|LINK
it throws a different exception
Index was outside the bounds of the array.
Nasser Malik
Star
10990 Points
1691 Posts
Re: Conversion to DateTime
May 23, 2012 04:13 PM|LINK
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
Skype: maleknasser1
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Conversion to DateTime
May 23, 2012 04:18 PM|LINK
whats date in textbox just want to know the format
Dev1411
Member
7 Points
184 Posts
Re: Conversion to DateTime
May 23, 2012 04:18 PM|LINK
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