Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here i have to store b'day in table.i've took 3 dropdownlist for day.month,year respectively.
and stors it's value to a hiddenfield.
bt it shows me the error that."
String was not recognized as a valid DateTime.
"

here i gave u chunk of code of what i did..
if there is any mistake then plz point me out...
protected void Button1_Click(object sender, System.EventArgs e)
        {
HiddenField1.Value = ddl_mnth.SelectedValue + ddl_day.SelectedValue + ddl_yr.SelectedValue;
          
                cn.Open(); 
                DateTime dt;
              
                string role = "user";
                dt = Convert.ToDateTime(HiddenField1 .Value );
                string cstr = "INSERT INTO register VALUES('" + unm_txt.Text + "','" + fnm_txt.Text + "','" + lnm_txt.Text + "','" + eml_txt.Text + "','" + ddl_lv.SelectedItem.Value + "','" + pwd_txt.Text + "','"+ gd.SelectedValue  +"','" + HiddenField1.Value  + "','" + phn_txt.Text + "')";
                SqlCommand cmd = new SqlCommand(cstr, cn);
                cmd.ExecuteNonQuery();
cn.close();
}
Posted
Updated 14-Apr-13 7:56am
v2
Comments
Richard MacCutchan 14-Apr-13 13:52pm    
What is the actual string in HiddenField1 .Value at the time of the error?
mahi0607 14-Apr-13 13:58pm    
it contain the value selected from dropdownlist control..
my dropdownlists contain the month,year,day,, it's used for b'day.
Richard MacCutchan 14-Apr-13 14:01pm    
Well, you have not answered my question. I asked you what was the actual string at the time of the error, and only that information will help you to solve it.
mahi0607 14-Apr-13 14:09pm    
@richard it contain string type of value..
which r day , month,year and that's it...
day like 1,2,3..
month like jan ,feb....
year like 1989,1990,,, etc...

Richard MacCutchan 14-Apr-13 14:13pm    
Yes but what does it actually contain at the time of the error? What are the exact contents? All you are telling me is what you think it should contain.

String was not recognized as a valid DateTime.


this error generally comes when you try to parse(dd/MM/yyyy) string value to datetime which is not acceptable.

Either store date in your string as (dd/MMM/yy) or (MM/dd/yyyy) and then convert it and it must work.

hope it works...
 
Share this answer
 
If you are not sure if this value is a DateTime value use the output from DateTime.TryParse[^] in the query.
 
Share this answer
 
Comments
mahi0607 14-Apr-13 14:04pm    
no it's datetime value.. actually it's used for birthdate
Abhinav S 14-Apr-13 14:10pm    
From the error String was not recognized as a valid DateTime, it does not appear so.
If you have stored something as a hidden field, it is likely it is a string. Conversion will be required.
Sergey Alexandrovich Kryukov 14-Apr-13 15:11pm    
It depends on actual format; can be resolved with ParseExact, TryParseExact...
—SA
Abhinav S 14-Apr-13 22:46pm    
Yes those could be used too. Incase Parse fails, code should handle failed conditions.

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