0

我是一名新的 ASP.NET 开发人员,在将一些数据插入数据库时​​遇到了问题。我有一个创建事件的表格。填充的数据应该被插入到数据库中。我有以下数据库设计:

列:(ID, Title, Description, StartDateTime, EndDateTime, IsActive
并且StartDateTimeEndDateTime数据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 &rarr;" onclick="submitButton_Click" />
              </fieldset>
            </div>

仅供参考,我正在使用 jQuery UIDateTimePicker插入StartDateTimeand 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);
    }

我收到以下错误:

在此处输入图像描述

4

2 回答 2

4

您在文本框上调用 ToString 而不是从中检索文本。所以传递给数据库的值是字符串"System.Web.UI.WebControls.TextBox"而不是实际的日期/时间。

尝试替换这个:

string startDateTime = start_DateTime.ToString();
string endDateTime = end_DateTime.ToString();

有了这个:

string startDateTime = start_DateTime.Text;
string endDateTime = end_DateTime.Text;

您拥有的第一个代码隐藏(更新之前)应该适用于此更改......至少它对我有用。:)

于 2012-08-12T04:20:57.173 回答
2

两行都说@StartDateTime。将第二个更改为 @EndDateTime :)

  cmd.Parameters.AddWithValue("@StartDateTime", "'" + startDateTime + "'");
  cmd.Parameters.AddWithValue("@StartDateTime", "'" + endDateTime + "'");

而且您不必将它们转换为字符串,也不必添加'

于 2012-08-12T04:18:26.790 回答