我是一名新的 ASP.NET 开发人员,在将一些数据插入数据库时遇到了问题。我有一个创建事件的表格。填充的数据应该被插入到数据库中。我有以下数据库设计:
列:(ID, Title, Description, StartDateTime, EndDateTime, IsActive
并且StartDateTime
是EndDateTime
数据DateTime
类型)
当我尝试插入数据时,我遇到了以下问题,我不知道为什么:
我的代码隐藏(C#):
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);
}
我的 ASP.NET 代码:
<div id="contactform">
<fieldset>
<label for="title">
Title</label>
<asp:TextBox ID="txtTitle" CssClass="text-input" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter a subject for your message"
ControlToValidate="txtTitle"></asp:RequiredFieldValidator>
<label for="description">
Description</label>
<asp:TextBox ID="txtDescription" TextMode="MultiLine" CssClass="textarea" Rows="6" cols="50"
runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter your message"
ControlToValidate="txtDescription"></asp:RequiredFieldValidator>
<label for="location">
Location</label>
<asp:TextBox ID="txtLocation" CssClass="text-input" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Please enter your message"
ControlToValidate="txtLocation"></asp:RequiredFieldValidator>
<label for="start_DateTime">
Start Date & Time</label>
<asp:TextBox ID="start_DateTime" CssClass="textarea" runat="server"></asp:TextBox>
<%--<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="start_DateTime"
PopupPosition="Right">
</ajaxToolkit:CalendarExtender>--%>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please enter your message"
ControlToValidate="start_DateTime"></asp:RequiredFieldValidator>
<label for="end_DateTime">
End Date & Time</label>
<asp:TextBox ID="end_DateTime" CssClass="textarea" runat="server"></asp:TextBox>
<%--<ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="end_DateTime"
Format=" PopupPosition="Right">
</ajaxToolkit:CalendarExtender>--%>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please enter your message"
ControlToValidate="end_DateTime"></asp:RequiredFieldValidator>
<asp:Button ID="submitButton" runat="server" CssClass="button"
Text="Create →" onclick="submitButton_Click" />
</fieldset>
</div>
仅供参考,我正在使用 jQuery UIDateTimePicker
插入StartDateTime
and EndDateTime
。
那么如何将具有DateTime
类型的数据插入到数据库中呢?
更新#1:
我将代码更改为以下内容:
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);
}
我收到以下错误: