Saturday, 24 August 2013

Submit method not recognizing AddWithValue

Submit method not recognizing AddWithValue

I'm trying to get my submit button to insert data entered into a couple
text boxes and a hidden field. Right now, it gives me an error saying that
the name "txtComments", "txtName", and "datePosted" do not exist in the
current context. I'm relatively certain that I have to create the
variable, but how would I make sure they are equal to what's in the
respective ASP controls? Here's my code:
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();
}
So how do I make sure the variables are set to the asp controls? Which are
here:
<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>

No comments:

Post a Comment