title: Adding datetime to SQL database from formatted textbox fields
body: <p>I'm trying to add a datetime into a database table by grabbing values from textboxes that have various masks etc. I'm getting the following error:</p>
  
  <pre><code>String was not recognized as a valid DateTime.
  </code></pre>
  
  <p>Textbox code:</p>
  
  <pre><code>&lt;asp:TableRow&gt;
          &lt;asp:TableCell&gt;    
                  &lt;asp:ToolkitScriptManager ID="calendarToolkit" runat="server"&gt;&lt;/asp:ToolkitScriptManager&gt;   
                  &lt;asp:TextBox ID="startDateTextbox" runat="server"&gt;&lt;/asp:TextBox&gt;  
                  &lt;asp:CalendarExtender Format="yyyy-dd-MM"  ID="startDateExtender" TargetControlID="startDateTextbox" runat="server" /&gt; 
          &lt;/asp:TableCell&gt;
      &lt;/asp:TableRow&gt;
  
      &lt;asp:TableRow&gt;
          &lt;asp:TableCell&gt;    
                  &lt;asp:TextBox ID="startTimeTextbox" runat="server"&gt;&lt;/asp:TextBox&gt;  
                  &lt;asp:MaskedEditExtender ID="m1" runat="server" Mask="99:99" MaskType="Time" AcceptAMPM="false" 
                                      MessageValidatorTip="true" TargetControlID="startTimeTextbox"
                                      ClearMaskOnLostFocus="false"&gt;
                  &lt;/asp:MaskedEditExtender&gt;
  
                  &lt;asp:MaskedEditValidator ID="mv1" runat="server" ControlExtender="m1" ControlToValidate="startTimeTextbox"
                                      Display="none" EmptyValueMessage="Time is required" InvalidValueMessage="Valid Start Time"
                                      IsValidEmpty="true" TooltipMessage="Input a time"&gt;
                  &lt;/asp:MaskedEditValidator&gt;
          &lt;/asp:TableCell&gt;
      &lt;/asp:TableRow&gt;
  </code></pre>
  
  <p>c# code to add values into database:</p>
  
  <pre><code>string sqlQuery =
                  "insert into dbo.Event_Info(event_name, description, location, date_end,date_start,image_url) values(@name, @desc, @location,@startDate,@endDate,@imageURL)";
  
  SqlConnection dbConnection = new SqlConnection("server=(local)\\SGSQL;database=STEvent;Trusted_Connection=yes");
              dbConnection.Open();
              SqlCommand dbCommand = new SqlCommand(sqlQuery, dbConnection);
  
  SqlParameter startParam = new SqlParameter("@startDate",SqlDbType.DateTime);
              startParam.Value = Convert.ToDateTime(startDateTextbox.Text +" " + startTimeTextbox.Text);
  
              dbCommand.Parameters.Add(startParam);
              dbCommand.ExecuteNonQuery();
  
              dbConnection.Close();
  </code></pre>
  
  <p>Note: Only added a subset of the code as showing all the other parameters seemed long and redundant.</p>