title: How to fix The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.'
body: <p>I try to insert my data into SQL Server but the  query cannot run due to an error </p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException: 'The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.</p>
  </blockquote>
  
  <p>I'm using c# to code.</p>
  
  <pre><code>txtPlateNo.Text = vehicresult.results[0].plate.ToString();
  lblDate.Text = DateTime.Now.ToShortDateString();
  lblTime.Text = DateTime.Now.ToShortTimeString();
  
  //string parkDate = lblDate.Text;
  //DateTime newDate = Convert.ToString(parkDate.ToString("MMddyyyy"));
  //string parkTime = lblTime.Text;
  //DateTime newTime = Convert.ToDateTime(parkTime);
  
  //label10.Text = newDate;
  //string newDate2 = label10.Text.ToString();
  //string newTime2 = newTime.ToString();
  
  SqlConnection conn = new SqlConnection(new DBConnection().ConnectionString);
  if (conn.State == System.Data.ConnectionState.Closed)
      conn.Open();
  SqlDataReader dr;
  
  SqlCommand cmd = new SqlCommand("SELECT * FROM pmsVehicleRecord WHERE clVehicPlateNo='" + txtPlateNo.Text + "'", conn);
  
  dr = cmd.ExecuteReader();
  
  if (dr.HasRows)
  {
      while (dr.Read())
      {
          lblFullName.Text = dr["clFullName"].ToString();
          lblUsername.Text = dr["clUsername"].ToString();
          lblAddress.Text = dr["clAddress"].ToString();
          lblPaymentMethod.Text = dr["clPaymentMethod"].ToString();
      }
  
      dr.Close();
      SqlCommand cms = new SqlCommand("SELECT * FROM pmsParkingRecord WHERE parkVehicPlateNo ='" + txtPlateNo + "' AND parkDate='" + lblDate + "'", conn);
      dr = cms.ExecuteReader();
  
      if (dr.HasRows)
      {
          dr.Close();
          SqlCommand cmt = new SqlCommand("UPDATE pmsParkingRecord SET parkExitTime='" + DateTime.Now.ToShortTimeString() + "'");
          cmt.Connection = conn;
          if (conn.State == System.Data.ConnectionState.Closed)
              conn.Open();
          SqlDataReader ds = cmt.ExecuteReader();
      }
      else
      {
          dr.Close();
          SqlCommand cmn = new SqlCommand("INSERT INTO pmsParkingRecord (parkUsername, parkFullName, parkAddress, parkVehicPlateNo, parkPaymentMethod, parkDate, parkEntryTime) VALUES ('" + lblUsername.Text + "', '" + lblFullName.Text + "', '" + lblAddress.Text + "', '" + txtPlateNo.Text + "', '" + lblPaymentMethod.Text + "', '" + Convert.ToDateTime(DateTime.Now.ToShortDateString()) + "', '" + Convert.ToDateTime(DateTime.Now.ToShortTimeString()) + "')");
          cmn.Connection = conn;
          if (conn.State == System.Data.ConnectionState.Closed)
              conn.Open();
          SqlDataReader ds = cmn.ExecuteReader();
      }
  }
  else
  {
      lblWarning.Text = "License Plate No is not registered!";
  }
  dr.Close();
  </code></pre>
  
  <p>The expected result suppose to insert the data into database and display the data on label.</p>
  
  <p>The error shows on this line </p>
  
  <pre><code>SqlDataReader ds = cmn.ExecuteReader();
  </code></pre>