いくつかのテキスト ボックスと非表示フィールドに入力されたデータを挿入する送信ボタンを取得しようとしています。現在、「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>