Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Source Error:
C#
Line 64: {
Line 65: 
Line 66:     int a = Sadapter.Insert(txtSErNo.Text, txtSfname.Text, txtSLname.Text, txtSadd.Text, txtScity.Text,txtSstate.Text, 
Line 67:         txtSpin.Text, Convert.ToDateTime(DrpDD.SelectedItem.Text + " " + DrpMM.SelectedItem.Text + " " + DrpYY.SelectedItem.Text),
Line 68:         DrpGender.SelectedItem.ToString(), txtSMoNo.Text, txtSeid.Text, txtSUname.Text, txtSpass.Text);


What I have tried:

C#
int a = Sadapter.Insert(txtSErNo.Text, txtSfname.Text, txtSLname.Text, txtSadd.Text, txtScity.Text,txtSstate.Text, txtSpin.Text, Convert.ToDateTime(DrpDD.SelectedItem.Text + " " + DrpMM.SelectedItem.Text + " " + DrpYY.SelectedItem.Text), DrpGender.SelectedItem.ToString(), txtSMoNo.Text, txtSeid.Text, txtSUname.Text, txtSpass.Text);
Posted
Updated 24-Jul-17 2:08am
v2
Comments
Ralf Meier 24-Jul-17 6:18am    
So ... make a valid DateTime from the Content of the 3 ComboBoxes ...!
Supposing that 'DrpDD.SelectedItem.Text' really contains a valid day and 'DrpMM.SelectedItem.Text' really contains a valid month and 'DrpYY.SelectedItem.Text' really contains a valid year - how should "11 12 13" (for exmaple) automaticly be converted into a date ...?
sommr0 24-Jul-17 7:24am    
Yeah its valid but problm
Graeme_Grant 24-Jul-17 6:20am    
Have you checked that this is a valid format? Convert.ToDateTime Method (String, IFormatProvider) (System)[^]

Convert.ToDateTime(DrpDD.SelectedItem.Text + " " + DrpMM.SelectedItem.Text + " " + DrpYY.SelectedItem.Text)
sommr0 24-Jul-17 7:24am    
yeah Ralf Meier its valid
Ralf Meier 24-Jul-17 8:05am    
No ... it isn't valid because the converter don't know your Format.
Take a look to the Solutions and the Suggestion 'TryParse' or 'ParseExact' instead of 'Convert' ...
See my Solution ...

Convert.ToDateTime(DrpDD.SelectedItem.Text + " " + DrpMM.SelectedItem.Text + " " + DrpYY.SelectedItem.Text)


use DateTime.TryParse instaed of Convert method to make sure the string selected in dropdown lists makes a valid date and if not throw appropriate error message to end user. Refer this for its usage DateTime.TryParse Method (String, DateTime) (System)[^]
 
Share this answer
 
v2
Comments
sommr0 24-Jul-17 7:19am    
its not working DataTime.TryParse method
Atlapure Ambrish 24-Jul-17 7:33am    
Can you show me the code what exactly you tried?
sommr0 24-Jul-17 7:40am    
{ int a = Sadapter.Insert(txtSErNo.Text, txtSfname.Text, txtSLname.Text, txtSadd.Text, txtScity.Text,txtSstate.Text,
txtSpin.Text, Convert.ToDateTime(DrpDD.SelectedItem.Text + " " + DrpMM.SelectedItem.Text + " " + DrpYY.SelectedItem.Text),
DrpGender.SelectedItem.ToString(), txtSMoNo.Text, txtSeid.Text, txtSUname.Text, txtSpass.Text);

if (a == 1)
{
Session["SERNO"] = txtSErNo.Text;
Response.Redirect("AddEducation.aspx");
clean();

}
Atlapure Ambrish 24-Jul-17 8:32am    
Convert the values selected in dropdown lists to date format string first and then use it in TryParse e.g.:

DateTime dateValue;
DateTime.TryParse("5/01/2008", out dateValue)

please refer the article I have provided in my solution.
sommr0 24-Jul-17 8:43am    
it insertion time date time when i change parametr .. it show over loaded error
In addition to the useful answers above, there could also be a problem if the date is in a foreign format (Culture), or the application is run on a machine in a different country.
You can set the needed Culture for your application as follows:
C#
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
 
Share this answer
 
Try it like this :
C#
string myString = DrpDD.SelectedItem.Text + " " + DrpMM.SelectedItem.Text + " " + DrpYY.SelectedItem.Text ;
DateTime myDate = DateTime.ParseExact(myString, "dd MM yy",null) ;


int a = Sadapter.Insert(txtSErNo.Text, txtSfname.Text, txtSLname.Text, txtSadd.Text, txtScity.Text,txtSstate.Text, 
 txtSpin.Text, myDate, DrpGender.SelectedItem.ToString(), txtSMoNo.Text, txtSeid.Text, txtSUname.Text, txtSpass.Text);
 
Share this answer
 
Comments
sommr0 24-Jul-17 8:38am    
when i change it . it show overloaded error
sommr0 24-Jul-17 8:46am    
same error
Ralf Meier 24-Jul-17 9:11am    
When you debug it - how does the content of myString looks like ?
If you want to get a Solution you should try to work with us together. Remember : We don't sit beside you - we can't see what you see.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900