gpt4 book ai didi

c# - 提交方法无法识别 AddWithValue

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:50 27 4
gpt4 key购买 nike

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

最佳答案

代码隐藏:

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>

关于c# - 提交方法无法识别 AddWithValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18424946/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com