2

いくつかのテキスト ボックスと非表示フィールドに入力されたデータを挿入する送信ボタンを取得しようとしています。現在、「txtComments」、「txtName」、および「datePosted」という名前が現在のコンテキストに存在しないというエラーが表示されます。変数を作成する必要があることは比較的確かですが、それぞれの ASP コントロールの内容と同じであることを確認するにはどうすればよいでしょうか? これが私のコードです:

protected void submitButton_Click(object sender, EventArgs e)
{
    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=~\App_Data\TravelJoansDB.accdb";
    string cmdstr = "INSERT INTO Comments VALUES (@txtComments, @datePosted, @personName)";

    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand com = new OleDbCommand(cmdstr, con);
    con.Open();
    com.Parameters.AddWithValue("@txtComments", txtComments.TextBox);
    com.Parameters.AddWithValue("@datePosted", datePosted.DateTime);
    com.Parameters.AddWithValue("@personName", txtName.TextBox);
    com.ExecuteNonQuery();
    con.Close();
}

では、変数がaspコントロールに設定されていることを確認するにはどうすればよいですか? ここにあるもの:

<InsertItemTemplate>
    Name: <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("personName") %>'></asp:TextBox><br />
    Comments:<br />
    <asp:TextBox ID="txtComments" runat="server" Text='<%# Bind("commentText") %>'
                TextMode="MultiLine" Rows="4" Columns="50"></asp:TextBox><br />
    <asp:HiddenField ID="hidTimeDate" runat="server" Value='<%# Bind("datePosted") %>' />
    <asp:Button ID="InsertButton" runat="server" CausesValidation="True" 
                    CommandName="Insert" Text="Submit" OnClick="submitButton_Click" />
</InsertItemTemplate>
4

1 に答える 1

1

コードビハインド:

protected void submitButton_Click(object sender, EventArgs e)
{

    //string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=~\App_Data\TravelJoansDB.accdb";
    string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database21.accdb;";
    string cmdstr = "INSERT INTO Comments(commentText,datePosted,personName) VALUES (@txtComments, @datePosted, @personName)";
    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand com = new OleDbCommand(cmdstr, con);
    TextBox tComments = (TextBox)FormView1.FindControl("txtComments");
    HiddenField tDate = (HiddenField)FormView1.FindControl("hidTimeDate");
    TextBox tName = (TextBox)FormView1.FindControl("txtName");
    con.Open();
    com.Parameters.AddWithValue("@txtComments", tComments.Text);
    com.Parameters.AddWithValue("@datePosted", DateTime.Now.ToString());
    //com.Parameters.AddWithValue("@datePosted", tDate.Value.ToString());
    com.Parameters.AddWithValue("@personName", tName.Text);
    com.ExecuteNonQuery();
    con.Close();
}
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{ 
}

注:constrを変更してください!

ASPX:

<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" 
    oniteminserting="FormView1_ItemInserting">
    <InsertItemTemplate>
        Name: <asp:TextBox ID="txtName" runat="server" 
    Text='<%# Bind("personName") %>'></asp:TextBox><br />
Comments:<br />
<asp:TextBox ID="txtComments" runat="server" Text='<%# Bind("commentText") %>'
            TextMode="MultiLine" Rows="4" Columns="50"></asp:TextBox>
            <br />
<asp:HiddenField ID="hidTimeDate" runat="server" Value='<%# Bind("datePosted") %>' />
<asp:Button ID="InsertButton" runat="server" CausesValidation="True" 
                CommandName="Insert" Text="Submit" OnClick="submitButton_Click" />
    </InsertItemTemplate>
</asp:FormView>
于 2013-08-25T02:15:58.533 に答える