提交方法无法识别AddWithValue

时间:2013-08-25 01:59:36

标签: c# asp.net visual-studio-2010

我试图让我的提交按钮将输入的数据插入到几个文本框和一个隐藏字段中。现在,它给我一个错误,说明名称" 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>

1 个答案:

答案 0 :(得分: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)
{ 
}

注意:改变你的建议!

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>