title: How insert the data with DateTime type into SQL Server database using c#?
body: <p>I am a new ASP.NET developer and I am facing a problem with inserting some data to the database. I have a form for creating Event. The filled data should be inserted to the database. I have the following database design:</p>
  
  <p>Columns: <code>ID, Title, Description, StartDateTime, EndDateTime, IsActive</code><br>
  (<em><code>StartDateTime</code> and <code>EndDateTime</code> are <code>DateTime</code> data types</em>)</p>
  
  <p>When I tried to insert data, I faced the following problem and I don't know why:</p>
  
  <p><img src="https://i.stack.imgur.com/gFpXu.png" alt="enter image description here"></p>
  
  <p>My code-behind (C#):</p>
  
  <pre><code>protected void submitButton_Click(object sender, EventArgs e)
  {
          string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=RegistrationSysDB;Integrated Security=True;";
          string insertCommand = "INSERT INTO Events (Title, Description, Location, StartDateTime, EndDateTime) values (@Title, @Description, @Location, @StartDateTime, @EndDateTime)";
  
          string title = txtTitle.Text;
          string description = txtDescription.Text;
          string location = txtLocation.Text;
          string startDateTime = start_DateTime.ToString();
          string endDateTime = end_DateTime.ToString();
  
          using(SqlConnection conn = new SqlConnection(connString))
          {
              //open DB Connection
              conn.Open();
  
              using (SqlCommand cmd = new SqlCommand(insertCommand, conn))
              {
                  cmd.Parameters.Clear();
                  cmd.Parameters.AddWithValue("@Title", title);
                  cmd.Parameters.AddWithValue("@Description", description);
                  cmd.Parameters.AddWithValue("@Location", location);
                  cmd.Parameters.AddWithValue("@StartDateTime", startDateTime);
                  cmd.Parameters.AddWithValue("@EndDateTime", endDateTime);
                  cmd.ExecuteNonQuery();
              }
  
              conn.Close();
          }
  
          MultiView1.SetActiveView(ViewConfirm);
      }
  </code></pre>
  
  <p>My ASP.NET code:</p>
  
  <pre><code>&lt;div id="contactform"&gt;
                  &lt;fieldset&gt;
                          &lt;label for="title"&gt;
                              Title&lt;/label&gt;
                          &lt;asp:TextBox ID="txtTitle" CssClass="text-input" runat="server"&gt;&lt;/asp:TextBox&gt;
                          &lt;br /&gt;
                          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter a subject for your message"
                              ControlToValidate="txtTitle"&gt;&lt;/asp:RequiredFieldValidator&gt;
  
                          &lt;label for="description"&gt;
                              Description&lt;/label&gt;
                          &lt;asp:TextBox ID="txtDescription" TextMode="MultiLine" CssClass="textarea" Rows="6" cols="50"
                              runat="server"&gt;&lt;/asp:TextBox&gt;
                          &lt;br /&gt;
                          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter your message"
                              ControlToValidate="txtDescription"&gt;&lt;/asp:RequiredFieldValidator&gt;
  
                          &lt;label for="location"&gt;
                              Location&lt;/label&gt;
                          &lt;asp:TextBox ID="txtLocation" CssClass="text-input" runat="server"&gt;&lt;/asp:TextBox&gt;
                          &lt;br /&gt;
                          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Please enter your message"
                              ControlToValidate="txtLocation"&gt;&lt;/asp:RequiredFieldValidator&gt;
  
                          &lt;label for="start_DateTime"&gt;
                              Start Date &amp; Time&lt;/label&gt;
                          &lt;asp:TextBox ID="start_DateTime" CssClass="textarea" runat="server"&gt;&lt;/asp:TextBox&gt;
                          &lt;%--&lt;ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="start_DateTime" 
                                                          PopupPosition="Right"&gt;
                          &lt;/ajaxToolkit:CalendarExtender&gt;--%&gt;
                          &lt;br /&gt;
                          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please enter your message"
                              ControlToValidate="start_DateTime"&gt;&lt;/asp:RequiredFieldValidator&gt;
  
                          &lt;label for="end_DateTime"&gt;
                              End Date &amp; Time&lt;/label&gt;
                          &lt;asp:TextBox ID="end_DateTime" CssClass="textarea" runat="server"&gt;&lt;/asp:TextBox&gt;
                          &lt;%--&lt;ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="end_DateTime" 
                                                          Format=" PopupPosition="Right"&gt;
                          &lt;/ajaxToolkit:CalendarExtender&gt;--%&gt;
                          &lt;br /&gt;
                          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please enter your message"
                              ControlToValidate="end_DateTime"&gt;&lt;/asp:RequiredFieldValidator&gt;
  
                          &lt;asp:Button ID="submitButton"  runat="server" CssClass="button" 
                              Text="Create &amp;rarr;" onclick="submitButton_Click" /&gt;
                &lt;/fieldset&gt;
              &lt;/div&gt;
  </code></pre>
  
  <p>FYI, I am using jQuery UI <code>DateTimePicker</code> for inserting the <code>StartDateTime</code> and <code>EndDateTime</code>. </p>
  
  <p>So how can I insert the data with <code>DateTime</code> type into the database?</p>
  
  <p><strong>UPDATE #1:</strong>  </p>
  
  <p>I changed my code to the following:</p>
  
  <pre><code>protected void submitButton_Click(object sender, EventArgs e)
  {
          string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=RegistrationSysDB;Integrated Security=True;";
          string insertCommand = "INSERT INTO Events (Title, Description, Location, StartDateTime, EndDateTime) values (@Title, @Description, @Location, @StartDateTime, @EndDateTime)";
  
          string title = txtTitle.Text;
          string description = txtDescription.Text;
          string location = txtLocation.Text;
          string startDateTime = start_DateTime.ToString();
          string endDateTime = end_DateTime.ToString();
  
          using(SqlConnection conn = new SqlConnection(connString))
          {
              //open DB Connection
              conn.Open();
  
              using (SqlCommand cmd = new SqlCommand(insertCommand, conn))
              {
                  cmd.Parameters.Clear();
                  cmd.Parameters.AddWithValue("@Title", title);
                  cmd.Parameters.AddWithValue("@Description", description);
                  cmd.Parameters.AddWithValue("@Location", location);
                  cmd.Parameters.AddWithValue("@StartDateTime", "'" + startDateTime + "'");
                  cmd.Parameters.AddWithValue("@StartDateTime", "'" + endDateTime + "'");
                  cmd.ExecuteNonQuery();
              }
  
              conn.Close();
          }
  
          MultiView1.SetActiveView(ViewConfirm);
      }
  </code></pre>
  
  <p>and I got the following error:</p>
  
  <p><img src="https://i.stack.imgur.com/1P5fy.png" alt="enter image description here"></p>