Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here i have to convert txtbox's value into date time and also want to stred in table..

bt it's getting error .
error is
String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.Convert.ToDateTime(String value) at fc.Registration.submit_Click1(Object sender, EventArgs e)'




here is my code
C#
protected void submit_Click1(object sender, EventArgs e)
        {
            try
            {
                SqlConnection sc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\My projects5-6 sem\friend's corner\back up\fc 20-3\fc\App_Data\fc.mdf;Integrated Security=True;User Instance=True");
                sc.Open();
                string str;
               
                str = bdt_txt.Text;
                DateTime dt;
                dt = DateTime.Now;
                string role = "user";

                string cmdstr1 = "INSERT INTO login VALUES('" + unm_txt.Text + "','" + pwd_txt.Text + "','" + DropDownList9.SelectedItem.Value + "','" + ans_txt.Text + "','" + eml_txt.Text + "','" + dt + "','" + role + "')";
                SqlCommand cmd1 = new SqlCommand(cmdstr1, sc);
                dt = Convert.ToDateTime(str);
                cmd1.ExecuteNonQuery();

                string cmdstr = "INSERT INTO register VALUES('" + fnm_txt.Text + "','" + lnm_txt.Text + "','" + eml_txt + "','" + unm_txt.Text + "','" + pwd_txt.Text + "','" + dt + "','" + phn_txt.Text + "','" + RadioButtonList1.SelectedItem.Value + "')";
                SqlCommand cmd = new SqlCommand(cmdstr, sc);
                cmd.ExecuteNonQuery();

                string cmdstr2 = "INSERT INTO profile VALUES('" + unm_txt.Text + "','" + null + "','" + null + "','" + null + "','" + null + "','" + null + "','" + null + "')";
                SqlCommand cmd2 = new SqlCommand(cmdstr2, sc);
                cmd2.ExecuteNonQuery();
              
               
               
               // string cmdstr3 = "INSERT INTO birthday VALUES('" + unm_txt.Text + "','" + dt.ToString("d") + "','" + null + "','" + null + "','" + null + "','" + null + "')";
              
                sc.Close();
                Response.Redirect("Rsuccessful.aspx");
            }
            catch (Exception ex)
            {

                Response.Write(ex.Message);
                Label3.Visible = true;
                Label3.Text = ex.ToString();

            }
        }
Posted
Updated 21-Mar-13 23:26pm
v2
Comments
snehal harawande 22-Mar-13 5:20am    
Please specify in which query you re getting error
CHill60 22-Mar-13 5:30am    
What are the contents of bdt_txt?
mahi0607 22-Mar-13 5:45am    
it's name of may txtbox... which value i have to convert
CHill60 22-Mar-13 5:48am    
I know it's the name of your textbox ... what did you type into that textbox?
mahi0607 22-Mar-13 5:54am    
date of birth .

try DateTime.TryParse instead of Convert. though it should work anyway, try a specific culture, maybe try to specifiy an advanced format yourself and try invariant culture, too. same with styles.
C#
// Parse a date and time with no styles.
dateString = "03/01/2009 10:00 AM";
culture = CultureInfo.CreateSpecificCulture("en-US");
styles = DateTimeStyles.None;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
   Console.WriteLine("{0} converted to {1} {2}.",
                     dateString, dateResult, dateResult.Kind);
else
   Console.WriteLine("Unable to convert {0} to a date and time.",
                     dateString);
 
Share this answer
 
Comments
mahi0607 22-Mar-13 5:51am    
it can be done without any namespace?? if namesapce is required then which one is use?
please trim the string of text box where your getting the date, and then convert it to date time.
 
Share this answer
 
Hi,

string date should be MM/DD/YYYY format.

ASP
protected void Page_Load(object sender, EventArgs e)
{
      string strDate = "03/20/2013";
      DateTime odt;
      odt = DateTime.Now;
      odt = Convert.ToDateTime(a);

      Response.Write(odt.ToString());
}
 
Share this answer
 
v2
Comments
mahi0607 22-Mar-13 5:56am    
bt i have to convert it on the btn click event..
will it be worked on page load??
CHill60 22-Mar-13 6:29am    
He's used Page_Load as an example - use HansHammels solution
CHill60 22-Mar-13 6:28am    
"string date should be MM/DD/YYYY format" ... not in many (most?) countries in the world it shouldn't :-)
Try this,
Convert(Datetime,+'" + dt + "'+,105)

With Regrads,
Gopinath
 
Share this answer
 

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