该字段在代码隐藏中的当前上下文中不存在

本文关键字:上下文 不存在 代码 隐藏 字段 | 更新日期: 2023-09-27 18:29:01

请协助解决问题。我有一个asp.net应用程序。在*.aspx文件中,我有<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>,后面的代码是cmd = new SqlCommand("insert into tbl_newemp values('"txtemail.Text"')", con);。当我试图编译我的解决方案时,我得到了以下错误

The name 'txtemail' does not exist in the current context

该字段在代码隐藏中的当前上下文中不存在

您可以在解决方案资源管理器中右键单击页面,然后有一个选项,类似于"转换为Web应用程序",它将重新生成设计器文件。

我想你想做的是:

cmd = new SqlCommand("insert into tbl_newemp values('" + txtemail.Text + "')", con);

然而,你确实应该使用一个参数

cmd = new SqlCommand("insert into tbl_newemp values(@p0)", con);
cmd.Parameters.Add("@p0", GetType(String).Value = txtemail.Text;

类似的东西。