title: Insert datatime value from ASP.NET gridview to SQL Server Datatable
body: <p>I have a column in ASP.NET gridview that displays the date value in this format (dd/mm/yyyy 00:00:00) upon page load. This is bound to an SQL table with a column declared as "Date" type.</p>
  
  <p>In the footer template, there is an "Insert" button as well as textboxes in the various columns of the gridview. Upon clicking the button, the new record should be inserted into the SQL server Datatable.</p>
  
  <p>However, there is a debug error with an error saying:
  <strong>SQLException was unhandled by usercode.
  Conversion failed when converting date and/or time from character string</strong></p>
  
  <p>Here is the code for this method:</p>
  
  <pre><code>protected void insertRowToDOitems(object sender, GridViewCommandEventArgs e)
      {
          if (e.CommandName == "Add")
          {
              string strDO_no = ((TextBox)GridView1.FooterRow.FindControl("txtdo_no")).Text;
              string strDO_item_no = ((TextBox)GridView1.FooterRow.FindControl("txtdo_item_no")).Text;
              string strMaterial = ((TextBox)GridView1.FooterRow.FindControl("txtmaterial")).Text;
              string strDeliveryQty = ((TextBox)GridView1.FooterRow.FindControl("txtdelivery_qty")).Text;
              string strSO_no = ((TextBox)GridView1.FooterRow.FindControl("txtso_no")).Text;
              string strSOItem_no = ((TextBox)GridView1.FooterRow.FindControl("txtso_item_no")).Text;
              string strUOM = ((TextBox)GridView1.FooterRow.FindControl("txtuom")).Text;
              string strDeliveryDate = ((TextBox)GridView1.FooterRow.FindControl("txtDelivery_date")).Text;
  
              //SQLDataSource parameters
              DOitem.InsertParameters.Clear();
              DOitem.InsertParameters.Add("do_no", strDO_no);
              DOitem.InsertParameters.Add("do_item_no", strDO_item_no);
              DOitem.InsertParameters.Add("material", strMaterial);
              DOitem.InsertParameters.Add("delivery_qty", strDeliveryQty);
              DOitem.InsertParameters.Add("so_no", strSO_no);
              DOitem.InsertParameters.Add("so_item_no", strSOItem_no);
              DOitem.InsertParameters.Add("uom", strUOM);
              DOitem.InsertParameters.Add("delivery_date", strDeliveryDate);
  
              DOitem.Insert();
          }
      }
  </code></pre>
  
  <p>And this method was called from the GridView tag:</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                      DataKeyNames="do_no,do_item_no" DataSourceID="DOitem" CellPadding="4" 
                      GridLines="None" 
                      AllowSorting="True" ShowFooter="True" style="margin-top: 1px" 
                      ForeColor="#333333" Height="58px" Width="546px" OnRowCommand="insertRowToDOitems"&gt;
  </code></pre>
  
  <p>If the error is due to difference in Date format or type, how can this be solved?</p>
  
  <p>Also, how can I display the date retrieved from SQL table in the gridview like this ("dd/MM/yyyy")? I've tried the following but it doesn't work:</p>
  
  <pre><code>&lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="txtDelivery_date" runat="server"
                                           Text='&lt;%#Bind("delivery_date", "{0:dd/MM/yyyy}") %&gt;'&gt;
                              &lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
  </code></pre>