Articles → .NET → Conversion of a varchar data type to a datetime data type resulted in an out-of-range value

Conversion of a varchar data type to a

datetime data type resulted in an

out-of-range value








Software requirement









Prerequisite knowledge

















Steps of execution















Create a new table





Click to Enlarge


Create a new project





Click to Enlarge


Add controls on the page





Click to Enlarge


Add code to insert data in database table




protected void btnInsert_Click(object sender, EventArgs e) {
  using(SqlConnection connection = new SqlConnection("connection_string")) {
    // Open a connection
    connection.Open();
    string query = string.Empty;
    if (string.IsNullOrEmpty(txtDOB.Text)) query = string.Format("insert into Employee values('{0}','{1}')", txtEmployeeName.Text, DateTime.MinValue);
    else query = string.Format("insert into Employee values('{0}','{1}')", txtEmployeeName.Text, Convert.ToDateTime(txtDOB.Text));

    using(SqlCommand command = new SqlCommand(query, connection)) {
      command.ExecuteNonQuery();
    }
  }
}







Click to Enlarge




Posted By  -  Karan Gupta
 
Posted On  -  Friday, April 26, 2013

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250