title: Convert ASP.NET textbox control .text contents to Date/Time format
body: <p>I am trying to insert into a database - various details about an event. The asp.net textbox is using the Calendar Extender (so a little calendar pops up and fills the textbox with a correctly formatted date). The EventDate field in my Access database is of the type Date/Time. I need to convert the text/string to date/time format</p>
  
  <p>I have tried this so far:</p>
  
  <p>VB:</p>
  
  <blockquote>
  <pre><code>Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
      Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
      Dim SqlString As String = "Insert into Events(EventTitle,EventDescription,EventDate,EventCategory) Values
     (@f1,@f2,@f3,@f4)"
      Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
  
      Dim strDate As String = tb_eventdate.Text
      Dim dtfi As New System.Globalization.DateTimeFormatInfo
      dtfi.ShortDatePattern = "dd/MM/yyyy"
      dtfi.DateSeparator = "/"
      Dim objDate As DateTime = Convert.ToDateTime(strDate, dtfi)
  
      cmd.CommandType = CommandType.Text
      cmd.Parameters.AddWithValue("@f1", tb_eventtitle.Text)
      cmd.Parameters.AddWithValue("@f2", tb_eventdescription.Text)
      cmd.Parameters.AddWithValue("@f3", tb_eventdate.Text)
      cmd.Parameters.AddWithValue("@f4", dd_eventcategory.Text)
      oleDbConn.Open()
      cmd.ExecuteNonQuery()
      System.Threading.Thread.Sleep("2000")
      Response.Redirect("~/calendar.aspx")
  End Sub
  </code></pre>
  </blockquote>
  
  <p>Here is my clientside code just for reference:</p>
  
  <blockquote>
  <pre><code> &lt;h1&gt;Add An Event!&lt;ajaxToolkit:ToolkitScriptManager
   ID="ToolkitScriptManager1" 
                     runat="server"&gt;
                 &lt;/ajaxToolkit:ToolkitScriptManager&gt;
              &lt;/h1&gt;
              &lt;p&gt;Title of Event:
                  &lt;asp:TextBox ID="tb_eventtitle" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/p&gt;
              &lt;p&gt;Event Description:
                  &lt;asp:TextBox ID="tb_eventdescription" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/p&gt;
              &lt;p&gt;Event Date:
                  &lt;asp:TextBox ID="tb_eventdate" runat="server"&gt;&lt;/asp:TextBox&gt;
                  &lt;ajaxToolkit:CalendarExtender ID="tb_eventdate_CalendarExtender" runat="server" 
                      TargetControlID="tb_eventdate"&gt;
                  &lt;/ajaxToolkit:CalendarExtender&gt;
              &lt;/p&gt;
              &lt;p&gt;Event Category:
                  &lt;asp:DropDownList ID="dd_eventcategory" runat="server" 
                      DataSourceID="SqlDataSource1" DataTextField="CategoryTitle" 
                      DataValueField="CategoryTitle"&gt;
                  &lt;/asp:DropDownList&gt;
              &lt;/p&gt;
              &lt;p&gt;
                  &lt;asp:Button ID="Button1" runat="server" Text="Submit" /&gt;
              &lt;/p&gt;
  </code></pre>
  </blockquote>
  
  <p>When I try to fill out the form, I receive this error:</p>
  
  <p><img src="https://i.stack.imgur.com/UJ8Ka.png" alt="date time error"></p>
  
  <p>My Two questions are:</p>
  
  <ol>
  <li>What is wrong with the code above, and how do I successfully use the DateTimeFormatInfo class to convert String to Date/Time?</li>
  <li>On a side note, the Calendar Extender inputs the date into the textbox in American Time format (MM/DD/YYYY), how do I change this to British (DD/MM/YYYY) format (I couldn't see an obvious property in the properties dialog to do this?)</li>
  </ol>
  
  <p>Thanks in advance for your answers!</p>
  
  <p>Adam </p>
  
  <p>EDIT: Updated code below:</p>
  
  <blockquote>
  <pre><code>Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
      Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
      Dim SqlString As String = "Insert into Events(EventTitle,EventDescription,EventDate,EventCategory) Values
   (@f1,@f2,@f3,@f4)"
      Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
      cmd.CommandType = CommandType.Text
      cmd.Parameters.AddWithValue("@f1", tb_eventtitle.Text)
      cmd.Parameters.AddWithValue("@f2", tb_eventdescription.Text)
      cmd.Parameters.AddWithValue("@f3", DateTime.ParseExact(tb_eventdate.Text, "dd/MM/yyyy",
     CultureInfo.InvariantCulture))
      cmd.Parameters.AddWithValue("@f4", dd_eventcategory.Text)
      oleDbConn.Open()
      cmd.ExecuteNonQuery()
      System.Threading.Thread.Sleep("2000")
      Response.Redirect("~/calendar.aspx")
  End Sub
  </code></pre>
  </blockquote>