Community discussion forum

String was not recognized as a valid DateTime.

  • 11 months ago
    hi i am using asp.net with c# i get this problem when i try to enter date grater than 12 "String was not recognized as a valid DateTime." i am using sql server 2005. In the table field is in datetime format.In the procedure its is also taking as datetime.and the codes are DateTime dob = Convert.ToDateTime(txtdob.Text.ToString().Trim()); rec.Add("dob", dob); rec is a hash table. and the regional setting short date time :MM/dd/yyyy please help
  • 11 months ago
    Its becoz u have entered a value greateer than 12 for month
  • 11 months ago
    so what i have to do changing regional setting to dd/MM/yyyy
  • 11 months ago
    hi, try this: DateTime DTime = new DateTime(); DTime =Convert.ToDateTime(DateTime.Today.Date.ToShortDateString()); lblDate.Text = string.Format("{0:dd/MM/yyyy}",DTime); Regards, Royal
  • 11 months ago
    Hashtable rec = new Hashtable(); DateTime dob = Convert.ToDateTime(txtdob.Text.Trim()); rec.Add("dob", dob.ToShortDateString()); why the second line not executing .i have gave a brak point to see the flow ,but after the first line its jump to catch()
  • 11 months ago
    That is because you have an error at runtime. Read the exception to see what is triggering the fault. Regards Si
  • 11 months ago
    i am using this code in the submit event not in page load. protected void btnSave_Click(object sender, System.EventArgs e) { try { Hashtable rec = new Hashtable(); if (txtdob.Text.Length != 0) { DateTime dob = Convert.ToDateTime(txtdob.Text.Trim()); rec.Add("dob", dob.ToShortDateString());//execution not goes to this line....it jumps to the catch() } } catch( Exception ex) { lblerror.visible=true; lblerror.text=ex.Message; } } exception is again same: String was not recognized as a valid DateTime.
  • 11 months ago
    i am using this code in the submit event not in page load.
    protected void btnSave_Click(object sender, System.EventArgs e) { try { Hashtable rec = new Hashtable(); if (txtdob.Text.Length != 0) { DateTime dob = Convert.ToDateTime(txtdob.Text.Trim()); rec.Add("dob", dob.ToShortDateString());//execution not goes to this line....it jumps to the catch() } } catch( Exception ex) { lblerror.visible=true; lblerror.text=ex.Message; } } exception is again same: String was not recognized as a valid DateTime.
  • 11 months ago
    i am using this code in the submit event not in page load.
    protected void btnSave_Click(object sender, System.EventArgs e)
    {
    try
    {
    Hashtable rec = new Hashtable();
    if (txtdob.Text.Length != 0)
    {
    DateTime dob = Convert.ToDateTime(txtdob.Text.Trim());
    rec.Add("dob", dob.ToShortDateString());//execution not goes to this line....it jumps to the catch()
    }
    } catch( Exception ex)
    {
    lblerror.visible=true;
    lblerror.text=ex.Message;
    }
    }
    exception is again same: String was not recognized as a valid DateTime.
  • 11 months ago
    Hi, DateTime dob = Convert.ToDateTime(txtdob.Text.Trim()); What you should do is provide the format that the txtdob has in it. for example, DateTime dob = Convert.ToDateTime(txtdob.text.trim(), 'dd/MM/yyyy')); See if that works Si
  • 11 months ago
    Compiler Error Message: CS1012: Too many characters in character literal
    Line 431: if (txtdob.Text.Length != 0)
    Line 432: {
    Line 433: DateTime dob = Convert.ToDateTime(txtdob.Text.Trim(),'dd/MM/yyyy');
    Line 434: rec.Add("dob", dob);
    Line 435: }
  • 11 months ago
    oops, my bad. i used single quotes instead of double. Si
  • 11 months ago
    Compiler Error Message: CS1502: The best overloaded method match for 'System.Convert.ToDateTime(object, System.IFormatProvider)' has some invalid arguments
    Source Error:
    Line 431: if (txtdob.Text.Length != 0)
    Line 432: {
    Line 433: DateTime dob = Convert.ToDateTime(txtdob.Text.Trim(),"dd/MM/yyyy");
    Line 434: rec.Add("dob", dob);
    Line 435: }
  • 11 months ago
    did u try string.format() ? Regards, Royal
  • 11 months ago
    HI, sorry, can you try: DateTime dob = Date.ParseExact(txtdob.Text.Trim(), "dd/MM/yyyy"); You will need to change the format to match the datetime format entered in your textbox. Si
  • 11 months ago
    yes i have tried in string format ,the database is in datetime format,its give the error " Error converting data type nvarchar to datetime."
  • 11 months ago
    Compiler Error Message: CS1501: No overload for method 'ParseExact' takes '2' arguments
    Source Error:
    Line 431: if (txtdob.Text.Length != 0)
    Line 432: {
    Line 433: DateTime dob = DateTime.ParseExact(txtdob.Text.Trim(),"dd/MM/yyyy");
    Line 434: rec.Add("dob", dob);
    Line 435: }
  • 11 months ago
    hi, did you try this: DateTime DTime = new DateTime(); DTime =Convert.ToDateTime(DateTime.Today.Date.ToShortDateString()); lblDate.Text = string.Format("{0:dd/MM/yyyy}",DTime); Here, firstly I have converted the string into datetime format then I have changed the format of the date to dd/mm/yyyy. Regards, Royal
  • 11 months ago
    i have used this one but getting the same problem
    Hashtable rec = new Hashtable();
    if (txtdob.Text.Length != 0)
    {
    DateTime dob = Convert.ToDateTime(txtdob.Text.Trim());
    rec.Add("dob", dob.ToShortDateString());
    }
    is there any problem with my web server?
  • 11 months ago
    hi, Just now I tried running this: DateTime DT = new DateTime(); DT = Convert.ToDateTime(TextBox1.Text.Trim()); TextBox2.Text = Convert.ToString(string.Format("{0:dd/MM/yyyy}", DT)); and this worked fine. Regards, Royal
  • 11 months ago
    Its better u add a validator to validate the date before updating the date in the table
  • 11 months ago
    if (txtdob.Text.Length != 0)
    {
    DateTime dob = new DateTime();
    dob=Convert.ToDateTime(txtdob.Text.Trim());
    rec.Add("dob", Convert.ToString(string.Format("{0:dd/MM/yyyy}", dob)));
    }
    Errot:::String was not recognized as a valid DateTime.
  • 11 months ago
    Validate the textbox using compare validator before u insert it into the table. For eg:

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!