Forums - C# Corner

Forum guidelines
Sony Biswas

Sony Biswas

  • 1.5k
  • 27
  • 1.2k

How to convert varchar to datetime

Jan 1 2017 5:44 AM
When I try to update the dates "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value" error is occurred.  How can I fix this issue. 
 
c# code
------------
  1. SqlConnection cs = new SqlConnection(ConfigurationManager.ConnectionStrings["travelConnectionString"].ConnectionString);  
  2. String str = "UPDATE [tbl_training_details] SET [service_agent_id] = '" + 
  3. ddlServiceAgentList.SelectedValue + "',[training_addmission_date] = '" + txtAddmissionDate.Text + "',[training_exam_date] = '" + txtExamDate.Text + "',[training_certificate_no] = '" + txtCertificateNo.Text + "',[training_certificate_date] = '" + txtCertificateDate.Text + "',[status_id] = '" + ddlStatus.SelectedValue + "',[comment] = '" + txtComment.Text + "'WHERE [passenger_id] = " + txtPassengerName.Text;  
  4. SqlCommand sc = new SqlCommand(str, cs);  
  5. cs.Open();  
  6. sc.ExecuteNonQuery();  
  7. cs.Close();  
 str value I got from this code is 
 
  1. str = "UPDATE [tbl_training_details] SET [service_agent_id] = '0',[training_addmission_date] = '29/09/2016',[training_exam_date] = '20/10/2016',[training_certificate_no] = '5rfde',[training_certificate_date] = '2/11/2016',[status_id] = '14',[comment] = '5gfds'WHERE [passenger_id] = 10413"  
Thanks

Answers (4)