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>

参考までに、とDateTimePickerを挿入するためにjQueryUIを使用しています。StartDateTimeEndDateTime

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と言っています。2番目のものを@EndDateTimeに変更します:)

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

そして、それらを文字列に変換したり、追加したりする必要はありません'

于 2012-08-12T04:18:26.790 に答える