Convert ASP.NET textbox control .text contents to Date/Time format

Kay

New Member
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 formatI have tried this so far:VB:\[quote\]\[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\]\[/quote\]Here is my clientside code just for reference:\[quote\]\[code\] <h1>Add An Event!<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </ajaxToolkit:ToolkitScriptManager> </h1> <p>Title of Event: <asp:TextBox ID="tb_eventtitle" runat="server"></asp:TextBox> </p> <p>Event Description: <asp:TextBox ID="tb_eventdescription" runat="server"></asp:TextBox> </p> <p>Event Date: <asp:TextBox ID="tb_eventdate" runat="server"></asp:TextBox> <ajaxToolkit:CalendarExtender ID="tb_eventdate_CalendarExtender" runat="server" TargetControlID="tb_eventdate"> </ajaxToolkit:CalendarExtender> </p> <p>Event Category: <asp:DropDownList ID="dd_eventcategory" runat="server" DataSourceID="SqlDataSource1" DataTextField="CategoryTitle" DataValueField="CategoryTitle"> </asp:DropDownList> </p> <p> <asp:Button ID="Button1" runat="server" Text="Submit" /> </p>\[/code\]\[/quote\]When I try to fill out the form, I receive this error:
UJ8Ka.png
My Two questions are:[*]What is wrong with the code above, and how do I successfully use the DateTimeFormatInfo class to convert String to Date/Time?[*]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?)Thanks in advance for your answers!Adam EDIT: Updated code below:\[quote\]\[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\]\[/quote\]
 
Back
Top