title: Trying to convert string to datetime
body: <p>I am trying to restrict a user from entering a new record if the date requested already exits.   I was trying to do a count to see if the table that the record would be placed in already has that date 1  or not 0. I have a calendar extender attached to a text box which has the date.</p>
  
  <p>I keep getting either a:</p>
  
  <p>String was not recognized as a valid DateTime.</p>
  
  <p>or </p>
  
  <p>Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'.</p>
  
  <p>depending on the different things I have tried. </p>
  
  <p>Here is my code. </p>
  
  <pre><code> TextBox startd = (TextBox)(DetailsView1.FindControl("TextBox5"));
                  TextBox endd = (TextBox)(DetailsView1.FindControl("TextBox7"));
                  DropDownList lvtype = (DropDownList)(DetailsView1.FindControl("DropDownList6"));
  
                  DateTime scheduledDate = DateTime.ParseExact(startd.Text, "dd/MM/yyyy", null);
                  DateTime endDate = DateTime.ParseExact(endd.Text, "dd/MM/yyyy", null);
  
                  DateTime newstartDate = Convert.ToDateTime(startd.Text);
                  DateTime newendDate = Convert.ToDateTime(endd.Text);
  
                  //foreach (DataRow row in sd.Tables[0].Rows)
                  DateTime dt = newstartDate;
  
                  while (dt &lt;= newendDate)
                  {
                      //for retreiving from table
                      Decimal sd = SelectCountDate(dt, lvtype.SelectedValue, countDate);
  
                      String ndt = Convert.ToDateTime(dt).ToShortDateString();
  
                      // //start = string.CompareOrdinal(scheduledDate, ndt);
                      // // end = string.CompareOrdinal(endDate, ndt);
  
                      //trying to make say when leavetpe is greater than count 1 then throw error. 
                      if (sd &gt; 0)
                      {
                          Response.Write("&lt;script&gt;alert('Date Already Requested');&lt;/script&gt;");
                      }
                      dt.AddDays(1);
  
                  }
  </code></pre>
  
  <p>^^^
  This version throws the: "String was not recognized as valid date type" error</p>
  
  <p>But if i replace the string with either of these :</p>
  
  <pre><code>  /*-----------------------Original------------------------------------
                  string scheduledDate = Convert.ToDateTime(endd).ToShortDateString();
                  string endDate = Convert.ToDateTime(endd).ToShortDateString();
                  -------------------------------------------------------------------*/
  
                  /*----------10-30---------------------------------------
                  DateTime scheduledDate = DateTime.Parse(startd.Text);
                  DateTime endDate = DateTime.Parse(endd.Text);
                  ------------------------------------------------------*/
  </code></pre>
  
  <p>I get the "Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'." error. </p>
  
  <p>I am just trying to stop a user from entering a record date that already exits.</p>
  
  <pre><code>                &lt;InsertItemTemplate&gt;
                      &lt;asp:TextBox ID="TextBox5" runat="server" Height="19px"
                          Text='&lt;%# Bind("lstdate",  "{0:MM/dd/yyyy}") %&gt;' Width="67px"&gt;&lt;/asp:TextBox&gt;
                      &lt;asp:CalendarExtender ID="TextBox5_CalendarExtender" runat="server" Enabled="True"
                          TargetControlID="TextBox5"&gt;
                      &lt;/asp:CalendarExtender&gt;
                      &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                          ControlToValidate="TextBox5" ErrorMessage="*Leave Date Required" 
                          ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;
                      &lt;br /&gt;
                      &lt;asp:CompareValidator ID="CompareValidator18" runat="server" 
                          ControlToCompare="TextBox7" ControlToValidate="TextBox5" 
                          ErrorMessage="Leave date cannot be after start date" ForeColor="Red" 
                          Operator="LessThanEqual" ToolTip="Must choose start date before end date"&gt;&lt;/asp:CompareValidator&gt;
                  &lt;/InsertItemTemplate&gt;
  </code></pre>