该字段在代码后面的当前上下文中不存在

时间:2014-11-04 19:31:39

标签: c# asp.net web

请协助解决问题。我有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

2 个答案:

答案 0 :(得分:1)

您可以右键单击解决方案资源管理器中的页面,并且有一个选项,例如“转换为Web应用程序”,它将重新生成您的设计器文件。

答案 1 :(得分:0)

我认为你想要做的是:

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;

类似的东西。