我正在尝试插入数据库 - 有关事件的各种详细信息。asp.net 文本框正在使用日历扩展器(因此会弹出一个小日历并用正确格式的日期填充文本框)。我的 Access 数据库中的 EventDate 字段属于日期/时间类型。我需要将文本/字符串转换为日期/时间格式
到目前为止,我已经尝试过:
VB:
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
这是我的客户端代码仅供参考:
<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>
当我尝试填写表格时,我收到此错误:
我的两个问题是:
- 上面的代码有什么问题,如何成功使用 DateTimeFormatInfo 类将字符串转换为日期/时间?
- 附带说明,日历扩展器以美国时间格式 (MM/DD/YYYY) 将日期输入到文本框中,如何将其更改为英国 (DD/MM/YYYY) 格式(我看不到明显的属性在属性对话框中执行此操作?)
提前感谢您的回答!
亚当
编辑:更新代码如下:
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