Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using asp.net and c# with sql-server2005 database.

I am entering a date in textbox in format mm/dd/yyyy,yesterday it was working correctly but 2day i am getting error "String is not valid for datetime".

I am using this:
DateTime dt = DateTime.Parse(txtdob.Text);

and running query
SqlCommand cmd = new SqlCommand("insert into new_user values('" + ID + "','" + txtname.Text.Trim() + "','" + txtcompany.Text.Trim() + "','" + txtdesig.Text.Trim() + "',' " + txtcontact.Text.Trim() + " ','" + txtemail.Text.Trim() + "', '" + txtadd.Text.Trim() + "','" + dt + "')", con);

If you require anything else I can provide you.

The basic problem is I want to insert date like birthdate in textbox and want to save it in database.

Please help me, its urgent....

Thanks
Posted
Updated 15-Sep-11 7:00am
v5
Comments
Pradeep Shukla 15-Sep-11 13:02pm    
I don't see any issue here, at which line you got this exception?

This is a bit of a long shot, but have you changed your locale settings recently? DateTime.Parse uses the system locale settings by default, so if you switched from a locale using mm/dd/yyyy to one using some other format (e.g. dd/mm/yyyy), then that could cause the error you're seeing.

P.S. you really should be using parameterized queries so as to avoid SQL injection!
 
Share this answer
 
v2
Comments
RaviRanjanKr 15-Sep-11 17:49pm    
Nice Answer, My 5+
the text in textbox was not in correct form.may be it is null or may be it is not in date time format
 
Share this answer
 
Why dont you try to convert dt to a string

string newDate=Convert.ToString(dt);

then save as

SqlCommand cmd = new SqlCommand("insert into new_user values('" + ID + "','" + txtname.Text.Trim() + "','" + txtcompany.Text.Trim() + "','" + txtdesig.Text.Trim() + "',' " + txtcontact.Text.Trim() + " ','" + txtemail.Text.Trim() + "', '" + txtadd.Text.Trim() + "','" + newDate + "')", con);
 
Share this answer
 
Comments
Member 7861155 17-Sep-11 2:38am    
I already did it, bt it is not working, it is showing error "String was not recognized as a valid DateTime".
Is there any other way to insert date. just tel me....
Thanks
Anele Ngqandu 17-Sep-11 3:09am    
Sory sir why did you accept the solution if its not working?

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